2012-07-24 105 views
0

在提出我的問題之前,我會嘗試解釋我需要做什麼。我已成立了一個表,其中有幾個欄顯示一個下拉框如圖所示:如何從jtable中的combobox獲取值和選定的值?

picture http://www.freeimagehosting.net/newuploads/4ks9s.png

這應該創造的就業機會這個意義上,工作1將首先去車站的「訂單」 1.如果我在車站2列上添加車站4,則它將前往車站4,依此類推。它旨在創建進一步處理的訂單。所以,我想:

  • 創建表並顯示呈現的組合框;
  • 如果前一列的值爲「無」(從而確保它保持正確的順序),使列(3-6)中的單元格不可編輯;
  • 不要顯示已經爲該行選擇的工作站;

但是對於初學者來說,我無法獲得從組合框中選擇的值,也無法獲得這些值!

繼承人是我的代碼到目前爲止。

創建組合框:

public class SimGui extends JFrame { 
          //implements ActionListener { 
    String Stations[] = new String[] {"Station 1","Station 2","Station 3","Station 4","Station 5","None"}; 
    JComboBox stationscombo = new JComboBox(Stations); 
    Object obj = stationscombo.getSelectedItem(); 
在桌子上

鼠標點擊事件:

private void jTable2MouseClicked(java.awt.event.MouseEvent evt) {          
    //Object event = evt.getSource(); 
    obj = stationscombo.getSelectedItem(); 
    System.out.println("Item: " + obj); 
    //ListSelectionModel selectionModel = jTable2.getSelectionModel(); 
    int tb1columns = jTable2.getColumnCount(); 
    int selectionrow= jTable2.getSelectedRow(); 
    int selectioncolumn = jTable2.getSelectedColumn(); 
    if (selectioncolumn > 1) { 
     for (int i=2;i<tb1columns;i++) { 
      System.out.println(jTable2.getValueAt(selectionrow,selectioncolumn)); 
      /*if (jTable2.getValueAt(selectionrow, i) != "None") { 
       stationscombo.removeItem(jTable2.getValueAt(selectionrow, i)); 
      }*/ 
     } 
    } 
    else { System.out.println(jTable2.getValueAt(selectionrow,selectioncolumn)); } 
} 

表結構:

jTable2.setModel(new javax.swing.table.DefaultTableModel(
    new Object [][] { 
     {null, null, null, null, null, null, null}, 
     {null, null, null, null, null, null, null}, 
     {null, null, null, null, null, null, null} 
    }, 
    new String [] { 
     "Job Type", "Parts", "Station 1", "Station 2", "Station 3", "Station 4", "Station 5" 
    } 
) { 
    Class[] types = new Class [] { 
     java.lang.Integer.class, java.lang.Integer.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class 
    }; 
    boolean[] canEdit = new boolean [] { 
     false, true, true, true, true, true, true 
    }; 

    public Class getColumnClass(int columnIndex) { 
     return types [columnIndex]; 
    } 

    public boolean isCellEditable(int rowIndex, int columnIndex) { 
     return canEdit [columnIndex]; 
    } 
}); 
jTable2.getTableHeader().setReorderingAllowed(false); 
for (int x = 2;x<7;x++) { 
    jTable2.getColumnModel().getColumn(x).setCellEditor(new DefaultCellEditor(stationscombo)); 
} 
jTable2.addMouseListener(new java.awt.event.MouseAdapter() { 
    public void mouseClicked(java.awt.event.MouseEvent evt) { 
     jTable2MouseClicked(evt); 
    } 
}); 

我環顧四周,試圖實現一個監聽器爲組合框但失敗。如果我在類實現ActionListener它會顯示一個警告說:

SimGui is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener 

我有alreay爲這是工作表中實現一個ActionListener,但我不知道這是否可能會干擾?

Action action = new AbstractAction() { 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     System.out.println(e.getSource()); 
     //TableCellListener tcl = (TableCellListener)e.getSource(); 
     //JComboBox cb = (JComboBox)e.getSource(); 
     //String newSelection = (String)cb.getSelectedItem(); 
     /*JComboBox cb = (JComboBox)e.getSource(); 
     String teste = (String)cb.getSelectedItem(); 
     System.out.println("Item: " + teste);*/ 
     //TableCellListener tcl1 = new TableCellListener(jTable1, action); 
     /*if (tcl.getColumn() == 3) { 
      if (tcl.getNewValue() == true) { 
       int x = tcl.getColumn(); 
       table1model.setColumnEditable(x, true); 
      } 
      else { 
       table1model.setColumnEditable(tcl.getColumn(), false); 
      } 
      /*boolean canEdit[] = { 
      false, true, true, true, true, true 
      }; 
      //System.out.println(isCellEditable(tcl1.getRow(),tcl1.getColumn())); 
     }*/ 
     /*System.out.println(newSelection); 
     System.out.println("Row : " + tcl.getRow()); 
     System.out.println("Column: " + tcl.getColumn()); 
     System.out.println("Old : " + tcl.getOldValue()); 
     System.out.println("New : " + tcl.getNewValue());*/ 
    } 
}; 

但底線,我的問題是,我怎麼能正確地與表內的組合框工作,使我能得到的值設定和選擇時,獲得該項目的價值?

+1

爲了儘快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2012-07-24 02:43:40

回答

相關問題