As you can see, the passwordField is positioned before jButton1 in the navigator, however, when the usernameField is focused, and I press the TAB key, the second element being focused is the jButton1 and not the passwordField.
What really controls the order of focus?
The phrase you need search for is "tab order": there is an answer to this here. Briefly, you will need to implement a FocusTraversalPolicy subclass, and make your container use it by calling setFocusTraversalPolicy.
The default focus transferral is based more on the visual layout the the order of the components. In this case it would want to go left to right, top to bottom. This is the expected behaviour – MadProgrammer just now edit
Take a look at How to use the Focus Subsystem, especially, Customizing Focus Traversal
Updated
Take a look at switching JTextFields by pressing Enter key, which demonstrates the use of a custom focus policy
Try to search tab index or tab order to set for the components
Related
I have a JEditorPane and when I press ctr + space it makes visible a JScrollPane but because the editor pane is still selected is there any way to set the selected Item to be another?
(Selected is when you press a JTextArea and you can edit it so is selected the text area.)
Like I want to when I press ctr + space it appears the scroll pane and it selected so I just need to press an arrow to scroll.
You need to call requestFocus() on the JComponent you want "selected".
Edit: From the documentation for JComponent#requestFocus:
Note that the use of this method is discouraged because its behavior is platform dependent. Instead we recommend the use of requestFocusInWindow(boolean).
So maybe give requestFocusInWindow() a try as well.
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.
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
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.
I want a behavior similar to e.g. Firefox where the list of available tabs does only show up if at least two tabs exist.
I wasn't able to find anything like that, yet.
The best idea I had was changing the layout manually:
in case of one component, just add that to the surrounding panel
if a component is added, remove the component from the surrounding panel, add a JTabbedPane instead and add both the previous and the new component to that pane.
if a component is removed and only one component is left in the pane, remove the pane and add the contained component instead.
While this would probably work it feels like a hack or workaround...
Any better idea?
A solution should ideally work in both Java 1.5 and 1.6... but I'd be happy about a 1.6-only solution, too.
You can override the UI method that calculates the height for the tab button area, forcing the height to 0 when there's only one tab:
tabbed_pane.setUI(new BasicTabbedPaneUI() {
#Override
protected int calculateTabAreaHeight(int tab_placement, int run_count, int max_tab_height) {
if (tabbed_pane.getTabCount() > 1)
return super.calculateTabAreaHeight(tab_placement, run_count, max_tab_height);
else
return 0;
}
});
You may be better off simply using CardLayout.
I believe you'll have to do it manually. Apparently it has been done before, but only as a small bit of a system which seems to not be available.
Your approach looks good to me. I would do it just like you laid it out, and wrap all that logic in a custom JComponent so it will feel less hackish.
Yes, there is a way. Took me four hours to find at the oracle website:
http://docs.oracle.com/javase/7/docs/api/javax/swing/JTabbedPane.html#setTabLayoutPolicy()
Simply use this:
//declare
private JTabbedPane editor = new JTabbedPane ();
//construct like this:
editor.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
//just add components and see how it goes.
editor.addTab("", newPanel);
Another option would be to customize the L&F delegate (either BasicTabbedPaneUI or WindowsTabbedPaneUI depending on the platforms you care about) used by the JTabbedPane. This would allow you to customize the behavior of the tabbed pane in the case where only a single tab was being shown.
This is another way of doing things however I would say it's quite an undertaking and doing what Michael said will get you where you want to go with a lot less effort. I just wanted to post this as an answer in case you weren't aware of this option.
I think this can be achieved using tab bar and a card layout,
add the tab bar and card layout to a grid bag layout so that they
re-size automatically
the max height of the tab bar should be the height of the tab
add a listener to tab bar so that when certain tabs are clicked it
will switch the card layout to show appropriate content
hide the tab bar if it has only one tab
and this should do the job.
jTabbedPane1.removeTabAt(0); seems to work after initComponents();