用于更改组合框数据的单选按钮
Radio button to change JCombo Data
正在寻找一种方法来通过选择单选按钮来更改输出到组合框的数据,但是数据正在从文本文件中提取并保存到数组中,然后传回 JCOMBO 数组列表。抱歉,如果问题很含糊,我不确定该如何措辞。但是这些文件被两个不同的 TXT 文件分开,我可以很容易地 return 从中获取数据。
ArrayList<String> stations = Reader("Default.txt");
JComboBox<String> cb = new JComboBox<>(stations.toArray(new String[stations.size()]));
JRadioButton belgrave = new JRadioButton("Belgrave Line");
belgrave.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cb.removeAllItems();
stations.clear();
ArrayList<String> stations = Reader("Belgrave.txt");
JComboBox<String> cb = new JComboBox<>(stations.toArray(new String[stations.size()]));
}
});
JRadioButton glenwaverly = new JRadioButton("Glen Waverly Line");
glenwaverly.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cb.removeAllItems();
stations.clear();
ArrayList<String> stations = Reader("Glenwaverly.txt");
JComboBox<String> cb = new JComboBox<>(stations.toArray(new String[stations.size()]));
}
});
ButtonGroup bG = new ButtonGroup();
JButton apply = new JButton("Touch on ?");
JButton cancel = new JButton("Cancel");
与您添加到 apply
和 cancel
按钮中的动作侦听器非常相似,您也需要将动作侦听器应用于单选按钮。
然后执行如下操作。
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//suppose this is your file input, that you will have to read
String[] test = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
//your combobox name supposed it is combo
//remove all the previous items
combo.removeAllItems();
//add all the items of the array(there is no addAll method)
for(int i=0; i<test.length; i++)
combo.addItem(test[i]);
}
希望对您有所帮助。
请注意,读取TXT文件并将数据解析为数组与您的txt结构有关。查看 here 如何逐行读取 txt。
编辑
在您的侦听器中,您正在创建一个新的本地组合框。但是,侦听器内部的 cb
与侦听器外部的 cb
不同,它只是一个仅在方法内部创建和知道的变量。需要直接调用cb而不创建新对象
替换此 JComboBox<String> cb = new JComboBox<>(stations.toArray(new String[stations.size()]))
与那个
String[] items = stations.toArray(new String[stations.size()];
for(int i=0; i<items.length; i++)
cb.addItem(items[i]);
正在寻找一种方法来通过选择单选按钮来更改输出到组合框的数据,但是数据正在从文本文件中提取并保存到数组中,然后传回 JCOMBO 数组列表。抱歉,如果问题很含糊,我不确定该如何措辞。但是这些文件被两个不同的 TXT 文件分开,我可以很容易地 return 从中获取数据。
ArrayList<String> stations = Reader("Default.txt");
JComboBox<String> cb = new JComboBox<>(stations.toArray(new String[stations.size()]));
JRadioButton belgrave = new JRadioButton("Belgrave Line");
belgrave.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cb.removeAllItems();
stations.clear();
ArrayList<String> stations = Reader("Belgrave.txt");
JComboBox<String> cb = new JComboBox<>(stations.toArray(new String[stations.size()]));
}
});
JRadioButton glenwaverly = new JRadioButton("Glen Waverly Line");
glenwaverly.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cb.removeAllItems();
stations.clear();
ArrayList<String> stations = Reader("Glenwaverly.txt");
JComboBox<String> cb = new JComboBox<>(stations.toArray(new String[stations.size()]));
}
});
ButtonGroup bG = new ButtonGroup();
JButton apply = new JButton("Touch on ?");
JButton cancel = new JButton("Cancel");
与您添加到 apply
和 cancel
按钮中的动作侦听器非常相似,您也需要将动作侦听器应用于单选按钮。
然后执行如下操作。
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//suppose this is your file input, that you will have to read
String[] test = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
//your combobox name supposed it is combo
//remove all the previous items
combo.removeAllItems();
//add all the items of the array(there is no addAll method)
for(int i=0; i<test.length; i++)
combo.addItem(test[i]);
}
希望对您有所帮助。
请注意,读取TXT文件并将数据解析为数组与您的txt结构有关。查看 here 如何逐行读取 txt。
编辑
在您的侦听器中,您正在创建一个新的本地组合框。但是,侦听器内部的 cb
与侦听器外部的 cb
不同,它只是一个仅在方法内部创建和知道的变量。需要直接调用cb而不创建新对象
替换此 JComboBox<String> cb = new JComboBox<>(stations.toArray(new String[stations.size()]))
与那个
String[] items = stations.toArray(new String[stations.size()];
for(int i=0; i<items.length; i++)
cb.addItem(items[i]);