1

Button lost focus: enter image description here Button has focus: enter image description here

Short code:

MLibrary = new JButton();
MenuPane.add(MLibrary, new AnchorConstraint(0, 0, 0, 0, AnchorConstraint.ANCHOR_ABS, AnchorConstraint.ANCHOR_NONE, AnchorConstraint.ANCHOR_ABS, AnchorConstraint.ANCHOR_ABS));
MLibrary.setText("Library");
setButtonDefaults(MLibrary);

MScheduler = new JButton();
MenuPane.add(MScheduler, new AnchorConstraint(0, 0, 0, 110, AnchorConstraint.ANCHOR_ABS, AnchorConstraint.ANCHOR_NONE, AnchorConstraint.ANCHOR_ABS, AnchorConstraint.ANCHOR_ABS));
MScheduler.setText("Scheduler");
setButtonDefaults(MScheduler);

private void setButtonDefaults(JButton but) { //calls in JPanel init only for setting button defaults
    but.setBorderPainted(false);
    but.setBackground(Color.DARK_GRAY);
    but.setForeground(Color.WHITE);
    but.setName(but.getText().toLowerCase());
    but.setPreferredSize(buttonSize);
    but.addActionListener(this);
}

private void enableOnlyOne(JButton but) {//calls each time when one of menu buttons are pressed. but is pressed button
    // TODO Auto-generated method stub
    setButtonDisabled(MLibrary);
    setButtonDisabled(MScheduler);
    setButtonDisabled(MBudget);
    setButtonDisabled(MReports);
    setButtonDisabled(MManage);
    setButtonDisabled(MSettings);
    //enable one
    //but.setFocusPainted(true);
    but.getModel().setPressed(true);
    but.setBackground(ContentPane.getBackground());
    but.setForeground(Color.BLACK);
}

private void setButtonDisabled(JButton but) { //sets button unpressed
    but.getModel().setPressed(false);
    but.setBackground(Color.DARK_GRAY);
    but.setForeground(Color.WHITE);
}

So, my question is how to code each button, when button looses focus, it would be with light grey background, not default appearance

Paulius Vindzigelskis
  • 2,121
  • 6
  • 29
  • 41

1 Answers1

3

Add a Focus Listener to each button and implement the function public void focusLost(FocusEvent e) and inside thie function set the background of the buttons.

Source: http://docs.oracle.com/javase/tutorial/uiswing/events/focuslistener.html

UPDATE:

Inside the focusLost(FocusEvent e), put the following code:

JButton button = (JButton) e.getSource();
e.setBackground(Color.Gray);
Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
  • @PooLaS : inside your focusLost Method do this. JButton button = (JButton)e.getSource(); if (button == MLibrary) {Mlibrary.setBackground(Color.LIGHT_GRAY);} else if (button == MScheduler){MScheduler.setBackground(Color.LIGHT_GRAY);} Hope that might can help. – nIcE cOw Jan 08 '12 at 19:48
  • Just one question, why did you put the **if statement** if on **if** and **else** you do the same work? – Adel Boutros Jan 08 '12 at 19:58
  • @AdelBoutros `e.getComponent().setBackground(ContentPane.getBackground());` I used this to set background, but I can't reach ContentPane because it is outside FocusListener. How can I reach this? ContentPane is jpanel below main menu. This time I try to make same color button and contentPane background, so it looks like tab button – Paulius Vindzigelskis Jan 08 '12 at 19:59
  • @Adel Boutros : Exactly, seems like my brain is stuck today, hehe. He can write inside his focusLost method simply this. e.getSource().setBackground(Color.LIGHT_GRAY); And everything will work. LOL, MY BAD, seems like i shouldn't answer anyone today. Regards – nIcE cOw Jan 08 '12 at 20:02
  • @GagandeepBali you can't just write `e.getSource().setBackground(Color.LIGHT_GRAY);`. I tried it first (before your post) and eclipse didn't let me, so I changed it to `e.getComponent().setBackground(Color.LIGHT_GREY);` – Paulius Vindzigelskis Jan 08 '12 at 20:05
  • @PooLaS : I am really not myself today, just see Adel Boutros's edit, use that, simply replace e with button, i.e. button.setBackground(Color.LLIGHT_GRAY); r – nIcE cOw Jan 08 '12 at 20:09
  • Check this link for the difference between **getComponent** and **getSource** (http://www.coderanch.com/t/196784/java-programmer-SCJP/certification/getSource-vs-getComponent) – Adel Boutros Jan 08 '12 at 20:23
  • Looks like everything working and Adel Boutros answer is good, but strange thing happens. When I tried to log what color returns `ContentPane.getBackground()`, it says 238 238 238. It also return that if i call log inside FocusListener. But if I try to put that color directly to setting background in focuslost and focusgained functions (`e.getComponent().setBackground(ContentPane.getBackground())`), shows as default JButton, but if i manually set color (`e.getComponent().setBackground(new Color(238, 238, 238))`), then it works. Strange and don't know how to fix this – Paulius Vindzigelskis Jan 08 '12 at 20:23
  • @AdelBoutros do you have any idea how to fix this (what I mentioned in post before)? – Paulius Vindzigelskis Jan 08 '12 at 21:36
  • I didn't quite understand your second problem – Adel Boutros Jan 08 '12 at 21:39
  • maybe it's better to post it as a 2nd question with the new code you have modified based on my suggestion, then I can maybe help you. – Adel Boutros Jan 08 '12 at 21:39
  • @AdelBoutros ok, I moved this question [here](http://stackoverflow.com/questions/8788696/seems-like-jpanel-background-isnt-read-in-focuslistener) – Paulius Vindzigelskis Jan 09 '12 at 12:52