2012-12-04 2 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 combobox에서 일부 값을 선택하면 column2 콤보 상자를 채워야합니다. 이제 원하는 작업을 수행하는 콤보 상자가 있습니다. 예를 들어 사용자가 column1 combobox에서 value1을 선택한 경우 column2 combobox는 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

관련 문제