2013-07-30 27 views
1

我是JAVA的新手,我試圖製作一個JTable。我想要的是,只要用戶從Jtable中的組合框中進行選擇,Jtable就會將當前日期放置在組合框旁邊的單元格中。我寫了一個代碼,但代碼無法提升單元格的值。這裏是我的代碼(我只想讓Jtable在第1行第4列插入「a」)。如何讓JTable監聽用戶的選擇並更新單元格值

public class GChamber extends JPanel { 
private boolean DEBUG = true; 
ListSelectionModel listSelectionModel; 
public GChamber() { 

    ... 

    JTable table1 = new JTable(new Table1()); 
    listSelectionModel = table1.getSelectionModel(); 

    table1.setSelectionModel(listSelectionModel); 

    ... 

    TableColumn TestName=table1.getColumnModel().getColumn(3); 
    final JComboBox comboBox= new JComboBox(); 

    //Setup comboBox. The data of comboBox is from another Jtable. 
    for(int i=0;i<table2rowlength;i++) 
    { 
    comboBox.addItem(Listofdiease[i]); 
    } 

    comboBox.addActionListener(new ActionListener() 
    TestName.setCellEditor(new DefaultCellEditor(comboBox)); 
    { 

     @Override 
     public void actionPerformed(ActionEvent e) { 

      String userinput = (String)comboBox.getSelectedItem(); 
      Table1 temp=new Table1(); 
       for(int i=0;i<table2rowlength;i++) 
       { 
        if (userinput.equals(Listofdiease[i])) { 
         temp.setValueAt("a", 1, 4); 

        } 
       } 

     } 
    }); 


    .... 


} 
public class Table1 extends AbstractTableModel { 


     String[] cName1 = {"1","2","3","4","5", "6"}; 
     Object[][] data1 = { 
       {"CC040-2", new Integer(1),"", "","",""}, 
       {"CC040-2", new Integer(2),"Rowing", "", "",""}, 
       {"CC040-2", new Integer(3),"Knitting", "", "",""}, 
       {"CC040-2", new Integer(4),"Speed reading", "", "",""}, 
       {"CC040-2", new Integer(5),"Pool", "", "",""}, 
       {"CC040-2", new Integer(6),"Pool", "", "",""}, 
       {"CC040-2", new Integer(7),"Pool", "", "",""}, 
       {"CC040-2", new Integer(8),"Pool", "", "",""}, 
       {"CC040-2", new Integer(9),"Pool", "", "",""}, 
       {"CC040-2", new Integer(10),"Pool", "", "",""}, 
       {"CC040-2", new Integer(11),"Pool", "", "",""}, 
       {"CC040-2", new Integer(12),"Pool", "", "",""}, 
       {"CEA003", new Integer(13),"Pool","", "",""}, 
       {"CEA003", new Integer(14),"Pool", "", "",""}, 
       {"CEA003", new Integer(15),"Pool", "", "",""}, 
       {"CEA003", new Integer(16),"Pool", "", "",""}, 
       {"CEA004", new Integer(17),"Pool", "", "",""}, 
       {"CEA004", new Integer(18),"Pool", "", "",""}, 
       {"CEA004", new Integer(19),"Pool", "", "",""}, 
       {"CEA004", new Integer(20),"Pool", "", "",""}, 
     }; 




     public int getColumnCount() { 
      return cName1.length; 
     } 

     public int getRowCount() { 
      return data1.length; 
     } 

     public String getColumnName(int col) { 
      return cName1[col]; 
     } 

     public Object getValueAt(int row, int col) { 
      return data1[row][col]; 
     } 


     public Class getColumnClass(int c) { 
      return getValueAt(0, c).getClass(); 
     } 


     public boolean isCellEditable(int row, int col) { 

      if (col < 2) { 
       return false; 
      } else { 
       return true; 
      } 
     } 


     public void setValueAt(Object value, int row, int col) { 
      if (DEBUG) { 
       System.out.println("Setting value at " + row + "," + col 
            + " to " + value 
            + " (an instance of " 
            + value.getClass() + ")"); 
      } 

      data1[row][col] = value; 
      fireTableCellUpdated(row, col); 

      if (DEBUG) { 
       System.out.println("New value of data:"); 
       printDebugData(); 
      } 
     } 

     private void printDebugData() { 
      int numRows = getRowCount(); 
      int numCols = getColumnCount(); 

      for (int i=0; i < numRows; i++) { 
       System.out.print(" row " + i + ":"); 
       for (int j=0; j < numCols; j++) { 
        System.out.print(" " + data1[i][j]); 
       } 
       System.out.println(); 
      } 
      System.out.println("--------------------------"); 
     } 


     public void addTableModelListener(TableModelListener l) { 


     } 

}

有沒有人能告訴我爲什麼這個代碼不工作,如何解決呢?

+0

該表不直接修改模型的狀態。相反,表格會調用你的模型.setValueAt,ethos。在這一點上,你會更新模型的狀態,因爲你需要 – MadProgrammer

回答

1

您可以使用建議的方法之一here。如果依賴列是而不是可編輯,則只需根據組合選擇從TableModel中返回所需的結果。如果依賴列可編輯,則使用顯示爲here的任一方法。

image

+0

非常感謝。 – user2635499

+0

很高興幫助;往前走,你可以通過點擊[空對號](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235)來接受這個答案。剩下。 – trashgod

相關問題