2012-12-04 21 views
2
public class TablePanel extends JPanel implements ActionListener,Serializable 
{ 
    JTable m_table; 
    JComboBox combo,combo1; 
    DefaultTableModel model=new DefaultTableModel(); 
    DefaultComboBoxModel model1=new DefaultComboBoxModel(); 
    DefaultComboBoxModel model2=new DefaultComboBoxModel(); 
     List<String> field; 
    List<String> attrCode; 
    TablePanel() 
    { 

      m_table=new JTable(model); 
      m_table.setBackground(Color.WHITE); 
      model.addColumn("col1"); 
      model.addColumn("col2"); 
      model.addColumn("col3"); 
      model.addColumn("col4"); 
      model.addColumn("col5"); 
      model.addColumn("col6"); 
      JScrollPane scrollpane=new JScrollPane(m_table); 
      scrollpane.setBackground(Color.WHITE); 
      Dimension d = m_table.getPreferredSize(); 
      scrollpane.setPreferredSize(
       new Dimension(d.width,m_table.getRowHeight()*15+1)); 
      add(scrollpane); 

      } 

         attrCode = service.getAllAttributes(value); 
       combo1=new JComboBox(model2); 
       model1.addElement(attrCode.get(0)); 
       model1.addElement(attrCode.get(1)); 
      model1.addElement(attrCode.get(2)); 
      model1.addElement(attrCode.get(3)); 
      model1.addElement(attrCode.get(4)); 
      model1.addElement(attrCode.get(5)); 
      model1.addElement(attrCode.get(6)); 
      col=m_table.getColumnModel().getColumn(0); 
      col.setCellEditor((new DefaultCellEditor(combo1))); 
      combo2=new JComboBox(model3); 
      model3.addElement(trans.get(0)); 
      model3.addElement(trans.get(1)); 
      model3.addElement(trans.get(2)); 
      model3.addElement(trans.get(3)); 
      model3.addElement(trans.get(4)); 
      col=m_table.getColumnModel().getColumn(2); 

      col.setCellEditor((new DefaultCellEditor(combo2)));} } 

我有一個表,表中有一些列。兩列現在有組合框現在我想要做的是,當用戶從column1組合框中選擇一些值時,基於用戶選擇的值column2組合框應填充。例如,如果用戶從column1組合框中選擇value1,則column2組合框將僅顯示與value1相對應的值。在jtable中爲列中的每一行添加不同的組合框

+0

你沒有共享任何代碼,你已經嘗試過。 – vels4j

+0

請幫助我。它緊急。 – Nidhi

+0

以下答案有效? – vels4j

回答

5

Render兩列。

TableColumn comboCol1 = table.getColumnModel().getColumn(0); 
TableColumn comboCol2 = table.getColumnModel().getColumn(1); 
comboCol1.setCellEditor(new CustomComboBoxEditor()); 
comboCol2.setCellEditor(new CustomComboBoxEditor()); 

//這是第二列,這取決於第一列的選擇。

public class CustomComboBoxEditor extends DefaultCellEditor { 

// Declare a model that is used for adding the elements to the `ComboBox` 
private DefaultComboBoxModel model; 

public CustomComboBoxEditor() { 
    super(new JComboBox()); 
    this.model = (DefaultComboBoxModel)((JComboBox)getComponent()).getModel(); 
} 

@Override 
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { 


    if(column == 0) { 
     // Just show the elements in the JComboBox.   
    } else { 

      // Remove previous elements every time. 
      // So that we can populate the elements based on the selection. 
      model.removeAllElements(); 

      // getValueAt(..) method will give you the selection that is set for column one. 
      String selectedItem = table.getValueAt(row, 0); 

      // Using the obtained selected item from the first column JComboBox 
      // selection make a call ans get the list of elements. 

     // Say we have list of data from the call we made. 
     // So loop through the list and add them to the model like the following. 
     for(int i = 0; i < obtainedList.size(); i++) { 
       model.addElement(obtainedList.get(i)); 
     } 
    } // Close else 

    // finally return the component. 
    return super.getTableCellEditorComponent(table, value, isSelected, row, column); 
} 
} 
+0

thnx的答覆。 :)你的意思是說應該有一個像TableColumn comboCol1這樣的渲染器;這是用於像comboCol1 = table.getColumnModel()。getColumn(1); comboCol1 = table.getColumnModel()。getColumn(0); – Nidhi

+0

對不起。我的疑問是在我的代碼中,我已經定義了一個單元格渲染器TableColumn col;我是否需要爲包含組合框的兩列定義不同的單元格渲染器? – Nidhi

+0

沒必要。一個渲染器類就足夠了。這兩列都需要使用JComboBox進行渲染.. – Amarnath

相關問題