I’ve almost got my asignment done. Yayy! But…. I have two problems in my handler. For one, I have a public void itemStateChanged(ItemEvent e) method that is supposed to print a registrants name to a text field, plus the type of registration he is (student, business, complimentary). I have this working, only it prints twice to the text area. I don’t get that. Also, there is a ‘calculate charges’ button which is used to…well, calculate the charges. When the button is clicked, the action event is supposed to check to make sure that a combo box (in a class called regPanel) does not have “Please select a registration type” which is element 0 in an object array. The way I have it now, if I don’t pick something from the combobox (leaving it on element 0), I get the error message, but then the program prints to the textfield anyways. It is not supposed to. It is only supposed to print the error box, then allow the user to make the proper selections. Any advice would be appreciated. Here is the class:
public class ConferenceHandler implements ActionListener, FocusListener, ItemListener
{
protected final static String ERROR_TEXT = "Please enter a name";
protected ConferenceGUI gui; //reference the ConferenceGUI panel
/**Constructor*/
public ConferenceHandler(ConferenceGUI gui) {this.gui = gui;}
if (gui.regPanel.getRegType() == "Please select a type")
JOptionPane.showMessageDialog(null, "Please select a registration type",
"Type Error",JOptionPane.ERROR_MESSAGE);
//prints to textarea if registrant will be attending keynote or not
else if (gui.regPanel.regCheckBox.isSelected())
gui.textArea.append("\nKeynote address will be attended");
else
Did a little more googling, and figured out the problem with the double firing of the itemStateChanged affair. Trimmed all of the extra code out, because I’m pretty sure that all I need is a loop of some sort around here. When I put a while or do-while loop, all that happens is the JOptionPane comes up and won’t go away. I need to validate that the user has entered an appropriate checkbox selection, though.
You still haven’t posted a SSCCE that demonstrates the problem as you where asked in a previoius question.
Copying and pasting a few lines of code from your real problem does not help you isolate the problem.
As a beginner, you need to learn how to simplify problems. Once you simplify the problem it is easier to find and solve the problem.
In this case a working SSCCE will be about 20 lines of code. A couple lines to create the GUI, a few more to add the compnonents and a few more to create the ItemListener.
Post your SSCCE and I’ll post the answer to your question. This is a common mistake when working with an ItemListener. I could probably give you the answer without seeing your SSCCE, but you need to learn how to create a SSCCE for when you encounter more complex problems. So this question is a good place to start.
Maybe looking at the section from the Swing tutorial on How to Write an Item Listenerwill help you understand what is happening.