aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes how to change the jbutton color while it is in focus? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how to change the jbutton color while it is in focus?" Watch "how to change the jbutton color while it is in focus?" New topic
Author

how to change the jbutton color while it is in focus?

kahkean chor
Ranch Hand

Joined: Jan 26, 2013
Posts: 56
refer to this code~

import java.awt.GridLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ButtonPane extends JPanel {

private JButton[][] buttons;

public ButtonPane(int row, int col) {
super(new GridLayout(row, col));
buttons = new JButton[row][col];
for (int i = 0; i < buttons.length; i++) {
for (int j = 0; j < buttons[i].length; j++) {
final int curRow = i;
final int curCol = j;
buttons[i][j] = new JButton(i + ", " + j);
buttons[i][j].addKeyListener(enter);
buttons[i][j].addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
if (curRow > 0)

buttons[curRow - 1][curCol].requestFocus();
break;
case KeyEvent.VK_DOWN:
if (curRow < buttons.length - 1)
buttons[curRow + 1][curCol].requestFocus();
break;
case KeyEvent.VK_LEFT:
if (curCol > 0)
buttons[curRow][curCol - 1].requestFocus();
break;
case KeyEvent.VK_RIGHT:
if (curCol < buttons[curRow].length - 1)
buttons[curRow][curCol + 1].requestFocus();
break;
default:
break;
}
}
});
add(buttons[i][j]);
}
}
}

private KeyListener enter = new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_ENTER) {
((JButton) e.getComponent()).doClick();
}
}
};

public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new ButtonPane(4, 4));
f.pack();
f.setVisible(true);
}
}
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
add a FocusListener:
focusGained() change to whatever color you want
focusLost() change back to normal
kahkean chor
Ranch Hand

Joined: Jan 26, 2013
Posts: 56
ok let say,
my button gained focus and changed its color,
how if later it lost focus?it still remains the changed color or it will change to the previous
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> or it will change to the previous

it will, if you code it that way
kahkean chor
Ranch Hand

Joined: Jan 26, 2013
Posts: 56
thank you.^^
m Korbel
Ranch Hand

Joined: Jun 19, 2012
Posts: 155
- dont to use FocusListener or MouseListener for this job

- this methods are implemented in the JButton API directly & correctly

- you can to implememt ChangeListener with taking a proper event (mouse & key too) from ButtonModel (isArmed, selected, ....)
Vvittya Malk
Greenhorn

Joined: Oct 02, 2014
Posts: 1
m Korbel wrote:- dont to use FocusListener or MouseListener for this job

- this methods are implemented in the JButton API directly & correctly

- you can to implememt ChangeListener with taking a proper event (mouse & key too) from ButtonModel (isArmed, selected, ....)


Why use a ChangeListener instead of a FocusListener or MouseListener?
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 650
    
  11
Indeed.

I would simply add a MouseListener to the button, like:

But what m Korbel says also works:

(Note that the button must be made final, for both to work).

But maybe you can use a ChangeListener in a handier way. Apart from this,
I dont know why one method should be preferred over the other method.

@m Korbel:
can you give us some additional information? Thanks!

Greetz,
Piet
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to change the jbutton color while it is in focus?