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.
Related
So my Java GUI always shows the current selected JButton by displaying a white rectangle around it. By hitting Tab or Shift + Tab the selection goes to the next/previous element.
Here is my custom decoration Panel(as an example). Is there a possiblity to hide this white frame?
By the way it has no function, if I press enter key nothing happens...
I would like to make a custom component that is much like a JList, except there is a little "x" button on the right side of each cell that removes that cell from the list (and triggers an event). I know that you would have to extend JList, but looking at the code for JList I have no idea where to go from there. For reference, I would like the list to be like on the macOS Messages app (except the "x" button can always be visible, not just when the mouse is over the cell).
I would like to make a custom component
I suggest you do that by extending JPanel and adding real components to your panel. Then you can actually add JButton with the "x" that can respond to the mouse event.
A JList does not display real components, only rendered images of the component and therefore is does not respond to events if you try to click on the "x".
The other option is to use a JTable. A JTable does allow you to display values in a column format. In this case it does support the concept of editors, which would allow you to add a button to a column. For example check out Table Button Column.
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
Can someone give me advice on how can I the change tab (only in the tab itself, not the tab's content area) in JTabbedPane to an icon. It is possible to set another icon when the tab is selected, another icon on mouse over, and another icon when tab isn't selected. I'd like to have this icon on full tab width and height under the text, similar like when one replaces a whole button with an image.
Take a look at setTabComponent, this will allow you to supply a custom renderer for that tab
You would then need to monitor changes to the tab selection via the changeListener, when the tab changes, you would use the getTabComponentAt method to get the renderer and update it
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.