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.
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();
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.
Related
I have a multiple-choice JavaSwing app that has checkboxes and next/previous buttons. I had it modified so focus is now on the Next button when it launches, allowing user to press Enter/Return to go to next screen.
The problem now is when user selects a checkbox, the Next button loses its keyboard focus and grey outline. Coder has added a hack so the button is regains focus and grey outline each time a checkbox is selected, but then the button blinks on each mouse-click! Some screens have 4 correct checkboxes, so button blinks 4 times.
I think this is unacceptable behaviour but he says,"Expected behavior is when you select any control its obvious it gains focus on that Element. You are saying to override Java Inbuilt Functionality which is not possible."
This just feels wrong. Is he right? Is there any source I can point him to that will allow him to keep focus on checkboxes on mouse-click, while keeping keyboard focus on buttons?
Many thanks in advance.
You should not mess around with the keyboard focus - that only infuriates the users that try to navigate your UI with key strokes only.
What you are looking for is the "default button" that is available on JFrames and JDialogs rootpane:
public JFrame makeFrame() {
JFrame f = new JFrame(...);
JButton next = new JButton("next");
f.getRootPane().setDefaultButton(next);
return f;
}
public JDialog makeFrame() {
JDialog d = new JDialog(...);
JButton next = new JButton("next");
d.getRootPane().setDefaultButton(next);
return d;
}
Your programmer probably should just set setFocusable(false) on the checkboxes.
I have been searching on the web for this but have not found a solution.
I have a 2D array of JButtons. When the user clicks the button, my application currently adds an ImageIcon to that button and does setEnabled(false) to the button.
The problem with this is that it greys out the image. I want the image to stay in full colour but I want to make the JButton not clickable. It would seem that setEnabled() is not the method that I want.
I have considered removing the ActionListener from it yet this would still allow the action of a click (even though nothing would happen).
How do I make a JButton unclickable without disabling it or removing the ActionListener?
JButton derives disabled icon by graying out original icon. if you want the button to be disabled but still the icon in full color, set its disabled icon explictly to full color image using following method
button.setDisabledIcon(button.getIcon());
checkout the javadoc for AbstractButton#setDisabledIcon(Icon)
I am extending the JButton class and using an ImageIcon as my button background. I have also added a mouseListener so I can change the image of the button when the mouse hovers over the button or presses the button. This all works fine but the problem is that when the button is clicked a JOptionPane is displayed asking the user "Are you sure" while the option pane is displayed the buttons MouseClicked, mouseExited or mouseReleased methods are not executed so it looks like the button is stuck pressed until the use deals with the option dialog. Removing the dialog is not an option. Is there a way to get the mouse events executed before or during the dialog displaying?
I have also added a mouseListener so I can change the image of the button when the mouse hovers over the button or presses the button.
Read the AbstractButton API. There are methods that allow you to specify the Icon for these situations. For examples setPressedIcon().
it looks like the button is stuck pressed until the use deals with the option dialog.
Not sure if the ButtonModel state has been changed before the option pane is displayed or not. Anyway you can try using:
button.paintImmediately(...);
before the option pane is displayed. If the ButtonModel state has been updated the button should be repainted in its normal state. Note, this method should rarely be used since it can affect painting performance.
i am working with swing gui.
I have a simple Jframe with a tabbed pane with two tabs. When i click a non selected Tab, and don't move the mouse, nothing happens. i.e. the hidden tab does not appear. But, if i click the unselected tab, then mouse over to a button(without clicking button) the tab repaints.
I also see this behavior with toggle buttons too. I toggle the text of a button based on the state but it doesn't show/repaint until i move focus to another button.
Am i supposed to be calling a repaint for the whole Jframe after every event? or am i not processing events properly?
I am having one simple form i created using JFrame.
In which i used one small JButton in which i set text only "X", now the problem is that when i press that button some border appears around the text which should not appear.
So, how can i resolve it?
How can i remove this border.I mean to say when the user clicks then also it should not show this border.
Here Try to look at the button at the corner with red background.
Looks like a keyboard focus indicator. If you want that never appear call setFocusable(false) for that button.