Re: Changing JButton icon when pressed
jill.mcafee@vanderbilt.edu wrote:
I am confused what you mean by not having the actual code. I included
the entire test case with my first post, as well as a web link. I am
not using a MouseListener, but ActionListener as indicated in the code.
I appreciate your taking the time to help me. The code you provided
for handling actions is like mine, only I am attempting to change the
ImageIcon rather than the background color, and I am working in an
Applet. Perhaps the new button is being created but not being
displayed? It seems like this should be easy to accomplish.
Jill:
I can only go on what you told me and that was the "The button will stay
the "selected" color until I move the mouse off the button, and then it
changes back to the unselected color. Any ideas?" The only way it can
do that is if you are listening to the mouse. If you meant it only
stays that way when you press and hold the button down then that is
something else. If what you are trying to make is a set of buttons that
have a relationship and change icons as they are pressed, then you need
to write your code that way. You also need to heed the things I
mentioned in my first reply to you, that is if you are going to use
Swing components (JApplet, JButton, and JPanel), you can't mix them with
AWT components (Applet, Button and Panel). Your code snippet from the
last post doesn't change the icons of the buttons that weren't pressed.
Is that what you want? If you set the icon of the button it will stay
set until you reset it. If you are still stuck, I would suggest that
you write an even simpler example and post it here for help. Clearly
define your objectives eg. you want the last button pressed to have this
icon or be blue and the others to display their inactive icon or be red.
Also if you want to create a button with special characteristics, then
write a class that does what you need don't just try to do it with a
method.
public class JillButton extends JButton {
ImageIcon notPressed, recentlyPressed;
JillButton[] allTheOtherButtons;
JillButton(String text,
ImageIcon notPressed,
ImageIcon recentlyPressed,
JillButton[] allTheOtherButtons) {
super(text,notPressed);
this.notPressed = notPressed;
this.recentlyPressed = recentlyPressed;
this.allTheOtherButtons = allTheOtherButtons;
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
for (int i=0; i<allTheOtherButtons.length; i++)
allTheOtherButtons.notPressed();
setIcon(recentlyPressed);
}
};
}
public void notPressed() {
setIcon(notRecentlyPressed);
}
you finish it...
--
Knute Johnson
email s/nospam/knute/