It's not a secret anymore!
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
JavaRanch » Java Forums » Java » Applets
Bookmark "Accessing imageIcon of JButton in actionPerformed" Watch "Accessing imageIcon of JButton in actionPerformed" New topic

Accessing imageIcon of JButton in actionPerformed

Bill Jackson

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.addActionListener( this );
panel1.add( btn );

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!

I agree. Here's the link:
subject: Accessing imageIcon of JButton in actionPerformed
It's not a secret anymore!