[英]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.