0
The selected item for both JComboxes does not change when the selected item is changed
Pls help. When running the program when the okButton1 is clicked, the system should run the if statement checking the selected index. However the selected index is always 0. How do I get this to change when i change the comboBox element and click okButton1.
6 Antworten
+ 1
Hello Stephen
Can you please show me your code?
+ 1
thanks 👍
0
Hello Stephen
Inside your startButton.addActionListener(){
}
You can remove the if statements to get playerNumber and numberOfHoles.
a) just set the start values per hand:
numberOfHoles = 3;
numberofPlayers = 1;
b)
numberOfHoles = Integer.valueOf(holeNumber.getSelectedItem().toString());
same for playerNumber
I would prefer a)
Selecting an item of a combobox is? Yes, an ActionEvent. So you need ActionListener.
holeNumber.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
String item = holeNumber.getSelectedItem().toString();
numberOfHoles = Integer.valueOf(item);
}
});
When ever you click on the combobox the value changes now.
Same for numberOfPlayerBox. When you click ok you can print the values to see that it works.
Hope this helps you :)
Btw: For modern GUI I suggest you JavaFX.
0
thanks 👍
0
thanks 👍