Results 1 to 15 of 15
  1. #1
    Join Date
    Jan 2010
    Posts
    72
    Rep Power
    0

    Default plz solve this error

    i am working on a project in which there is an actionListener for a button...when the user will click it, the code inside will create a JInternalFrame on which i have added a button....now the problem is how to add an actionListener for the button i.e on JInternalFrame
    my code goes like this

    Java Code:
    public void actionPerformed(ActionEvent e)
        {
            ...............
            ...............
            ...............
            JButton cancel = new JButton("Cancel");
    	        cancel.setFont(new Font("ARIAL",Font.BOLD,12));
    	        cancel.setBounds(410, 570, 75, 25);
    	        cancel.addActionListener(this);
    on running the code i am having the following error
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: illegal component position
    	at java.awt.Container.addImpl(Container.java:1048)
    	at javax.swing.JLayeredPane.addImpl(JLayeredPane.java:212)
    	at javax.swing.JDesktopPane.addImpl(JDesktopPane.java:470)
    	at java.awt.Container.add(Container.java:365)
    	at financialaccounting.FinancialAccounting.actionPerformed(FinancialAccounting.java:287)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    	at java.awt.Component.processMouseEvent(Component.java:6263)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    	at java.awt.Component.processEvent(Component.java:6028)
    	at java.awt.Container.processEvent(Container.java:2041)
    	at java.awt.Component.dispatchEventImpl(Component.java:4630)
    	at java.awt.Container.dispatchEventImpl(Container.java:2099)
    	at java.awt.Component.dispatchEvent(Component.java:4460)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    	at java.awt.Container.dispatchEventImpl(Container.java:2085)
    	at java.awt.Window.dispatchEventImpl(Window.java:2475)
    	at java.awt.Component.dispatchEvent(Component.java:4460)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    please help me :(

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    It's hard to help you, as you never show what line is causing this exception. By the way, you really shouldn't be using null layout and absolute positioning. You're far better off using one or more layout managers.

  3. #3
    Join Date
    Jan 2010
    Posts
    72
    Rep Power
    0

    Default

    i tried to use the grid layout here but it was not working so i was forced to implement this one...
    the 'cancel' button is on the internal frame....when i click it this error comes

  4. #4
    Join Date
    Jan 2010
    Posts
    72
    Rep Power
    0

    Default

    in simple words i want to know how to implement the actionlistener for the 'cancel' button

  5. #5
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Post

    Java Code:
    public void actionPerformed(ActionEvent e)
        {
            ...............
            ...............
            ...............
            JButton cancel = new JButton("Cancel");
    	        cancel.setFont(new Font("ARIAL",Font.BOLD,12));
    	        cancel.setBounds(410, 570, 75, 25);
    	        cancel.addActionListener(this);
    }
    in my point of view your are doing wrong use of adding action listener because you are adding action listener with button inside the actionPerformed method and this method is called when you register your listener with button outside the method. This wont work any more . . .
    In method you write your actions which you want to perform.
    you should write your code like this
    Java Code:
    JButton cancel = new JButton( "Cancel" );
    cancel.setFont( new Font( "ARIAL", Font.BOLD, 12) );
    cancel.setBounds( 410, 570, 75, 25);
    
    cancel.addActionListener( 
                   new ActionListener()
                   {
                         public void actionPerformed( ActionEvent e )
                         {
                               System.out.println( "You click Cancel" );
                         }
    
                   } // end actionListener
    );
    i explained what i understand ?

    Thanks . . .:)

  6. #6
    Join Date
    Jan 2010
    Posts
    72
    Rep Power
    0

    Default

    @ cluster storm

    thanx it worked...but i would still like to know why can't the actionlistener for the 'cancel' button be implemented outside this method :)

  7. #7
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by silversurfer2in View Post
    @ cluster storm

    thanx it worked...but i would still like to know why can't the actionlistener for the 'cancel' button be implemented outside this method :)
    If you look at your posted code this does not make any sense that you are registering actionListener with your cancel button inside the interface implementation method . . . Let suppose you register it inside your actionPerformed method and when the screen appear button also not displaying . . . because actionPerformed method is called when you register it before or after the methods body . . . Execution start line by line . . . when execution line reach the actionPerformed method, it skipped . . . and your button creation, its location etc are not performed because it depend upon the addActionListener statement . . . when cancel.addActionListener is executed it call the actionPerformed method and then the execution control shifted to this method . . . and so on . . .

    Again i say that i explained what i understand ??

    Thanks . . . ;)

  8. #8
    Join Date
    Jan 2010
    Posts
    72
    Rep Power
    0

    Default

    Quote Originally Posted by Cluster Storm View Post
    If you look at your posted code this does not make any sense that you are registering actionListener with your cancel button inside the interface implementation method . . . Let suppose you register it inside your actionPerformed method and when the screen appear button also not displaying . . . because actionPerformed method is called when you register it before or after the methods body . . . Execution start line by line . . . when execution line reach the actionPerformed method, it skipped . . . and your button creation, its location etc are not performed because it depend upon the addActionListener statement . . . when cancel.addActionListener is executed it call the actionPerformed method and then the execution control shifted to this method . . . and so on . . .

    Again i say that i explained what i understand ??

    Thanks . . . ;)
    well what you say is perfectly ok...but the thing what is bugging me is that i have created an actionListener method for the cancel button outside this interface i.e. outside this actionPerformed method but it was not working in that case....

  9. #9
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by silversurfer2in View Post
    well what you say is perfectly ok...but the thing what is bugging me is that i have created an actionListener method for the cancel button outside this interface i.e. outside this actionPerformed method but it was not working in that case....
    could you please show that code ?

  10. #10
    Join Date
    Jan 2010
    Posts
    72
    Rep Power
    0

    Default

    Java Code:
    public void actionPerformed(ActionEvent e)
        {
            ...............
            ...............
            ...............
            JButton cancel = new JButton("Cancel");
    	        cancel.setFont(new Font("ARIAL",Font.BOLD,12));
    	        cancel.setBounds(410, 570, 75, 25);
    	        cancel.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e){
    System.out.println("cancel is clicked");
    }

  11. #11
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by silversurfer2in View Post
    Java Code:
    public void actionPerformed(ActionEvent e)
        {
            ...............
            ...............
            ...............
            JButton cancel = new JButton("Cancel");
    	        cancel.setFont(new Font("ARIAL",Font.BOLD,12));
    	        cancel.setBounds(410, 570, 75, 25);
    	        cancel.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e){
    System.out.println("cancel is clicked");
    }
    Dear, you are making mistake which is that you are creating actionPerformed method two times this is not possible . . . Every ActionListener must have one actionPerformed method not two ( ActionListener documentation ).
    You cannot change the signature of the interface . . . For this you must have two ActionListener one for one method and one for other like

    Java Code:
    okButton.addActionListener( 
    		new ActionListener()
    		{
    			public void actionPerformed( ActionEvent e )
    			{
    				System.out.print( "Ok Button" );
    			}
    		}
    );
    
    cancelButton.addActionListener( 
    		new ActionListener()
    		{
    			public void actionPerformed( ActionEvent e )
    			{
    				System.out.print( "Cancel Button" );
    			}
    		}
    );
    but it is also possible that tow button have same actionListener and we recognize each button by getSource() method like

    Java Code:
    private class ok_cancel implements ActionListener
    {
    	public void actionPerformed( ActionEvent e )
    	{
    		if( e.getSource() == okButton )
    			System.out.print( "okButton" );
    		else if( e.getSource() == cancelButton )
    				System.out.print( "Cancel Button" );
    	}
    }
    
    okButton.addActionListener( new ok_cancel() );
    cancelButton.addActionListener( new ok_cancel() );
    I think you get my point !

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You may wish to create and post an SSCCE that demonstrates what you're trying to do, and gives us compilable code that we can run and adapt. Please see the link in my signature for more details.

  13. #13
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    Thanks for point me on my mistake . . .

  14. #14
    Join Date
    Jan 2010
    Posts
    72
    Rep Power
    0

    Thumbs up

    @ cluster storm

    thanx a lot man.........:)

  15. #15
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

Similar Threads

  1. Please solve my java.lang.NullPointerException error.?
    By Viruthagiri in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-23-2010, 10:30 PM
  2. Can't Solve Array Error
    By Gregadeaux in forum New To Java
    Replies: 2
    Last Post: 12-14-2009, 04:01 AM
  3. how to solve syntax error
    By pro85 in forum Java Applets
    Replies: 5
    Last Post: 04-06-2009, 12:20 PM
  4. Can't solve error message while looping
    By BHCluster in forum New To Java
    Replies: 15
    Last Post: 04-22-2008, 11:51 AM
  5. Help mi solve my error
    By Deon in forum New To Java
    Replies: 3
    Last Post: 01-11-2008, 06:26 AM

Posting Permissions

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