This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
kahkean chor
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
add a FocusListener:
focusGained() change to whatever color you want
focusLost() change back to normal
 
kahkean chor
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> or it will change to the previous

it will, if you code it that way
 
kahkean chor
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you.^^
 
m Korbel
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- 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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1153
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic