我遇到了JTables和JTabbedPane的问题
我有一个jtable" jtable1"在我的JFrame的主面板中 我有一个seconde JTable" jtable2"我的JFrame中的JTabbedPane(也包含在主面板中)。
在我的JFrame构造函数中:
jatble1.getModel().addTableModelListener(this);
jtable2.getModel().addTableModelListener(this);
我的JFrame实现TableModelListener并重新定义tableChanged方法
@Override
public void tableChanged(TableModelEvent e) , only work for jtable1.
我想在布尔列值上对我的jtable2(在JTabbedPaned内部)进行更改,然后在JTabbedPane外部更新jtable1。
怎么做?
开始回答:
Detecting the table of an onChanged() event with multiple JTable in a JTabbedPane但@ user3563124和@MadProgrammer没有提供 实施
@ user3563124如何实现他的onChanged()方法?所以它会帮助我。
感谢您的帮助。
编辑:
public class JTableInteraction extends javax.swing.JFrame implements TableModelListener{
// Model of the JTable which is in JFrame
private StudentAbsenceModel studentAbsenceModel;
// Model of the JTable which is in JTabbedPane
private StudentListModel studentListModel;
/**
* Creates new form JTableInteraction
*/
public JTableInteraction() {
initComponents();
studentAbsenceModel = new StudentAbsenceModel();
studentListModel = new StudentListModel();
jtableAbsence.setModel(studentAbsenceModel);
jtableListe.setModel(studentListModel);
// Add TableModelListener to the JTable
jtableAbsence.getModel().addTableModelListener(this);
// from jtabbedpane
jtableListe.getModel().addTableModelListener(this);
}
@Override
public void tableChanged(TableModelEvent e) {
System.out.println("Model used : " + e.getClass());
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jtableAbsence = new javax.swing.JTable();
jTabbedPane1 = new javax.swing.JTabbedPane();
jPanel1 = new javax.swing.JPanel();
jScrollPane2 = new javax.swing.JScrollPane();
jtableListe = new javax.swing.JTable();
jXSearchField1 = new org.jdesktop.swingx.JXSearchField();
jPanel2 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Absence management");
jScrollPane2.setViewportView(jtableListe);
jXSearchField1.setName(""); // NOI18N
jXSearchField1.setPrompt("Write a search criteria");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JTableInteraction().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTabbedPane jTabbedPane1;
private org.jdesktop.swingx.JXSearchField jXSearchField1;
private javax.swing.JTable jtableAbsence;
private javax.swing.JTable jtableListe;
// End of variables declaration
}
所以我想如果我在第二个JTable(jtabbedPane里面的jtableListe)中自动检查/取消选中缺席第一个JTable(JFrame JTableInteraction中的jtableAbsence)要更新,反之亦然。
怎么做?
注意:对于此方法
@Override
public void tableChanged(TableModelEvent e) {
System.out.println("Model used : " + e.getClass());
}
Result out:
Model used: com.test.tablemodels.StudentAbsenceModel
com.test.tablemodels.StudentListModel 从不显示即使我选中/取消选中&#34;缺席&#34;来自JTabbedPane的jtableListe列。
因此,对于jtableAbsence事件,此方法对jtableListe事件的作用不适用于jtableAbsence事件到jtableListe(请注意,2个表模型将TableModelListerner作为侦听器)。
需要帮助,谢谢。