Όταν φτιάχνω και γεμίζω "καρφωτά" το JTable μέσα από τον κατασκευαστή της Demo κλάσης που έχω, το JTable εμφανίζεται κανονικά.
Όταν όμως πάω να το παράγω μέσα από τον listener handler (actionPerformed()) τότε δεν εμφανίζεται τίποτα...
Είμαι πεπεισμένος ότι κάνω πολύ τραγικό λάθος αλλά δε μπορώ να δω τι ακριβώς. Παραθέτω ενδεικτικό κώδικα (είναι απλοποιημένος ώστε να μην περιλαμβάνει το κομμάτι της βάσης δεδομένων και να τρέχει standalone) αν μπορεί να βοηθήσει κάποιος.
Έχω βάλει να εμφανίζεται το table (μία μόνο πλειάδα) όταν επιλέγουμε την "Customers" option στον listener.
Code: Select all
package comboboxjtabledemo;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.Vector;
public class ComboBoxJTableDemo extends JPanel implements ActionListener {
JLabel picture;
JTable table;
JScrollPane scroll;
String[] columnNames;
DefaultTableModel model;
public ComboBoxJTableDemo() {
super(new BorderLayout());
String[] options = { "Customers", "Agencies", "Rooms", "Employees", "Services" };
//Create the combo box, select the item at index 1.
JComboBox optionList = new JComboBox(options);
optionList.setSelectedIndex(1);
optionList.addActionListener(this);
//Set up the picture.
picture = new JLabel();
picture.setFont(picture.getFont().deriveFont(Font.ITALIC));
picture.setHorizontalAlignment(JLabel.CENTER);
//updateLabel(options[optionList.getSelectedIndex()]);
picture.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
//The preferred size is hard-coded to be the width of the
//widest image and the height of the tallest image + the border.
//A real program would compute this.
picture.setPreferredSize(new Dimension(177, 122+10));
//Lay out the demo.
add(optionList, BorderLayout.PAGE_START);
add(picture, BorderLayout.PAGE_END);
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
/*TESTING code from within constructor*/
/*
columnNames[0] = "ID";
columnNames[1] = "Name";
columnNames[2] = "Surname";
columnNames[3] = "Father name";
columnNames[4] = "Address";
olumnNames[5] = "Telephone";
columnNames[6] = "County";
columnNames[7] = "City";
model = new DefaultTableModel(columnNames, 0);
Vector<String> row = new Vector<String>();
row.add("1");
row.add("John");
row.add("Johnakis");
row.add("Sr. John");
row.add("Adamantos 4");
row.add("0123456789");
row.add("Papua New Guinea");
row.add("Noland");
model.addRow(row);
table = new JTable(model);
scroll = new JScrollPane(table);
add(scroll);*/
}
/** Listens to the combo box. */
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource();
String option = (String)cb.getSelectedItem();
if (option.equals("Customers"))
{
columnNames[0] = "ID";
columnNames[1] = "Name";
columnNames[2] = "Surname";
columnNames[3] = "Father name";
columnNames[4] = "Address";
columnNames[5] = "Telephone";
columnNames[6] = "County";
columnNames[7] = "City";
model = new DefaultTableModel(columnNames, 0); //0 data rows for the moment
//add 1 record/row manually just for testing
Vector<String> row = new Vector<String>();
row.add("1");
row.add("John");
row.add("Johnakis");
row.add("Sr. John");
row.add("Adamantos 4");
row.add("0123456789");
row.add("Papua New Guinea");
row.add("Noland");
model.addRow(row);
table = new JTable(model);
}
else //just to prevent Null Pointer Exception
{
table = new JTable();
}
scroll = new JScrollPane(table);
add(scroll);
table.updateUI();
}
/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = ComboBoxJTableDemo.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("ComboBoxDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new ComboBoxJTableDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}