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
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?
精彩评论