Reputation: 47
I am trying to make a ui in which i read file from text file to JComboBoxes and display the results in JTextFields.Only the first combobox is getting updated from the file while the rest is not getting updated from the file.
Here is my Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class Main1 extends JPanel
{
public Main1() {
JPanel buttonPanel = new JPanel();
add(buttonPanel);
buttonPanel.setLayout(new GridLayout(0, 4, 5, 5));
JTextField field1 = new JTextField(5);
field1.setEditable(false);
buttonPanel.add(field1);
JTextField field2 = new JTextField(5);
field2.setEditable(false);
buttonPanel.add(field2);
JTextField field3 = new JTextField(5);
field3.setEditable(false);
buttonPanel.add(field3);
JTextField field4 = new JTextField(5);
field4.setEditable(false);
buttonPanel.add(field4);
JComboBox comboBox = new JComboBox();
comboBox.setEditable(true);
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JComboBox comboBox = (JComboBox) event.getSource();
field1.setText((String) comboBox.getSelectedItem());
}
});
JComboBox comboBox1 = new JComboBox();
comboBox1.addItem("1");
comboBox1.addItem("2");
comboBox1.addItem("4");
comboBox1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JComboBox comboBox1 = (JComboBox) event.getSource();
Object selected = comboBox1.getSelectedItem();
if(selected.toString().equals("1"))
field2.setText("3");
else if(selected.toString().equals("2"))
field2.setText("6");
else if(selected.toString().equals("4"))
field2.setText("12");
}
});
JComboBox comboBox2 = new JComboBox();
comboBox2.addItem("1");
comboBox2.addItem("2");
comboBox2.addItem("4");
comboBox2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JComboBox comboBox2 = (JComboBox) event.getSource();
Object selected = comboBox2.getSelectedItem();
if(selected.toString().equals("1"))
field3.setText("1");
else if(selected.toString().equals("2"))
field3.setText("2");
else if(selected.toString().equals("4"))
field3.setText("4");
}
});
JComboBox comboBox3 = new JComboBox();
comboBox3.addItem("1");
comboBox3.addItem("2");
comboBox3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JComboBox comboBox3 = (JComboBox) event.getSource();
Object selected = comboBox3.getSelectedItem();
if(selected.toString().equals("1"))
field4.setText("1");
else if(selected.toString().equals("2"))
field4.setText("2");
}
});
buttonPanel.add(comboBox);
buttonPanel.add(comboBox1);
buttonPanel.add(comboBox2);
buttonPanel.add(comboBox3);
try{
InputStream ips=new FileInputStream("test.txt");
InputStreamReader ipsr=new InputStreamReader(ips);
BufferedReader br=new BufferedReader(ipsr);
String line;
while ((line=br.readLine())!=null) {
String[] s = line.split(" ");
comboBox.setSelectedItem(s[0]);
comboBox1.setSelectedItem(s[1]);
comboBox2.setSelectedItem(s[2]);
comboBox3.setSelectedItem(s[3]);
}
br.close();
}
catch (Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
Main1 a = new Main1();
JFrame f = new JFrame();
f.getContentPane().add(a);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}
Text File:
14 54 89 56
Upvotes: 0
Views: 134
Reputation: 159864
None of the values in file list are contained within the combo box list so none appear except for the first one. This happens because this is the only one that is editable. From the docs
If anObject is not in the list and the combo box is uneditable, it will not change the current selection
Simply add all expected items to the combo boxes models on application startup.
Upvotes: 1
Reputation: 4948
is this cos u have given " " instead of "\n" in the stream reader ? i mean in the Split function
Upvotes: 0