gpt4 book ai didi

swing - 为 jtable 中的列的每一行添加不同的组合框

转载 作者:行者123 更新时间:2023-12-02 05:21:57 25 4
gpt4 key购买 nike

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 对应的值。

最佳答案

渲染两列。

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);
}
}

关于swing - 为 jtable 中的列的每一行添加不同的组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13699467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com