Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Change a buttons icon from a different frame

Forgotten my old username and password! :| I also tried to search on the forums for this and it's of little help.

Anyhow, I am making a paint program for my project at uni. I have a panel (in a frame) that has buttons on them. Each button loads up a frame.
I made each frame has their own set of buttons so...
Tools button will open ToolsFrame which contains Pencil button, Paintbrush, Circle, etc.

What I want to do is change the Tools button icon so that it reflects whatever the user chose in the ToolsFrame. Is there a simple way of doing this?

e.g.
I set the buttons up like...
JButton toolsBtn = new JButton("images/tools.JPG");

Pressing toolsBtn will make a new ToolsFrame object:
public void actionPerformed(ActionEvent e)
{
if("tools".equals(e.getActionCommand()))
{
tools = new ToolsFrame();
tools.show();
tools.setLocationRelativeTo(this);
tools.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
ToolsFrame is a whole class extending JFrame and implementing ActionListener. The buttons in ToolsFrame are setup just like the toolsBtn above:
JButton paintbrushBtn = new JButton("images/paintbrush.JPG");

Pressing any button in ToolsFrame will close the window.
[1344 byte] By [Chi] at [2007-11-11 8:26:21]
# 1 Re: Change a buttons icon from a different frame
Did you try the method setIcon() of JButton? this will set your icon to whatever you want whereever you want
graviton at 2007-11-11 22:35:24 >
# 2 Re: Change a buttons icon from a different frame
ah right...lol. I wondered why it didn't work - i didn't put in any event listeners to actually invoke setIcon(). that was simpler than expected tbh. lol.
Chi at 2007-11-11 22:36:29 >