Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing imageIcon of JButton in actionPerformed

Bill Jackson
Posts: 1
  • Mark post as helpful
  • send pies
  • 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.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!

Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!