同じ値を持つ必要がある 2 つのコンボボックスがあります。2 つの異なる ComboBoxModels を持つように設定しましたが、最初のコンボ ボックスでオプションを選択すると、2 番目のコンボ ボックスでも同じオプションが選択されます。
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);
}
}
これが私のモデルの作り方です
public class BusPanel extends JPanel implement 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;
}
}