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.

25th Apr 2020, 6:05 PM
Stephen
6 Antworten
+ 1
Hello Stephen Can you please show me your code?
25th Apr 2020, 6:20 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
thanks 👍
27th Apr 2020, 6:43 AM
Stephen
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.
27th Apr 2020, 12:26 AM
Denise Roßberg
Denise Roßberg - avatar
0
thanks 👍
27th Apr 2020, 6:45 AM
Stephen
0
thanks 👍
27th Apr 2020, 7:54 AM
Stephen