Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-27-2008, 12:19 PM
Member
 
Join Date: Aug 2008
Posts: 52
playwin2 is on a distinguished road
[SOLVED] ActionEvent.setSource() is failing in Swing while working in AWT
hello,

I have a class which works under AWT but fails under swing.
The part fails under swing uses ActionEvent.setSource(..)....

For ex. following code works...

Code:
public class Pad extends Panel implements ActionListener { private Button ok = new Button("OK"); public Pad() { setName("MyPad"); setLayout(new FlowLayout()); add(ok); ok.addActionListener(this); } public void addActionListener(ActionListener listnr) { ok.addActionListener(listnr); } public void actionPerformed(ActionEvent e) { e.setSource(this); } }
Now the same code fails if I make it swing...
Code:
public class JPad extends JPanel implements ActionListener { private JButton ok = new JButton("OK"); public JPad() { setName("MyJPad"); setLayout(new FlowLayout()); add(ok); ok.addActionListener(this); } public void addActionListener(ActionListener listnr) { ok.addActionListener(listnr); } public void actionPerformed(ActionEvent e) {// Here is the problem I think e.setSource(this); } }
Bellow is a test code to clarify the problem...
Code:
public class JPUser extends JFrame implements ActionListener { private JButton ok = new JButton("OK"); private JPad swPad = new JPad(); private Pad awtPad = new Pad(); public JPUser() { super("Test"); setLayout(new FlowLayout()); add(ok); add(swPad); add(awtPad); ok.addActionListener(this); swPad.addActionListener(this); awtPad.addActionListener(this); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); pack(); setVisible(true); } public void actionPerformed(ActionEvent e) { //System.out.println(e.getSource()); if(e.getSource() == swPad) System.out.println("Swing Pad -- OK"); // I never see this line else if(e.getSource() == awtPad) System.out.println("AWT Pad -- OK"); else System.out.println("OK"); } public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { public void run() { new JPUser(); } }); } }
Please can anyone help...
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-27-2008, 04:43 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Norm is on a distinguished road
Quote:
fails under swing
Please post the full text of the error message.
Quote:
public void actionPerformed(ActionEvent e)
{// Here is the problem I think
e.setSource(this);
}
Why are you using set.. here? Normally you do a get... to be able to respond to the action.

Quote:
//System.out.println(e.getSource());
Uncomment the above and What does it show you?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-27-2008, 08:08 PM
Member
 
Join Date: Aug 2008
Posts: 52
playwin2 is on a distinguished road
Quote:
Originally Posted by Norm View Post
Please post the full text of the error message.
Oh! no no there are no error msgs, compiles fine runs fine.
The problem is "design", the code fails do it's purpose under swing.
Quote:
Originally Posted by Norm View Post
Why are you using set.. here? Normally you do a get... to be able to respond to the action.
Yes, but I don't want to respond to any action here., the calling class (in this case JPUser) will do that.
Quote:
Originally Posted by Norm View Post
Uncomment the above and What does it show you?
It shows :
(1)JButton as source for swPad
(2)MyPad as source for awtPad
(3)JButton as source for JButton OK.

Now, see what I mean, #2 & #3 is right but #1 is wrong.
It still shows JButton as source for swPad, means the setSource(...) method in class JPad had failed or has no effects.

This is the question why awtPad(AWT) works while swPad(Swing) fails?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-27-2008, 08:49 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Norm is on a distinguished road
Quote:
I don't want to respond to any action here
Then why implement ActionListener?

Have you put println() statements in ALL of the actionPerformed() methods to see what is happening?

Put println()s in all the methods, press the buttons and then copy the output here. Your editted version of what happens doesn't help much. Make sure the println() show where in the code they are and what the values of key variables are at each point.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-27-2008, 09:17 PM
Member
 
Join Date: Aug 2008
Posts: 52
playwin2 is on a distinguished road
Ah! very good suggestion ... indeed actually now (after putting println()) in all actionPerformed I see a new problem...

here is the output,
Code:
Pad Class Before setSource() ============================== java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=OK,when=1219857065999,modifiers=] on button0 Pad Class After setSource() ============================== java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=OK,when=1219857065999,modifiers=] on MyPad Class JPUser ================== java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=OK,when=1219857065999,modifiers=] on MyPad AWT Pad -- OK Class JPUser ================== java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=OK,when=1219857068093,modifiers=Button1] on javax.sw ing.JButton[,5,5,51x26,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$Compou ndBorderUIResource@a01335,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIc on=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14], paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIco n=,selectedIcon=,text=OK,defaultCapable=true] OK JPad Class Before setSource() ============================== java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=OK,when=1219857068093,modifiers=Button1] on javax.sw ing.JButton[,5,5,51x26,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$Compou ndBorderUIResource@a01335,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIc on=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14], paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIco n=,selectedIcon=,text=OK,defaultCapable=true] JPad Class After setSource() ============================== java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=OK,when=1219857068093,modifiers=Button1] on MyJPad
As you can see setSource() do working in swingPad but it's working after...
What to do here ....
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-27-2008, 10:13 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Norm is on a distinguished road
The problem is the actionlisteners are NOT called in the order you expected.
A solution: Instead of having the Panel class's addActionListener add the listener to their button, have them save its address and handle the listening themselves. When their actionPerformed method is called, change the source of the event to this and then CALL the actionListener's actionPerformed method with the modified event.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 08-28-2008, 01:00 AM
Member
 
Join Date: Aug 2008
Posts: 52
playwin2 is on a distinguished road
Yes, exactly, so the new JPad class :
Code:
public class JPad extends JPanel implements ActionListener { private JButton ok = new JButton("OK"); private ActionListener lsnr = null; public JPad() { setName("MyJPad"); setLayout(new FlowLayout()); add(ok); ok.addActionListener(this); } public void addActionListener(ActionListener listnr) { //ok.addActionListener(listnr); lsnr = listnr; } public void actionPerformed(ActionEvent e) { System.out.println("JPad Class\nBefore setSource()"); System.out.println("=============================="); System.out.println(e); //Modify the source e.setSource(this); System.out.println("JPad Class\nAfter setSource()"); System.out.println("=============================="); System.out.println(e); //now pass the event to calling class.. if(lsnr != null) lsnr.actionPerformed(e); } }
This solves the problem. Thank you very much.

Although the problem is solved, I'm curious do you have any idea why swing ver. behave differently than the AWT ver.? Is it that another "Swing is not thread safe" kind of thing?
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 08-28-2008, 01:25 AM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 1,289
Fubarable is on a distinguished road
Quote:
Although the problem is solved, I'm curious do you have any idea why swing ver. behave differently than the AWT ver.? Is it that another "Swing is not thread safe" kind of thing?
No, it has nothing to do with the thread-safety bugaboo but all due to what Norm said above. One way to implement what you want is have the JPad class broadcast its own actions. I'm not sure if this is kosher, but it's cool and works:
Code:
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import java.util.List; public class JPad extends JPanel //implements ActionListener { private JButton ok = new JButton("OK"); private List<ActionListener> listenerList = new ArrayList<ActionListener>(); public JPad() { setName("MyJPad"); setLayout(new FlowLayout()); add(ok); ok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ActionEvent actionEvent = new ActionEvent(JPad.this, e.getID(), e.getActionCommand()); for (ActionListener al : listenerList) { al.actionPerformed(actionEvent); } } }); //ok.addActionListener(this); } public void addActionListener(ActionListener listnr) { //ok.addActionListener(listnr); listenerList.add(listnr); } /* public void actionPerformed(ActionEvent e) { e.setSource(this); } */ }
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 08-28-2008, 01:48 AM
Member
 
Join Date: Aug 2008
Posts: 52
playwin2 is on a distinguished road
Very nice. looks much better. [thumbs up]

If I understood it right... then I think now multiple class can use JPad ...

istenerList.add(listnr);

for (ActionListener al : listenerList)
{
al.actionPerformed(actionEvent);
}



Thank a lot... appreciate it.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java mail problem(working in intranet,but not working in iternet) sundarjothi Advanced Java 8 05-28-2008 09:00 AM
[SOLVED] Actionevent problem Cymro New To Java 3 04-04-2008 09:11 AM
ActionEvent example Java Tip Java Tips 0 03-12-2008 01:00 AM
map javax.swing.text.Element to javax.swing.text.View elizabeth New To Java 1 07-30-2007 09:02 PM
Working With ANT JavaForums Eclipse 0 04-26-2007 10:16 PM


All times are GMT +3. The time now is 12:25 PM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org