Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Changing button icon on pressing

88 views
Skip to first unread message

Dirk Bruere at NeoPax

unread,
Jul 8, 2009, 12:54:05 PM7/8/09
to
I want to be able to change the icon of a button when it is pressed.
So, for example, I have a red button. I press it and it changes to a
green button. I have set the new icon in the button action code, but
cannot find a way to redraw it so that it changes instantly. What method
do I use?

--
Dirk

http://www.transcendence.me.uk/ - Transcendence UK
http://www.theconsensus.org/ - A UK political party
http://www.onetribe.me.uk/wordpress/?cat=5 - Our podcasts on weird stuff

Simon

unread,
Jul 8, 2009, 1:18:04 PM7/8/09
to
Dirk Bruere at NeoPax wrote:
> I want to be able to change the icon of a button when it is pressed.
> So, for example, I have a red button. I press it and it changes to a
> green button. I have set the new icon in the button action code, but
> cannot find a way to redraw it so that it changes instantly. What method
> do I use?

repaint()?

However, what about just using

http://java.sun.com/javase/6/docs/api/javax/swing/AbstractButton.html#setPressedIcon(javax.swing.Icon)

Cheers,
Simon


markspace

unread,
Jul 8, 2009, 1:41:39 PM7/8/09
to


Works for me:


package fubar;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ButtonIconTest {

public static void main(String[] args) throws Exception
{

SwingUtilities.invokeLater( new Runnable() {

public void run()
{
createAndShowGui();
}
} );
}

private static void createAndShowGui()
{
ImageIcon dog = null;
ImageIcon pig = null;
try
{
dog = new ImageIcon(
new
URL("http://java.sun.com/docs/books/tutorial/uiswing/" +

"examples/components/SplitPaneDemoProject/src/components/" +
"images/Dog.gif"));
pig = new ImageIcon(
new
URL("http://java.sun.com/docs/books/tutorial/uiswing/"
+
"examples/components/SplitPaneDemoProject/src/components/"
+ "images/Pig.gif"));
} catch (MalformedURLException ex)
{

Logger.getLogger(ButtonIconTest.class.getName()).log(Level.SEVERE,
"Error loading images:", ex);
return;
}
JFrame frame = new JFrame( "Example Images" );
JPanel panel = new JPanel();
panel.add( new JLabel( dog ) );
panel.add( new JLabel( pig ) );

JButton button = new JButton( dog );
button.setPressedIcon( pig );
panel.add( button );

frame.add( panel );

frame.pack();
frame.setLocationRelativeTo( null );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setVisible( true );
}

}

Dirk Bruere at NeoPax

unread,
Jul 8, 2009, 2:08:25 PM7/8/09
to

I already use pressed icon.
So, I have redIcon and pressedRedIcon and when I push the button I want
it to change to greenIcon and greenPressedIcon as a toggle on an if-else
statement

repaint() doesn't seem to do anything

Roedy Green

unread,
Jul 8, 2009, 2:23:59 PM7/8/09
to
On Wed, 08 Jul 2009 17:54:05 +0100, Dirk Bruere at NeoPax
<dirk....@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>I want to be able to change the icon of a button when it is pressed.
>So, for example, I have a red button. I press it and it changes to a
>green button. I have set the new icon in the button action code, but
>cannot find a way to redraw it so that it changes instantly. What method
>do I use?

see http://mindprod.com/jgloss/jbutton.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Out of 135 criminals, including robbers and rapists, 118 admitted that when they were children they burned, hanged and stabbed domestic animals."
~ Ogonyok Magazine 1979.

Roedy Green

unread,
Jul 8, 2009, 2:30:31 PM7/8/09
to
On Wed, 08 Jul 2009 19:18:04 +0200, Simon <count....@web.de>

wrote, quoted or indirectly quoted someone who said :

> I press it and it changes to a

>> green button. I have set the new icon in the button action code, but
>> cannot find a way to redraw it so that it changes instantly

I have written such code in a proprietary project that flips the
buttons instantly. The trick is to exit your event handler quickly. So
long as you are using the EDT thread it cannot get around to handling
the automatically generated repaint event.

see http://mindprod.com/jgloss/swingthreads.html

Roedy Green

unread,
Jul 8, 2009, 2:33:17 PM7/8/09
to
On Wed, 08 Jul 2009 19:08:25 +0100, Dirk Bruere at NeoPax
<dirk....@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>


>repaint() doesn't seem to do anything

it is not needed.

I think you may have misunderstood how it works. You set you button
up with the dozen or so possible icons for various states, then from
then on the icon switches happen automatically. You don't toggle or
change the icons in your ActionEvent handler. You just notice the
button presses.

Dirk Bruere at NeoPax

unread,
Jul 8, 2009, 2:52:32 PM7/8/09
to
Roedy Green wrote:
> On Wed, 08 Jul 2009 19:08:25 +0100, Dirk Bruere at NeoPax
> <dirk....@gmail.com> wrote, quoted or indirectly quoted someone who
> said :
>
>> repaint() doesn't seem to do anything
>
> it is not needed.
>
> I think you may have misunderstood how it works. You set you button
> up with the dozen or so possible icons for various states, then from
> then on the icon switches happen automatically. You don't toggle or
> change the icons in your ActionEvent handler. You just notice the
> button presses.

Well, all I really want to do is change the icon when the button is
pressed (and released), to a new icon depending on the value of an input
parameter (int). So, I press a redIcon and if x=1 it changes to
greenIcon, if x=2 it changes to blueIcon etc

However, I cannot see how your advice applies

Roedy Green

unread,
Jul 8, 2009, 8:00:54 PM7/8/09
to
On Wed, 08 Jul 2009 19:52:32 +0100, Dirk Bruere at NeoPax

<dirk....@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>Well, all I really want to do is change the icon when the button is

>pressed (and released), to a new icon depending on the value of an input
>parameter (int).

If all you wanted to do was change the icon when it was pressed, that
happens automatically. If you wanted to change it based on some other
parameter, it may be that by the time you get the event it has already
done its automatic change. It has already noticed the press. This
might be what is causing the problem.

Try experimenting with a miniature program such as the one posted at
http://mindprod.com/jgloss/jbutton.html to sort out the button stuff.
Then when you are sure you have that nailed, move onto your actual
program.

Dirk Bruere at NeoPax

unread,
Jul 11, 2009, 12:31:09 PM7/11/09
to
Roedy Green wrote:
> On Wed, 08 Jul 2009 19:52:32 +0100, Dirk Bruere at NeoPax
> <dirk....@gmail.com> wrote, quoted or indirectly quoted someone who
> said :
>
>> Well, all I really want to do is change the icon when the button is
>> pressed (and released), to a new icon depending on the value of an input
>> parameter (int).
>
> If all you wanted to do was change the icon when it was pressed, that
> happens automatically. If you wanted to change it based on some other
> parameter, it may be that by the time you get the event it has already
> done its automatic change. It has already noticed the press. This
> might be what is causing the problem.
>
> Try experimenting with a miniature program such as the one posted at
> http://mindprod.com/jgloss/jbutton.html to sort out the button stuff.
> Then when you are sure you have that nailed, move onto your actual
> program.

Well, it would be nice to do it.
The app I have in mind is a series of buttons connecting to switch
closures over an IP connection.

Initially if the state of the switch is unknown, the button is yellow.
If on startup the prog detects a closed switch the button is red, or
green for open. When the green is pressed and the switch is closed the
button goes red, and vice versa.

A pretty idea, but there's loads more to do before that of higher priority.

0 new messages