I created a GUI class that displays a list of items. When the user selects an item, it pops a new window that displays a JTable, which is supposed to give detailed information for that item (two columns: parameter on the left, the value on the right). This (kind of) worked until I decided to make all the methods and variables in the table class non-static, in case the user wants to open several table windows in the same time.
And then it stopped working. The table won’t show. All the code executes, but the table is empty, even after I use the add method of its model.
(also, I’d appreciate comments about a better idea for the GUI, mainly because one of the fields is called “description” and can be very long. I am writing the application for myself, so function is first priority)
Edit: here is some code, don’t hesitate to ask me for more. (I didn’t copy it in the first place because I assumed it was simple enough)
JTable class, starting from line 82 – all the rest is automatically generated.
public void main(ArrayList<String> l, ArrayList<String> d) {
labels = l;
data = d;
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(PopupTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(PopupTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(PopupTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(PopupTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PopupTable().setVisible(true);
model.addColumn("Description");
model.addColumn("Value");
for (int i = 0; i < data.size(); i++) {
Object[] r = {labels.get(i), data.get(i)};
System.out.println(labels.get(i) + data.get(i));
model.addRow(r);
}
Object[] asd = {"Name", "Skelet"};
model.addRow(asd);
}
});
}
public static void asd() {
System.out.println("Bazinga!");
}
private ArrayList<String> labels;
private ArrayList<String> data;
private DefaultTableModel model = new DefaultTableModel();
;
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable table;
// End of variables declaration
}
The other class calls this one’s “main” method by sending two lists, one for the field data and another for the description of each data (first would send “2000”, second would send “Experience”). As I said, everything works if the variables and methods are static. If they’re not, everything works, except for the table, which doesn’t show up.
Here’s the code from the outside class, which I use to create the Table class:
new PopupTable().main(list1, list2);
I just noticed that the run() method had:
So I deleted it and then run the class like that:
Now everything works perfectly! I can’t believe it was so simple, I thought I looked at everything and mistakenly concluded it was a fundamental problem with JTables and static/non-static.