我写的一个应用程序包包含一个主类,其中主要方法是主要方法,并且与GUI类分开,而GUI类包含一个带有JTABBEDPANE的Jframe,该Jframe具有两个选项卡,第一个选项卡包含一个jtable,将其称为jtable1,第二个选项卡包含3个表,将它们称为jtable2,jtable3和jtable4,我在GUI类构造函数中添加了,该构建器在主类中的新实例中被调用,我添加了此GUI类构造函数listSelectionListener到第一个JTable:
jTable1.getSelectionModel().addListSelectionListener((ListSelectionEvent event) -> { // do some actions here, for example /* my code */ });
而且,通过良好的命运,它运作良好,可以选择这样的选择而且我还为GUI类构造函数的Jtabbedpane添加了ChangElistener,它也很好:
jTabbedPane1.addChangeListener((ChangeEvent e) -> {
/* my code */
});
但是,当我尝试在GUI类构造函数中添加listSelectionListener时,将其称为jtable2,在其中包含三个表的第二个选项卡中,并将listselectionListener添加到它们,它不会响应任何选择,以及将ListSelectionListener添加到JTable2的代码为:
jTable2.getSelectionModel().addListSelectionListener((ListSelectionEvent event) -> { System.out.println("jTable2 Selected"); /* mycode */ });
毫无疑问,问题是什么问题以及如何解决?但是,如果它并不简单,那么概率是什么,或者我该如何解释?
注意:将添加ListSelectionListener的添加将在第二个选项卡(JTable3和jtable4)中的其他表中完成,并且愿意将其添加到其他计划的选项卡中,这些选项卡将被创建并包含表
>感谢您的外观和关怀
问题在于重新创建未聆听的动作在其jtabbedpane的更改中,就像在每个标签更改中一样,我正在重新创建JTable JTable,以使用新输入的信息来对其进行更新,以更新它。其他选项卡的jtables:
jTabbedPane1.addChangeListener((ChangeEvent e) -> {
if(jTabbedPane1.getSelectedIndex() == 1)
{
jTable2 = new javax.swing.JTable(){
DefaultTableCellRenderer renderRight =
new DefaultTableCellRenderer();
{ // initializer block
renderRight.setHorizontalAlignment(SwingConstants.RIGHT);
}
@Override
public TableCellRenderer getCellRenderer (int arg0, int arg1)
{
return renderRight;
}
public boolean isCellEditable(int row, int column) {
return false;
};
}});
jTable2.setAutoCreateRowSorter(true);
jTable2.setFont(new java.awt.Font("Traditional Arabic", 0, 23));
jTable2.setRowHeight(25);
DefaultTableModel workersJtableForFamilyModel =
new DefaultTableModel();
/* update model with columns and rows, and set it */
}
});
因此,问题在于重新创建JTable,因此从指向和侦听中遗漏了收到的记忆中观察到的JTable对象,因此解决方案是在不创建新的JTable对象的情况下更新JTable的模型,然后将其设置为JTable:
jTabbedPane1.addChangeListener((ChangeEvent e) -> {
if(jTabbedPane1.getSelectedIndex() == 1)
{
DefaultTableModel workersJtableForFamilyModel =
new DefaultTableModel();
/* Feed the model with new columns and rows */
/* write updated Model prepration statements */
/* set the updated model to the jtable */
jTable2.setModel(workersJtableForFamilyModel);
}
});