[英]JTable in non-static object
我創建了一個顯示項列表的GUI類。 當用戶選擇一個項目時,它會彈出一個顯示JTable的新窗口,該窗口應該提供該項目的詳細信息(兩列:左邊的參數,右邊的值)。 這種(有點)工作,直到我決定使表類中的所有方法和變量非靜態,以防用戶想要同時打開幾個表窗口。
然后它就停止了工作。 該表不會顯示。 所有代碼都會執行,但即使我使用其模型的add方法,表也是空的。
(另外,我很欣賞有關GUI的更好主意的評論,主要是因為其中一個字段被稱為“描述”並且可能很長。我正在為自己編寫應用程序,因此函數是第一優先級)
編輯:這里有一些代碼,請不要猶豫,問我更多。 (我沒有在第一時間復制它,因為我認為這很簡單)
JTable類,從第82行開始 - 所有其余的都是自動生成的。
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
}
另一個類通過發送兩個列表來調用這個“主”方法,一個用於字段數據,另一個用於每個數據的描述(首先發送“2000”,第二個發送“體驗”)。 正如我所說,如果變量和方法是靜態的,一切都有效。 如果它們不是,那么一切都有效,除了沒有出現的桌子。
這是來自外部類的代碼,我用它來創建Table類:
new PopupTable().main(list1, list2);
我剛注意到run()方法有:
new PopupTable().setVisible(true);
所以我刪除了它然后像這樣運行類:
PopupTable pt = new PopupTable();
pt.main(MagiciaFileManipulator.getMetadata(), itemList.get(0));
pt.setVisible(true);
現在一切都很完美! 我不敢相信它是如此簡單,我以為我看了一切,錯誤地認為它是JTables和靜態/非靜態的基本問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.