2017-11-30 1 views
1

응용 프로그램 패키지에 writting하는 중 main 클래스가 main 메소드를 포함하고 GUI 클래스와 분리되어 있습니다. GUI 클래스 두 탭을 jtabbedpane 포함되어 있습니다. 첫 번째 탭 jtable, jtable1 호출, 두 번째 탭 3 테이블, jtable2, jtable3 및 jtable4 호출 할 포함되어 있습니다, 나는 호출되는 GUI 클래스 생성자에 추가했습니다.Jtable ListSelectionListener가 jtable 작업에 응답하지 않고 같은 클래스에있는 다른 jtable 액션에 응답하지 않습니다.

jTable1.getSelectionModel().addListSelectionListener((ListSelectionEvent event) -> { 
     // do some actions here, for example 
     /* my code */ 
    }); 
: 메인 클래스의 새로운 인스턴스에서, 나는 처음 JTable가이 GUI 클래스 생성자에 ListSelectionListener에 추가되었습니다 6,

좋은 운명함으로써, 이러한 선택에 아주 잘 을 작동하고 나 또한 아주 잘 또한 작동하고 GUI 클래스 생성자의 JTabbedPane에에 ChangeListener를 추가 한 :

jTabbedPane1.addChangeListener((ChangeEvent e) -> { 
     /* my code */ 
    }); 

하지만 난 시도 할 때 GUI 클래스 생성자의 ListSelectionListener를 세 개의 테이블을 포함하고 ListSelectionListener를 추가하는 두 번째 탭에서 jtable2라고하는 첫 번째 테이블에 추가하기 시작하면 ListSelectionListener를 추가하는 코드와 선택 항목에 응답하지 않습니다. ~ jtable2는 다음과 같습니다.

jTable2.getSelectionModel().addListSelectionListener((ListSelectionEvent event) -> { 
     System.out.println("jTable2 Selected"); 
     /* mycode */ 
    }); 

의심 할 여지없이 문제는 무엇이며 어떻게 해결할 것인가? 그러나 그것이 단순하지 않다면, 어떤 확률입니까 또는 어떻게 더 설명 할 수 있습니까?

참고 : ListSelectionListener에의 추가가 다른 두 번째 탭에서 표를 할 것이다 (jtable3 및 jtable4), 그리고이 생성 될 다른 계획 탭에 기꺼이 추가 할 것이며 포함하는 테이블

보고 돌보는 주셔서 감사합니다

+0

문제를 나타내는 적절한 [mcve]를 게시하십시오. 따라서 필요한 것은 JTable과 ListSelectionListener가있는 프레임입니다. 하나의 테이블로 작업하는 경우 가져 오기. 그런 다음 지식을 사용하여 실제 프로그램을 수정하십시오. – camickr

+0

사실 나는 최소한, 완전하고 검증 가능한 예제를 쓰기 시작하기 전에 해결하려고 노력할 것이다. 직접적으로 대답을 줄 수도있다. specailly 나는 jTabbedPane addChangeListener에서 응답하지 않는 selection의 jtable을 다시 만들 때의 문제점을 발견했다. jtable의 탭은 jtable을 다시 작성하여 첫 번째 탭의 다른 테이블에서 변경 한 내용을 업데이트하므로 jTabbedPane ChangeListener에서 jtable의 재 작성을 주석 처리하면 매우 잘 작동합니다. –

답변

0

문제는 jTable의 각 j 변경이 jtable을 재 작성하여 jtable의 새 입력 정보로 업데이트하도록 jTable의 jtable을 재생성하는 것입니다. 다른 탭 :

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); 

    } 

});