嗨,我正在使用一个包含两列即仪器和方法的表格。单击按钮后,我将在每个单元格中添加一个带有组合框的行作为编辑器。此外,那些组合框将具有动作侦听器。就像我在选择仪器时一样,应该更改方法组合框列表。我只使用两个组合框,每次添加一行时都会实例化它。我的问题是每当我添加一个新行时,现有的行组合都会重新加载最新的值。这意味着,组合框是独一无二的,即使我以不同的方式实例化它。动态创建组合框的方法是什么,它应该有自己的值。在下面发布我的代码供您参考。
addItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
comboInstrument = new CeNComboBox();
comboMethod = new CeNComboBox();
TableColumn instrumentColumn = table.getColumn("Instrument Used");
TableColumn methodColumn = table.getColumn("Method Title");
comboInstrument.removeAllItems();
listInstruments = analyticalUtil.getInstruments(listAnalysisSummaryPrefs);
iterateInstruments = listInstruments.iterator();
while(iterateInstruments.hasNext()){
comboInstrument.addItem(iterateInstruments.next());
}
dtm.addRow(new Object[]{" "," "});
comboInstrument.setEditable(true);
instrumentColumn.setCellEditor(new MyComboBoxEditor(comboInstrument));
instrumentColumn.setCellRenderer(new MyComboBoxRenderer());
comboMethod.setEditable(true);
methodColumn.setCellEditor(new MyComboBoxEditor(comboMethod));
methodColumn.setCellRenderer(new MyComboBoxRenderer());
comboInstrument.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent argEvent){
comboInstrumentActionPerformed();
}
});
comboMethod.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent argEvent){
comboMethodActionPerformed();
}
});
}
});
MyComboBoxEditor.java
public class MyComboBoxEditor extends DefaultCellEditor {
public MyComboBoxEditor(JComboBox combobox) {
super(combobox);
}
}
我对 Swing 很陌生。请帮帮我。
添加一些带有硬编码值的示例代码。如果你运行它,你就会明白我的问题。以这种方式进行测试。1. 从第一列、第一行中选择一个值,然后从另一列中选择一个值。2. 在第二行做同样的事情,现在检查第一行的第二列。所有值都将根据为第二行所做的选择重新加载。这是我面临的问题。以下代码从以下链接 JComboBox Action listener复制和编辑
private static final long serialVersionUID = 1L;
private JComboBox mainComboBox;
private JComboBox subComboBox;
private Hashtable<Object, Object> subItems = new Hashtable<Object, Object>();
public Testing() {
String[] items = {"","Select Item", "Color", "Shape", "Fruit", "Size"};
mainComboBox = new JComboBox(items);
mainComboBox.addActionListener(this);
mainComboBox.addItemListener(this);
mainComboBox.setEditable(true);
//prevent action events from being fired when the up/down arrow keys are used
//mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
// getContentPane().add(mainComboBox, BorderLayout.WEST);
subComboBox = new JComboBox();// Create sub combo box with multiple models
subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4
subComboBox.addItemListener(this);
// getContentPane().add(subComboBox, BorderLayout.CENTER);
String[] subItems1 = {"Select Color", "Red", "Blue", "Green"};
subItems.put(items[1], subItems1);
String[] subItems2 = {"Select Shape", "Circle", "Square", "Triangle"};
subItems.put(items[2], subItems2);
String[] subItems3 = {"Select Fruit", "Apple", "Orange", "Banana"};
subItems.put(items[3], subItems3);
String[] subItems4 = {"Select Size", "Big", "Middle", "Small"};
subItems.put(items[4], subItems4);
DefaultTableModel model = new DefaultTableModel(new String[]{"Instrument Used","Method Title"},0);
JTable table = new JTable(model);
table.getColumn("Instrument Used").setCellEditor(new MyComboBoxEditor(mainComboBox));
table.getColumn("Instrument Used").setCellRenderer(new MyComboBoxRenderer());
table.getColumn("Method Title").setCellEditor(new MyComboBoxEditor(subComboBox));
table.getColumn("Method Title").setCellRenderer(new MyComboBoxRenderer());
model.addRow(new String[]{""});
model.addRow(new String[]{""});
getContentPane().add(table, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
String item = (String) mainComboBox.getSelectedItem();
JOptionPane.showMessageDialog(null, "Action Performed "+item);
Object o = subItems.get(item);
if (o == null) {
subComboBox.setModel(new DefaultComboBoxModel());
} else {
subComboBox.setModel(new DefaultComboBoxModel((String[]) o));
}
}
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
if (e.getSource() == mainComboBox) {
if (mainComboBox.getSelectedIndex() != 0) {
FirstDialog firstDialog = new FirstDialog(Testing.this,
mainComboBox.getSelectedItem().toString(), "Please wait, Searching for ..... ");
}
}
}
}
private class FirstDialog extends JDialog {
private static final long serialVersionUID = 1L;
FirstDialog(final Frame parent, String winTitle, String msgString) {
super(parent, winTitle);
//setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
JLabel myLabel = new JLabel(msgString);
JButton bNext = new JButton("Stop Processes");
add(myLabel, BorderLayout.CENTER);
add(bNext, BorderLayout.SOUTH);
bNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
setVisible(false);
}
});
javax.swing.Timer t = new javax.swing.Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
t.setRepeats(false);
t.start();
setLocationRelativeTo(parent);
setSize(new Dimension(400, 100));
setVisible(true);
}
}
public static void main(String[] args) {
JFrame frame = new Testing();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}