Results 1 to 8 of 8
  1. #1
    jumpgirl is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default 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;
    	}

  2. #2
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default 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.

  3. #3
    jumpgirl is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default 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!");
    		  }
    	   }
    Should I add removeActionListener function?

  4. #4
    jumpgirl is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default Re: JDialogBox appears more than once and seems to be incrementing

    ohh i got it!!
    thanks for the hint :D

  5. #5
    jumpgirl is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default 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;      
                        }

  6. #6
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default 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.

  7. #7
    jumpgirl is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default 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.

  8. #8
    jumpgirl is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default Re: JDialogBox appears more than once and seems to be incrementing

    it already works :D

Similar Threads

  1. Combo Box defining what appears in a Jlist
    By Embercloud in forum New To Java
    Replies: 0
    Last Post: 01-04-2011, 01:29 AM
  2. GUI appears grey until resize randomly. Why?
    By AcousticBruce in forum New To Java
    Replies: 3
    Last Post: 12-13-2010, 06:54 PM
  3. Math tutor, no errors, nothing appears when run!
    By Reiyn in forum New To Java
    Replies: 6
    Last Post: 11-11-2010, 11:28 AM
  4. Replies: 11
    Last Post: 10-27-2009, 08:55 AM
  5. Replies: 3
    Last Post: 07-19-2007, 12:12 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •