简体   繁体   English

如何在Java中将2个单独的组合框中的选定项放入第3个组合框中,并同时更改itemstate?

[英]How to get selected item from 2 separate comboboxes into 3rd combobox and also with itemstatechanged, in java?

I have created two comboboxes in netbeans and inserted some items into first combobox and as well into second combobox and when I want to select an item in first and second , both selected values must appear in third combobox . 我在netbeans中创建了两个组合框,并将一些项目插入第一个组合框以及第二个组合框,并且当我想在第一个和第二个组合框中选择一个项目时,两个选定的值都必须出现在第三个组合框中。 and if I want to change the selection in first and second also it should make the same selection in third combobox,i,e,. 如果我想更改第一和第二选择,也应该在第三组合框中进行相同的选择。 the item status changed . 项目状态已更改。

thanks in advance and please help me, any help will be appreciated. 在此先感谢您,请帮助我,任何帮助将不胜感激。

Start by taking a look at How to Use Combo Boxes and How to Write an Action Listeners for more details... 首先查看如何使用组合框如何编写操作侦听器以了解更多详细信息...

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Combos {

    public static void main(String[] args) {
        new Combos();
    }

    public Combos() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                String[] numbers = new String[10];
                for (int index = 0; index < numbers.length; index++) {
                    numbers[index] = String.valueOf(index);
                }
                String[] letters = new String[10];
                for (int index = 0; index < letters.length; index++) {
                    letters[index] = Character.toString((char)(index + 65));
                }

                JComboBox cb1 = new JComboBox(numbers);
                JComboBox cb2 = new JComboBox(letters);

                String[] everything = new String[numbers.length + letters.length];
                System.arraycopy(numbers, 0, everything, 0, numbers.length);
                System.arraycopy(letters, 0, everything, numbers.length, letters.length);

                JComboBox cb3 = new JComboBox(everything);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                frame.add(cb1, gbc);
                gbc.gridx++;
                frame.add(cb2, gbc);

                gbc.gridx = 0;
                gbc.gridy++;
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                gbc.fill = GridBagConstraints.HORIZONTAL;
                frame.add(cb3, gbc);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                cb1.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        cb3.setSelectedItem(cb1.getSelectedItem());
                    }
                });
                cb2.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        cb3.setSelectedItem(cb2.getSelectedItem());
                    }
                });
            }
        });
    }

}

Updated 更新

Okay, so basically, for the third JComboBox , you need to combine the contents of the other two into a single unified model, for example... 好的,基本上,对于第三个JComboBox ,您需要将其他两个的内容合并为一个统一的模型,例如...

String combined[] = new String[numbers.length * letters.length];
int index = 0;
for (int outter = 0; outter < numbers.length; outter++) {
    for (int inner = 0; inner < numbers.length; inner++) {
        combined[index] = numbers[outter] + " " + letters[inner];
        index++;
    }
}

Then, when either of the first two change, you can combine the results and use that as the selected item in the third... 然后,当前两个更改中的任何一个更改时,您可以合并结果并将其用作第三个更改中的选定项目...

protected void updateCombinedView() {
    String value = cb1.getSelectedItem() + " " + cb2.getSelectedItem();
    cb3.setSelectedItem(value);
}

I still have no idea why, but there you are... 我仍然不知道为什么,但是你在那里...

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Combos {

    public static void main(String[] args) {
        new Combos();
    }

    private JComboBox<String> cb1;
    private JComboBox<String> cb2;
    private JComboBox<String> cb3;

    public Combos() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                String[] numbers = new String[10];
                for (int index = 0; index < numbers.length; index++) {
                    numbers[index] = String.valueOf(index);
                }
                String[] letters = new String[10];
                for (int index = 0; index < letters.length; index++) {
                    letters[index] = Character.toString((char) (index + 65));
                }

                String combined[] = new String[numbers.length * letters.length];
                int index = 0;
                for (int outter = 0; outter < numbers.length; outter++) {
                    for (int inner = 0; inner < numbers.length; inner++) {
                        combined[index] = numbers[outter] + " " + letters[inner];
                        index++;
                    }
                }

                cb1 = new JComboBox(numbers);
                cb2 = new JComboBox(letters);

                cb3 = new JComboBox(combined);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                frame.add(cb1, gbc);
                gbc.gridx++;
                frame.add(cb2, gbc);

                gbc.gridx = 0;
                gbc.gridy++;
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                gbc.fill = GridBagConstraints.HORIZONTAL;
                frame.add(cb3, gbc);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                cb1.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        updateCombinedView();
                    }
                });
                cb2.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        updateCombinedView();
                    }
                });
            }
        });
    }

    protected void updateCombinedView() {
        String value = cb1.getSelectedItem() + " " + cb2.getSelectedItem();
        cb3.setSelectedItem(value);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们如何才能在Java swing中动态地将2个组合框选择的项目动态地放入第3个组合框? - How can we get 2 combobox selected items only into 3rd combobox dynamically in java swings? Java Swing Combobox removeAllItems调用ItemStateChanged也? - Java Swing Combobox removeAllItems calling ItemStateChanged also? 从2个组合框中选择项目时,如何动态链接到Java Swing NetBeans中的第3个组合框中? - how to make when selecting items from 2 comboboxes dyanamically link to 3 rd combobox in java swings netbeans? 如何在Java中从一个组合框到另一个组合框获取选定项目的相关数据 - How to get related data of selected item from one ComboBox to another ComboBox in java 如何从Java中的另一个类调用combobox所选项目? - how to call combobox selected item from another class in java? 如何从 JavaFX 中的 ComboBox 获取所选项目的数量? - How to get number of selected item from a ComboBox in JavaFX? JavaFX-从其他组合框中删除选定的项目 - JavaFX - remove selected item from other comboboxes [JAVA]如何将方法称为“ itemStateChanged” - [JAVA]How to call the method “itemStateChanged” 如何计算 Java 中最后第三个逗号的字符串? - How to count string from last 3rd comma in Java? 当JComboBox中的itemStateChanged获得JTable的选定行 - Get selected row of JTable when itemStateChanged in JComboBox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM