Swing 为jtable中列的每一行添加不同的组合框 公共类TablePanel扩展JPanel实现ActionListener,可序列化 { JTable m_表; JComboBox组合,组合1; DefaultTableModel=新的DefaultTableModel(); DefaultComboxModel1=新的DefaultComboxModel(); DefaultComboxModel2=新的DefaultComboxModel(); 列表字段; 列表属性码; 表面板() { m_表=新JTable(模型); m_表.立根台(颜色.白色); 模型。添加列(“col1”); 模型。添加列(“col2”); 模型。添加列(“col3”); 模型。添加列(“col4”); 模型。添加列(“col5”); 模型。添加列(“col6”); JScrollPane scrollpane=新的JScrollPane(m_表); 滚动窗格。背景(颜色。白色); 维度d=m_table.getPreferredSize(); scrollpane.setPreferredSize( 新维度(d.width,m_table.getRowHeight()*15+1)); 添加(滚动窗格); } attrCode=service.getAllAttribute(值); combo1=新的JComboBox(model2); model1.addElement(attrCode.get(0)); model1.addElement(attrCode.get(1)); 模型1.addElement(attrCode.get(2)); 模型1.addElement(attrCode.get(3)); 模型1.addElement(attrCode.get(4)); 模型1.addElement(attrCode.get(5)); 模型1.addElement(attrCode.get(6)); col=m_table.getColumnModel().getColumn(0); col.setCellEditor((新的DefaultCellEditor(combo1)); combo2=新的JComboBox(型号3); 模型3.addElement(trans.get(0)); 模型3.addElement(trans.get(1)); 模型3.addElement(trans.get(2)); 模型3.addElement(trans.get(3)); 模型3.addElement(trans.get(4)); col=m_table.getColumnModel().getColumn(2); col.setCellEditor((新的DefaultCellEditor(combo2));}

Swing 为jtable中列的每一行添加不同的组合框 公共类TablePanel扩展JPanel实现ActionListener,可序列化 { JTable m_表; JComboBox组合,组合1; DefaultTableModel=新的DefaultTableModel(); DefaultComboxModel1=新的DefaultComboxModel(); DefaultComboxModel2=新的DefaultComboxModel(); 列表字段; 列表属性码; 表面板() { m_表=新JTable(模型); m_表.立根台(颜色.白色); 模型。添加列(“col1”); 模型。添加列(“col2”); 模型。添加列(“col3”); 模型。添加列(“col4”); 模型。添加列(“col5”); 模型。添加列(“col6”); JScrollPane scrollpane=新的JScrollPane(m_表); 滚动窗格。背景(颜色。白色); 维度d=m_table.getPreferredSize(); scrollpane.setPreferredSize( 新维度(d.width,m_table.getRowHeight()*15+1)); 添加(滚动窗格); } attrCode=service.getAllAttribute(值); combo1=新的JComboBox(model2); model1.addElement(attrCode.get(0)); model1.addElement(attrCode.get(1)); 模型1.addElement(attrCode.get(2)); 模型1.addElement(attrCode.get(3)); 模型1.addElement(attrCode.get(4)); 模型1.addElement(attrCode.get(5)); 模型1.addElement(attrCode.get(6)); col=m_table.getColumnModel().getColumn(0); col.setCellEditor((新的DefaultCellEditor(combo1)); combo2=新的JComboBox(型号3); 模型3.addElement(trans.get(0)); 模型3.addElement(trans.get(1)); 模型3.addElement(trans.get(2)); 模型3.addElement(trans.get(3)); 模型3.addElement(trans.get(4)); col=m_table.getColumnModel().getColumn(2); col.setCellEditor((新的DefaultCellEditor(combo2));},swing,jtable,jcombobox,Swing,Jtable,Jcombobox,我有一个表,表中有一些列。两列有组合框现在我想做的是,当用户从column1 combobox中选择一些值时,根据用户选择的值column2 combobox应该被填充。 例如,如果用户从column1组合框中选择value1,则column2组合框将仅显示与value1对应的值。渲染两列 public class TablePanel extends JPanel implements ActionListener,Serializable { JTable m_table;

我有一个表,表中有一些列。两列有组合框现在我想做的是,当用户从column1 combobox中选择一些值时,根据用户选择的值column2 combobox应该被填充。
例如,如果用户从column1组合框中选择value1,则column2组合框将仅显示与value1对应的值。

渲染两列

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)));} }
//这适用于第二列,取决于第一列的选择

TableColumn comboCol1 = table.getColumnModel().getColumn(0);
TableColumn comboCol2 = table.getColumnModel().getColumn(1);
comboCol1.setCellEditor(new CustomComboBoxEditor());
comboCol2.setCellEditor(new CustomComboBoxEditor());
公共类CustomComboBoxEditor扩展了DefaultCellEditor{
//声明用于将元素添加到“ComboBox”的模型`
私有模型;
公共CustomComboxBoxeditor(){
super(新JComboBox());
this.model=(DefaultComboxModel)((JComboBox)getComponent()).getModel();
}
@凌驾
公共组件getTableCellEditorComponent(JTable表、对象值、布尔isSelected、int行、int列){
如果(列==0){
//只需显示JComboBox中的元素。
}否则{
//每次删除以前的元素。
//因此,我们可以根据选择填充元素。
model.removeAllElements();
//getValueAt(..)方法将为您提供为第1列设置的选择。
字符串selectedItem=table.getValueAt(行,0);
//使用从第一列JComboBox中获得的选定项
//选择进行调用并获取元素列表。
//假设我们有我们打电话的数据列表。
//因此,循环浏览列表并将它们添加到模型中,如下所示。
对于(int i=0;i
您没有共享您尝试过的任何代码。很好,请接受您的另一个问题答案:-)谢谢您的回答。:)您的意思是说应该有一个像TableColumn comboCol1这样的渲染器;这用于表的两行,如comboCol1=table.getColumnModel().getColumn(1);comboCol1=table.getColumnModel().getColumn(0);很抱歉我的疑问是在我的代码中我已经定义了一个单元格渲染器tablecol;我是否需要为包含combobox的两列定义不同的单元格呈现器?不需要。一个渲染器类就足够了。这两列是否都需要使用JComboBox进行渲染..@Nidhi我已经编辑了我的代码。现在它只需要一个rederer类就可以满足您的需求。
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);
 }
}