GeeCON Prague 2014*
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


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, ....)
 
GeeCON Prague 2014
 
subject: how to change the jbutton color while it is in focus?