Results 1 to 8 of 8
- 03-31-2012, 06:17 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 18
- Rep Power
- 0
JDialogBox appears more than once and seems to be incrementing
I'm trying to check the input of the user if it is present in dictionary.txt and I have this problem.
Everytime I try to click the button where I place the handler, it keeps on incrementing. I don't understand what is happening. The first I clicked the button the dialog box won't appear, the second try, it will appear once then the third it will appear twice and so on. I think there is something wrong with my condition and my loop.
Java Code:public void compareDictionary(String input){ try { bufferedReader = new BufferedReader(new FileReader("dictionary.txt")); String line = null; check = 0; while ((line = bufferedReader.readLine()) != null) { if(input.equals(line)){ System.out.println("Match found!"); setCheck(1); break; } else if(input == ""){ //doesn't go here..i don't know why setCheck(2); System.out.println("No input"); //Grid.sbut.addActionListener(handler1); break; } else if(!(input.equals(line))){ setCheck(0);break; } } System.out.println("Check: "+ check ); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } finally { try { if (bufferedReader != null) bufferedReader.close(); } catch (IOException ex) { ex.printStackTrace(); } } getCheck = check; System.out.println("Check:!! " + getCheck); showMessage(); } public void showMessage(){ if(getCheck == 0){ Grid.sbut.addActionListener(handler2); } if(getCheck == 1){ Grid.sbut.addActionListener(handler); } if(getCheck == 2){ Grid.sbut.addActionListener(handler1); } } public int getCheck(int check){ return check; } public void setCheck(int check) { this.check = check; }
- 03-31-2012, 07:36 AM #2
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
Re: JDialogBox appears more than once and seems to be incrementing
Is Dialog box inside your handlers (handler, handler1, handler2)? Everytime your showMessage() is called an actionListener is added to Grid.sbut. When the button gets pushed every ActionListener you have added will be triggered. So if you have 10 ActionListeners they will all be fired.
- 03-31-2012, 07:47 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 18
- Rep Power
- 0
Re: JDialogBox appears more than once and seems to be incrementing
yes I placed it inside
Java Code:private class HandleClass implements ActionListener{ public void actionPerformed(ActionEvent e){ JOptionPane.showMessageDialog(new Grid(),"Match Found!"); } }
- 03-31-2012, 07:49 AM #4
Member
- Join Date
- Mar 2012
- Posts
- 18
- Rep Power
- 0
Re: JDialogBox appears more than once and seems to be incrementing
ohh i got it!!
thanks for the hint :D
- 03-31-2012, 07:53 AM #5
Member
- Join Date
- Mar 2012
- Posts
- 18
- Rep Power
- 0
Re: JDialogBox appears more than once and seems to be incrementing
what should I put in the condition if the input of the user is null?
i tried using this code but it doesn't work
Java Code:else if(input == ""){ //doesn't go here..i don't know why setCheck(2); System.out.println("No input"); //Grid.sbut.addActionListener(handler1); break; }
- 03-31-2012, 08:12 AM #6
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
Re: JDialogBox appears more than once and seems to be incrementing
Strings should be compared with the equals() method, which you have done in your other if statement conditions. (input == "") is comparing object references.
- 03-31-2012, 09:21 AM #7
Member
- Join Date
- Mar 2012
- Posts
- 18
- Rep Power
- 0
Re: JDialogBox appears more than once and seems to be incrementing
I tried making it like this input.equals(null), but it still doesn't checks.
- 03-31-2012, 01:05 PM #8
Member
- Join Date
- Mar 2012
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
Combo Box defining what appears in a Jlist
By Embercloud in forum New To JavaReplies: 0Last Post: 01-04-2011, 12:29 AM -
GUI appears grey until resize randomly. Why?
By AcousticBruce in forum New To JavaReplies: 3Last Post: 12-13-2010, 05:54 PM -
Math tutor, no errors, nothing appears when run!
By Reiyn in forum New To JavaReplies: 6Last Post: 11-11-2010, 10:28 AM -
Eclipse appears half a second and disappears Vista IE8
By ortollj in forum EclipseReplies: 11Last Post: 10-27-2009, 07:55 AM -
How to make the menu bar objects appears from right to left
By JavaBean in forum AWT / SwingReplies: 3Last Post: 07-19-2007, 11:12 AM
Bookmarks