多个组合框

时间:2013-07-10 11:28:01

标签: combobox

我有两个组合框,它们必须具有相同的值。我已将它们设置为具有2个不同的ComboBoxModel,但是当我在第一个组合框中选择一些选项时,相同的选项会在第二个选项中选择。

public class AddLineFrame extends JFrame {
    private ComboBoxModel model1;
    private ComboBoxModel model2;

    import java.awt.BorderLayout;


public class AddLineFrame extends JFrame {

    private JComboBox<String> comboBox1;
    private JComboBox<String> comboBox2;
    private ComboBoxModel model1;
    private ComboBoxModel model2;

    public AddLineFrame() {
        model1 = main.bPanel.getList();
        model2 = main.bPanel.getList();

        comboBox1 = new JComboBox();
        comboBox1.setModel(model1);
        comboBox1.setFont(new Font("Candara", Font.PLAIN, 14));
        comboBox1.setBounds(114, 33, 90, 25);
        panel_1.add(comboBox1);

        comboBox2 = new JComboBox(model2);
        comboBox1.setModel(model2);
        comboBox2.setFont(new Font("Candara", Font.PLAIN, 14));
        comboBox2.setBounds(114, 63, 90, 25);
        panel_1.add(comboBox2);
    }
}

这就是我的模型制作方式

公共类BusPanel扩展了JPanel实现的Runnable {

ArrayList <Bus> busList = new ArrayList<Bus> ();
DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();


public BusPanel() {
    model.addElement(" ");
    setBorder(BorderFactory.createLineBorder(Color.black, 2));
    setPreferredSize(new Dimension(400,300));
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);       
    for (Bus bus : busList) {
        bus.paintComponent(g);
    }
}

public void addBus(int id, double voltage, String title) { 
    Bus bus = new Bus(id, voltage, title);
    busList.add(bus);
    model.addElement(bus.title);
    revalidate();
    repaint();

}

public DefaultComboBoxModel<String> getList(){
    return model;
}

}

0 个答案:

没有答案