In my code if the user enters value in ComboBox 2 it reflects in TextField, but if the user selects from the dropdown in Combobox1 without changing the value in ComboBox 2 the value remains the same in Textfield it does not changes.want am i doing wrong?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import javax.swing.text.Document;
public class D extends JPanel {
public D() {
JPanel buttonPanel = new JPanel();
add(buttonPanel);
buttonPanel.setLayout(new GridLayout(0, 3, 5, 5));
JTextField field11 = new JTextField(15);
field11.setEditable(false);
buttonPanel.add(field11);
JTextField field12 = new JTextField(15);
field12.setEditable(false);
buttonPanel.add(field12);
JComboBox comboBox1 = new JComboBox();
comboBox1.addItem("1");
comboBox1.addItem("2");
comboBox1.addItem("3");
JComboBox comboBox2 = new JComboBox();
comboBox2.setEditable(true);
comboBox1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JComboBox comboBox1 = (JComboBox) event.getSource();
JComboBox comboBox2 = (JComboBox) event.getSource();
Object selected = comboBox1.getSelectedItem();
if (selected.toString().equals("1")) {
field11.setText("10");
String cb3a = ((String) comboBox2.getSelectedItem());
double cb3b = Double.valueOf(cb3a);
String cb3aa = field11.getText();
double cb3bb = Double.parseDouble(cb3aa);
double cb3c = (cb3b * cb3bb);
String cb3d = String.valueOf(cb3c);
field12.setText(cb3d);
}
else if (selected.toString().equals("2")) {
field11.setText("20");
String cb33a = ((String) comboBox2.getSelectedItem());
double cb33b = Double.valueOf(cb33a);
String cb33aa = field11.getText();
double cb33bb = Double.parseDouble(cb33aa);
double cb33c = (cb33b * cb33bb);
String cb33d = String.valueOf(cb33c);
field12.setText(cb33d);
}
else if (selected.toString().equals("3")) {
field11.setText("30");
String cb333a = ((String) comboBox2.getSelectedItem());
double cb333b = Double.valueOf(cb333a);
String cb333aa = field11.getText();
double cb333bb = Double.parseDouble(cb333aa);
double cb333c = (cb333b * cb333bb);
String cb333d = String.valueOf(cb333c);
field12.setText(cb333d);
}
}
});
comboBox2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JComboBox comboBox2 = ((JComboBox) event.getSource());
Object selected = comboBox1.getSelectedItem();
if (selected.toString().equals("1")) {
String cb3a = ((String) comboBox2.getSelectedItem());
double cb3b = Double.valueOf(cb3a);
String cb3aa = field11.getText();
double cb3bb = Double.parseDouble(cb3aa);
double cb3c = (cb3b * cb3bb);
String cb3d = String.valueOf(cb3c);
field12.setText(cb3d);
} else if (selected.toString().equals("2")) {
String cb33a = ((String) comboBox2.getSelectedItem());
double cb33b = Double.valueOf(cb33a);
String cb33aa = field11.getText();
double cb33bb = Double.parseDouble(cb33aa);
double cb33c = (cb33b * cb33bb);
String cb33d = String.valueOf(cb33c);
field12.setText(cb33d);
} else if (selected.toString().equals("3")) {
String cb333a = ((String) comboBox2.getSelectedItem());
double cb333b = Double.valueOf(cb333a);
String cb333aa = field11.getText();
double cb333bb = Double.parseDouble(cb333aa);
double cb333c = (cb333b * cb333bb);
String cb333d = String.valueOf(cb333c);
field12.setText(cb333d);
}
}
});
buttonPanel.add(comboBox1);
buttonPanel.add(comboBox2);
try {
InputStream ips = new FileInputStream("test2.txt");
InputStreamReader ipsr = new InputStreamReader(ips);
BufferedReader br = new BufferedReader(ipsr);
String line;
while ((line = br.readLine()) != null) {
comboBox1.setSelectedItem(line);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
D app = new D();
JFrame m = new JFrame("D");
m.getContentPane().add(app);
m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
m.pack();
m.setVisible(true);
}
}
test2.txt: 1 Any help would be appreciated!
You are only setting field11 in comboBox1's ActionListener, there is no reason for field12 to change. Only the respective ActionListener will get executed when a JComboBox is changed, not both.
Put your calculation in a separate method and then set both fields in the ActionListener, so both of them change.
Apart from that, you should add a type parameter to JComboBox, eg JComboBox<Integer>
. That way, you will have to cast less. The GUI should be initialized on the EDT.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.