1

I'm getting some trouble with a toolbar I made. In this I have 4 JButtons and below a JTable, which is set on focus when my windows shows in order to see the JButtons unfilled.

This is how I set each JButton:

    btn = new JButton("New");
    btn.setFocusPainted(false);
    btn.setBackground(SystemColor.window);
    btn.setVerticalTextPosition(SwingConstants.BOTTOM);
    btn.setHorizontalTextPosition(SwingConstants.CENTER);
    btn.addActionListener(new ActionListener() {
        ...
    });
    btn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    toolBar_General.add(btn);
    btn.setIcon(new ImageIcon(MainWindow.class.getResource("/icons/file11.png")));

Example of my toolbar at start: http://www.tiikoni.com/tis/view/?id=0ef950b

But after clicking on a JButton, it remains filled after itsactionListener does his work.

Example of my toolbar after clicking a JButton: http://www.tiikoni.com/tis/view/?id=9e88ea4

My question is: how can I mantain my buttons contentAreaFilled property true (so the user can tab between components in my window and see which control he has selected) and unfocus my button after the user click?

The result I would like to have is that of the first photo.

Matteo
  • 11
  • 5
  • Are you creating new instances of each JButton or are you reusing the same field? from your code snippet it looks like you're doing the latter – andrewdleach Aug 12 '15 at 17:01
  • I create new istances for each `JButton`. I wanted to show how I set each of my `JButtons` – Matteo Aug 12 '15 at 17:02
  • 1
    I need 4 more reputation for posting images... I think they will be useful. – Matteo Aug 12 '15 at 17:05
  • Do you have any need for added the additional calls to setFocusPainted and setCursor. Alot of Swing behavior comes prerolled in the components themselves – andrewdleach Aug 12 '15 at 17:06
  • Unfortunately I didn't see the hand cursor so I had to call `setCursor`. I wanted also to disable the dotted border so I called `setFocusPainted`. – Matteo Aug 12 '15 at 17:09
  • Add a link to the picture and someone with enough rep will edit it in for you – Vince Aug 12 '15 at 17:26
  • Maybe you are talking about a `JToggleButton`? – camickr Aug 12 '15 at 19:07
  • I upload some images so maybe the question is clearer. – Matteo Aug 12 '15 at 20:41

2 Answers2

1

I think the call you are looking for is:

btn.setRequestFocusEnabled(false);

this will stop your buttons from taking over the focus when the mouse clicks them, achieving the goal of not having the keyboard focus painted for mouse users. However it still lets keyboard-centric users "tab over" to them.

If one of your buttons gets the initial focus, however, then the keyboard focus will still render there. In that case, you probably want to select some other sensible component to place the initial keyboard focus onto by calling:

otherComponent.requestFocusInWindow();
Luke Usherwood
  • 3,082
  • 1
  • 28
  • 35
0

Are you talking about the input Focus? (the dotted rectangle a component shows when it has the focus)

You can explicitly disable a component getting the focus by calling setFocusable(false) on the component (e.g. every button). It does not prevent you from clicking on the buttons, but it does prevent you from cycling the focus with the tab-key to it.

Durandal
  • 19,919
  • 4
  • 36
  • 70