Java not focusing any component - java

I use a c1.setFocusable(false) so that my Checkbox c1 is not focused as shown in the pic and after that, focus goes to the next Checkbox c2, I use again c2.setFocusable(false) and it continues to all Checkboxes. Even if it doesn't find one, it continues to the next JTextField. Is there a way to not let the focus go to something instead of setting focusable to all my numerous components?

how can I get rid of that grey border thing?
Why would you want to do that? That "grey border thing" is the focus indicator. It is used to provide information to the user so the user knows which component has focus.
If you really want to confuse the user and make them guess which component has focus then you can use:
button.setFocusPainted( false );

Related

How do you remove the selection box in the GUI?

In any program, or at least most, when you select a button or anything, there is a selection box made of lines which are made of dots.
How do you get rid of that box?
The reason why I want to do this is because I have a button with an image, no contentFill, no borders, and it looks very awkward when selected.
Make the button or component non-focusable by calling its setFocusable(...) method:
myButton.setFocusable(false);
Edit
... or maybe even better:
myButton.setFocusPainted(false);
This way the component can still get the focus if need be, but just doesn't show the focus border.

What does focus means?

Just wondering what focus means in java code, because I have seen onWindowFocusChanged, addFocussables, findFocus...
If I have a scrollable list and I scrolled it down, the first item will have focus false? or it means other thing?
Thanks
Focus means you have selected the particular GUI element. For example when you select a window that window gains focus, when you select another window the first window loses focus.... It's the same for JTextField, JTextArea, etc.
The definition of the focus here on StackOverflow is as follows:
Focus indicates the component of the graphical user interface which is
currently selected to receive input.
Saying that focused component is selected is not accurate. For instance, we can have a JCheckBox which is deselected (has no tick mark) and it is also the current focus owner. Since it has focus, its state is toggled with the spacebar. The term active is more precise. I came up with the following definition of focus:
Focus is a state of a component in which it receives keyboard input. Focus is represented by some visual cue; for instance, in Metal look and feel a focused JButton has a blue rectangle around its label. The component with the current input focus is called the focus owner.
The current GUI element that is "active" has the focus. For example when you have several Input windows only one can have the focus and receive your keyboard input. See here the Android GUI doc http://developer.android.com/guide/topics/ui/ui-events.html

Retaining Focus for KeyboardListener

I have a JFrame which contains most everything in the application. It has a KeyListener attached, and it also has several buttons and textfield on it. The problem it, when a button is clicked or a textfield is selected, it gets focus and shortcuts don't work. Of course, one can tab out of them, but to do this you must tab through EVERYTHING (each button, each textfield) before giving the window focus again.
Is there a sensible way to only require one tab to return focus to the frame from the textfield, and no tabs to return focus to the frame from a button click?
A WindowListener doesn't seem like the best way to do this, but if it's the only way I suppose I can forge forward there.
Thanks in advance!
It has a KeyboardListener attached
I have never heard of the KeyboardListener class so I can only guess what you are trying to do.
My guess is that you should NOT be using a listener of any kind.
Instead you should be using Key Bindings.
If you only need this for the textfield, you can add a keyListener to the textfield and when the user presses tab use yourJFrame.requestFocus(). Otherwise refer you may want to use a window manager or a key map.

Rectangular Java Swing Radio buttons?

I'd like to create a set of buttons in a Java Swing application like you get in a typical tool palette in a paint program. That is, a set of small square buttons, each containing an icon, only one of which is pressed down, and when you press another button, the first is deselected. I've thought of a number of solutions and none of them seem very easy/elegant.
This sounds like a job for JRadioButton, but if you add an Icon to that, you still get the small circle, which is fairly space inefficient. I guess an option would be finding an alternative Look and Feel or painting code for JRadioButton.
Another alternative might be to add JButton to a ButtonGroup, maybe setting a JToggleButton.ToggleButtonModel as the model, but that doesn't have the desired effect, as the painting code for a standard JButton does not keep it depressed when selected. Possibly the JButton code could be modified to do this. Like making it painting "selected" the same way as "pressed".
A third alternative would be to use normal JButton's, and add a common mouse listener that keeps them pressed or not, and communicates the changes between the buttons.
Can anyone advise on the best way to achieve the aim please? A simple method I've missed would be best, but advice on which of these three alternatives would be best and pointers on how to get started would be useful too.
What about a plain JToggleButton in a ButtonGroup? It is not abstract, you can instantiate one with an Icon, and it stays depressed while selected.
See the SwingSet2 demo:
http://java.sun.com/products/plugin/1.4/demos/jfc/SwingSet2/SwingSet2.html
Click the second icon on the toolbar (the one twith the check box and radio button) then tab "Radio buttons". Then click on "Paint Border" on the right panel, under "Display Options".
Source code of the demo is under your JDK install dir, so for example on my PC it's under \jdk1.6.0_01\demo\jfc\SwingSet2\src

How can I freeze on one pane of a JTabbedPane?

Under certain circumstances, I need a JTabbedPane to remain on one pane until the user supplies certain information. Essentially, when this circumstance occurs, I need the current pane to become modal.
How can I implement this? I was thinking I could catch whatever event is triggered when the pane changes, and reset back to the pane I want to stick on. But I'm worried that this won't be quite right, that depending on when the event actually fires the transition to the new pane will happen after I call the method to set the pane to the pane I want, or some other similar race condition. Is there a better way? Is there a way I can make this approach work?
I would suggest setting the other tabs to disabled. This has a positive effect of providing the user feedback that they cannot click out of the tab. Otherwise they may be madly clicking and wondering why it will not let them leave the tab.
Simply set them enabled again after the required fields are completed.
just disable the JTabbedPane:
pane.setEnabled(false);
and enable it if all fields are correctly set (or whatever condition)
You could use a CardLayout along with JPanels to do what you want and not use JTabbedPanes. Since you need to use the tabbed panes, I would suggest that once the condition has been reached that you want to force the user to stay on that tab set that tab to be the selected one by using.
setTabComponentAt(int index, Component component)
or
setSelectedIndex(int index)
Set a flag indicating that the user should not be able to proceed until completing whatever it is you want them to do and have all the other tabs be disabled using setEnabledAt(int index, boolean enabled)
.
Once the user has completed what they needed in order to continue set the flag accordingly and reenable the other tabs.
I haven't the time to try that solution out but I think it should work.

Categories

Resources