• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Accessing imageIcon of JButton in actionPerformed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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




 
reply
    Bookmark Topic Watch Topic
  • New Topic