views:

266

answers:

2

hello,

I would like to know how to put a button inside a frame that contain JTable inside. (The button should not be inside a cell, but after the table ends) Here is the example code I wrote so far:

class SimpleTableExample extends JFrame

{

// Instance attributes used in this example

private JPanel topPanel;

private JTable table;

private JScrollPane scrollPane; private JButton update_Button;

// Constructor of main frame
public SimpleTableExample()
{
    // Set the frame characteristics
    setTitle("Add new item" );
    setSize(300, 200);
    setBackground( Color.gray );

    // Create a panel to hold all other components
    topPanel = new JPanel();
    topPanel.setLayout( new BorderLayout() );
    getContentPane().add( topPanel );



    // Create columns names
    String columnNames[] = {"Item Description", "Item Type", "Item Price"};

    // Create some data
    String dataValues[][] = {{ "0", "Entree", "0" }};

    // Create a new table instance
    table = new JTable( dataValues, columnNames );

    ////////////////////////////

    JComboBox item_Type_Combobox = new JComboBox();
    item_Type_Combobox = new JComboBox(item_Type.values());
    TableColumn column = table.getColumnModel().getColumn(1);
    column.setCellEditor(new DefaultCellEditor(item_Type_Combobox));


    ////////////////////////////    



    // Add the table to a scrolling pane
    scrollPane = new JScrollPane( table );
    topPanel.add( scrollPane, BorderLayout.CENTER );

}

}

How do I add button after the table I created?

thanks in advance

+1  A: 

If you wanted to add another component below (for example) the table you could use:

topPanel.add( button, BorderLayout.SOUTH );

Is that what you mean?

Ash
+1  A: 

If you want to actually embed the JButton into the JTable, you can see the tutorial here:

http://ivolo.mit.edu/post/A-Simple-Pattern-for-Embedding-Components-into-a-Swing-JTable.aspx

Ilya