它没有改变相应的组合框值,为什么它改变的是最新的组合框值,而不是相应的组合框?
当我运行此代码时,单击添加字段两次后,当我更改第一个组合框时,第二个组合框值应该更改而不是第四个组合框,这是错误的,我想要的是取决于第一个组合框选择第二个组合框应该是可选的,如果选择了第三个组合框,那么第四个组合框应该是可选的。
添加字段按钮在窗口中添加额外的组合框,它应该工作的方式是,当添加字段按钮按两次时,我分别选择第一个组合框,第二个组合框应该是可选的,但由于某种原因,第四个组合框变得可选。当你运行上面的代码时,你会更好地理解问题,我在描述中提供了完整的代码,请帮助!
package addextraitem;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ExtraComboBox {
int count = 0;
JComboBox fruits[] = new JComboBox[10];;
JPanel comboPanel;
JFrame guiFrame;
//JComboBox dude1;
String[] valOptions3 = {"&"};
String[] valOptions2 = {"|->", "|=>"};
String[] valOptions1 = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
String[] valOptions0 = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
String[] fruitOptions1 = {"", "Delay1", "Delay2", "Delay3"};
String chosenString;
int numPairs1 = valOptions0.length;
JButton addField;
int count1 = 0;
JComboBox[] ComboBox4 = new JComboBox[numPairs1];
JComboBox[] ComboBox5 = new JComboBox[numPairs1];
JComboBox[] ComboBox6 = new JComboBox[numPairs1];
String[] comborel = {"a", "b", "c", "d", "e"};
JLabel lb;
JComboBox dude;
JTextField handle;
String clockOptions1;
JLabel dudel[] = new JLabel[10];
JComboBox dude1[] = new JComboBox[10];
JComboBox dude2[] = new JComboBox[10];;
//JComboBox dude1[] = new JComboBox[10];
String[] valOptions = {"Unknown", "0", "1"};
String [] s = {"a", "b", "c", "d", "e", "f", "g", "h", "i"};
int cnt = 0;
int i = 0;
JLabel comboLbl;
JLabel lb1;
//Note: Typically the main method will be in a
//separate class. As this is a simple one class
//example it's all in the one class.
public static void main(String[] args) {
new ExtraComboBox();
}
public ExtraComboBox()
{
guiFrame = new JFrame();
//make sure the program exits when the frame closes
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("ComboBox GUI");
guiFrame.setSize(350,250);
//This will center the JFrame in the middle of the screen
guiFrame.setLocationRelativeTo(null);
//The first JPanel contains a JLabel and JCombobox
comboPanel = new JPanel();
addField = new JButton("Add Field");
addField.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == addField) {
count1++;
comboLbl = new JLabel("Select a relation:");
fruits[count1] = new JComboBox(fruitOptions1);
MyItemListener2 actionListener2 = new MyItemListener2();
fruits[count1].addItemListener(actionListener2);
//System.out.println("HI: " + fruits[count1].getParent());
dude2[count1] = new JComboBox();
System.out.println("ADD FIELDS: " + count1);
comboPanel.add(comboLbl);
comboPanel.add(fruits[count1]);
comboPanel.add(dude2[count1]);
guiFrame.revalidate();
guiFrame.validate();
guiFrame.pack();
guiFrame.repaint();
}
}
});
comboPanel.setLayout(new BoxLayout(comboPanel, BoxLayout.Y_AXIS));
comboPanel.add(addField);
//The JFrame uses the BorderLayout layout manager.
//Put the two JPanels and JButton in different areas.
guiFrame.add(comboPanel, BorderLayout.NORTH);
//make sure the JFrame is visible
guiFrame.setVisible(true);
}
class MyItemListener2 implements ItemListener {
// This method is called only if a new item has been selected.
public void itemStateChanged(ItemEvent evt) {
JComboBox cb = (JComboBox) evt.getSource();
Object item = evt.getItem();
if (evt.getStateChange() == ItemEvent.SELECTED) {
// Item was just selected
System.out.println("COUNTER: " + count1);
System.out.println(evt.getItem());
dude2[count1].removeAllItems();
if(evt.getItem() == "Delay1") {
System.out.println(valOptions1.length);
for(int i = 0; i < valOptions1.length; i++) {
dude2[count1].addItem(valOptions1[i]); //dude1 = new JComboBox(valOptions1);
System.out.println(valOptions1[i]);
}
}
else if(evt.getItem() == "Delay2") {
System.out.println(valOptions2.length);
for(int j = 0; j < valOptions2.length; j++) {
System.out.println(valOptions2[j]);
dude2[count1].addItem(valOptions2[j]); //dude1 = new JComboBox(valOptions1);
}
}
else if(evt.getItem() == "Delay3") {
System.out.println(valOptions3.length);
for(int j = 0; j < valOptions3.length; j++) {
System.out.println(valOptions3[j]);
dude2[count1].addItem(valOptions3[j]); //dude1 = new JComboBox(valOptions1);
}
}
} else if (evt.getStateChange() == ItemEvent.DESELECTED) {
// Item is no longer selected
}
}
}
}
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ExtraComboBox {
private int maxFields = 4; // The max number of fields allowed in the dialog
JComboBox fruits[] = new JComboBox[maxFields];
JPanel comboPanel;
JFrame guiFrame;
String[] valOptions3 = { "&" };
String[] valOptions2 = { "|->", "|=>" };
String[] valOptions1 = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
String[] valOptions0 = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
String[] fruitOptions1 = { "", "Delay1", "Delay2", "Delay3" };
JButton addField;
int count1 = 0;
JLabel dudel[] = new JLabel[maxFields];
JComboBox dude2[] = new JComboBox[maxFields];
String[] valOptions = { "Unknown", "0", "1" };
String[] s = { "a", "b", "c", "d", "e", "f", "g", "h", "i" };
private JLabel comboLbl;
public static void main(String[] args) {
new ExtraComboBox();
}
public ExtraComboBox() {
guiFrame = new JFrame();
// make sure the program exits when the frame closes
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("ComboBox GUI");
guiFrame.setSize(350, 350);
// The first JPanel contains a JLabel and JCombobox
comboPanel = new JPanel();
addField = new JButton("Add Field");
addField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if (event.getSource().equals(addField)) {
if (count1 < maxFields) {
comboLbl = new JLabel("Select a relation:");
fruits[count1] = new JComboBox<String>(fruitOptions1);
MyItemListener2 actionListener2 = new MyItemListener2(count1);
fruits[count1].addItemListener(actionListener2);
// System.out.println("HI: " + fruits[count1].getParent());
dude2[count1] = new JComboBox<String>();
System.out.println("ADD FIELDS: " + count1);
comboPanel.add(comboLbl);
comboPanel.add(fruits[count1]);
comboPanel.add(dude2[count1]);
guiFrame.validate();
guiFrame.repaint();
count1++;
} else {
System.out.println("You reached the maximum of 4 fields.");
}
}
}
});
comboPanel.setLayout(new BoxLayout(comboPanel, BoxLayout.Y_AXIS));
comboPanel.add(addField);
// The JFrame uses the BorderLayout layout manager.
// Put the two JPanels and JButton in different areas.
guiFrame.add(comboPanel, BorderLayout.NORTH);
// make sure the JFrame is visible
guiFrame.setVisible(true);
}
class MyItemListener2 implements ItemListener {
private int index;
public MyItemListener2(int pIndex) {
super();
index = pIndex;
}
// This method is called only if a new item has been selected.
public void itemStateChanged(ItemEvent evt) {
if (evt.getStateChange() == ItemEvent.SELECTED) {
// Item was just selected
System.out.println("COUNTER: " + index);
System.out.println(evt.getItem());
dude2[index].removeAllItems();
switch ((String) evt.getItem()) {
case "Delay1":
for (int i = 0; i < valOptions1.length; i++) {
dude2[index].addItem(valOptions1[i]); // dude1 = new JComboBox(valOptions1);
System.out.println(valOptions1[i]);
}
break;
case "Delay2":
for (int j = 0; j < valOptions2.length; j++) {
System.out.println(valOptions2[j]);
dude2[index].addItem(valOptions2[j]); // dude1 = new JComboBox(valOptions1);
}
break;
case "Delay3":
for (int j = 0; j < valOptions3.length; j++) {
System.out.println(valOptions3[j]);
dude2[index].addItem(valOptions3[j]); // dude1 = new JComboBox(valOptions1);
}
}
}
}
}
}
我想这解决了你的要求。我删除了所有未使用的代码,但保留了所有变量名。请以任何阅读代码的人都知道变量的用途的方式选择变量的名称。
很明显,您不了解java或任何其他高级编程语言。我建议您在尝试编程花哨的东西之前先学习基础知识。
我希望这对你有帮助
我有两个组合框。我正在填充这样的两个组合框。组合框1和2的名称是cmbpartyName和cmbprefPT2。 在这里,我从combox1中选择一个值,在它所选的索引更改事件中,如下所示。 由于两个组合框具有相同的值,如何从第二个组合框cmbPrefPT2中删除第一个组合框的选定值?
我的表单上有2个组合框(在表单加载事件中)。第一个组合框在表单加载后从选择语句中获取一个值。我想在我的第二个组合框中使用该值。这是我的代码: 1st组合框= cbDelivery 2de组合框= cbOrderNo 此组合框位于: private void CB delivery _ selectedindex changed(对象发送方,事件参数e) 因此,一旦我从1ste组合框中选择一个值,我
我试图通过FXML文件将项目添加到组合框中,但出现了这个错误。 这是我的FXML 主要 当我通过scene builder打开它时,会出现这个错误 我已经仔细检查过了,但代码似乎没问题。当我删除试图添加到FXML的值时,错误消失了,但我的组合框变为空。它说String不是有效的类型。我怎样才能解决这个问题?为什么它是无效的?我在这里检查了类似的问题,但是错误是由于没有导入FXCollections
问题内容: 我需要一些帮助,将一些项目添加到中。因此,我有两个组合框,其中一个根据所选项目填充另一个。 我的问题是,用于新项目,它可以工作,但是如果我为组合框选择另一个选项,它将添加新项目,但先前的项目不见了-新项目下面有空白项目。 我以为每次我从第一个组合框选择一个新选项来清除第二个组合框的内容。所以我在第二个上使用了-但没有用。 我就是这样想的: 上面是第一次更改时执行的功能的一部分。 问题答
我们试图使用以下源代码在组合框中显示树状结构https://fiddle.sencha.com/#fiddle/g50 我们可以在组合框中查看树结构,但是当我们选择任何节点时,它不会显示在组合框中。
几个小时寻找答案后,我终于放弃了。我有一个包含以下组合框的FXML表单: 它注入到 JavaFX 控制器类中: 该组合显示了使用Eclipselink 2.7和JPA 2.2从嵌入式H2数据库加载的Tobra(在西班牙语中代表Tipo de Obra)列表。 我不会向用户展示Tobra的价值。toString,相反,我在初始化中设置了一个转换器: 我有一个实现<code>Task的内部类 当然,当