Results 1 to 15 of 15
Thread: plz solve this error
- 06-14-2010, 04:20 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 72
- Rep Power
- 0
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
on running the code i am having the following errorJava 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);
please help me :(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)
-
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.
- 06-15-2010, 06:06 AM #3
Member
- Join Date
- Jan 2010
- Posts
- 72
- Rep Power
- 0
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
- 06-15-2010, 06:10 AM #4
Member
- Join Date
- Jan 2010
- Posts
- 72
- Rep Power
- 0
in simple words i want to know how to implement the actionlistener for the 'cancel' button
- 06-15-2010, 12:15 PM #5
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 . . .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 method you write your actions which you want to perform.
you should write your code like this
i explained what i understand ?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 );
Thanks . . .:)
- 06-15-2010, 12:35 PM #6
Member
- Join Date
- Jan 2010
- Posts
- 72
- Rep Power
- 0
@ 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 :)
- 06-15-2010, 12:55 PM #7
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 . . . ;)
- 06-15-2010, 01:02 PM #8
Member
- Join Date
- Jan 2010
- Posts
- 72
- Rep Power
- 0
- 06-15-2010, 01:30 PM #9
- 06-15-2010, 01:35 PM #10
Member
- Join Date
- Jan 2010
- Posts
- 72
- Rep Power
- 0
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"); }
- 06-15-2010, 01:50 PM #11
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
but it is also possible that tow button have same actionListener and we recognize each button by getSource() method likeJava 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" ); } } );
I think you get my point !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() );
-
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.
- 06-15-2010, 01:54 PM #13
Thanks for point me on my mistake . . .
- 06-15-2010, 03:10 PM #14
Member
- Join Date
- Jan 2010
- Posts
- 72
- Rep Power
- 0
@ cluster storm
thanx a lot man.........:)
- 06-15-2010, 03:30 PM #15
Similar Threads
-
Please solve my java.lang.NullPointerException error.?
By Viruthagiri in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 03-23-2010, 09:30 PM -
Can't Solve Array Error
By Gregadeaux in forum New To JavaReplies: 2Last Post: 12-14-2009, 03:01 AM -
how to solve syntax error
By pro85 in forum Java AppletsReplies: 5Last Post: 04-06-2009, 11:20 AM -
Can't solve error message while looping
By BHCluster in forum New To JavaReplies: 15Last Post: 04-22-2008, 10:51 AM -
Help mi solve my error
By Deon in forum New To JavaReplies: 3Last Post: 01-11-2008, 05:26 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks