This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Applets and the fly likes Accessing imageIcon of JButton in actionPerformed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Accessing imageIcon of JButton in actionPerformed" Watch "Accessing imageIcon of JButton in actionPerformed" New topic
Author

Accessing imageIcon of JButton in actionPerformed

Bill Jackson
Greenhorn

Joined: Sep 11, 2011
Posts: 1
Hi everyone, I'm new here and also fairly new at Java.
I'm working on a project (not for a class) that I can't figure out.
We have an applet that teaches basic programming skills, the part I currently wrestle with is referred to as a goal image panel.
Desired behavior -
Click on goal image button in the main applet, JFrame opens with goalImages on tabbed pages as JButton icon images. Student clicks an image, a new JFrame opens with just that one image for the student to refer to.
The number of goalImages is varies because this framework can be applied to other applets that might have a different number of images.

Everything works up to the part of clicking on the goalImage JButton to have that one image open in it's own JFrame.

JButtons are added to the selection form this way (4 per page, up to the number stored in imgNum)...

for (int k = 0; k < 4; k++) {
if(i < imgNum){
String imgCodeNum = "Img" + digit3.format(i);
nameStrings[i] = prop.getProperty(imgCodeNum + ".name");
srcStrings[i] = prop.getProperty(imgCodeNum + ".loc");
descStrings[i] = prop.getProperty(imgCodeNum + ".desc");
tooltStrings[i] = prop.getProperty(imgCodeNum + ".toolt");


images[i] = createImageIcon(srcStrings[i]);
ImageIcon img = images[i];
btn = new JButton();
btn.setIcon(img);
btn.setBorder(BorderFactory.createRaisedBevelBorder());
btn.setToolTipText(tooltStrings[i]);
btn.addActionListener( this );
panel1.add( btn );

i++;
}
}
tabPane.add("Page "+(j+1), scroller);
}
add( tabPane );

I am thinking that

void actionPerformed( ActionEvent ae)
Object src = ae.getSource();

returns the JButton pressed, which I've printed to the console and there seems to be (among a lot of other stuff) a reference to defaultIcon = file:.... that I could use to set the image into the new JFrame, but I can't figure out how to access src's attributes.

I've tried:
Icon displayImg;
displayImg = src.getIcon();

It's seems like it should be easy, but... how would you suggest doing it?

Thanks in advance!
Bill




 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing imageIcon of JButton in actionPerformed
 
Similar Threads
Icons
using getIcon
Problems with ImageIcon in JButton
Why can't my button's Image Icon change when i set Icon???
How to change the coffee cup icon in JFrame