--
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
repaint()?
However, what about just using
Cheers,
Simon
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 );
}
}
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
>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.
> 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
>
>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
>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.