*
The moose likes Swing / AWT / SWT and the fly likes using getIcon Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "using getIcon" Watch "using getIcon" New topic
Author

using getIcon

Larry Reynolds
Greenhorn

Joined: Oct 22, 2005
Posts: 24
I creating new buttons using btn[a]=new JButton (image1) where image1 has been defined as an icon using ImageIcon image1=new ImageIcon ("test1.gif").
I would like to be able to retrieve the icon type from the button when pressed but this doesn't seem to work:

public void actionPerformed (ActionEvent e)
if (e.getSource()=btn[1] && btn[1].getIcon<>"image1"){
do this
}

Any help/suggestions would be appreciated.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Your syntax isn't exactly Java -- for example using <> for !=, but anyway, you are first doing a case analysis on the source of the action event, then you want to do a case analysis on the current icon for the source? If that state isn't held elsewhere in a cleaner form you can always do this:

By the way, it also may be cleaner to have separate listeners for each button.


There is no emoticon for what I am feeling!
Larry Reynolds
Greenhorn

Joined: Oct 22, 2005
Posts: 24
Thanks for the great help! I guess my BASIC background shows through.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: using getIcon