I'm a Java beginner and I'm using Eclipse to create an simple app with a SpringLayout
and a button inside. I call that button 'btnTABLE' and here is its actionPerformed
code:
btnTABLE.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)}
};
JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
}
});
But when i click that button, the table doesn't show up.
simple app with a SpringLayout
A SpringLayout is a complicated layout manager. You can' just use simple code like:
frame.getContentPane().add(scrollPane);
When using SpringLayout you need to specify a few constraints. Read the section from the Swing tutorial on How to Use SpringLayout for more information.
Also, if you are attempting to dynamically add a component to a visible GUI then the basic code is:
panel.add(...);
panel.revalidate();
panel.repaint();
I would suggest you should be using a different layout manager for the panel.