Currently I am working on a crappy GUI. What I am trying to do is listen for when the user presses "enter". When they press enter, I will take the text they have written in the JTextField & clear the JTextField.
However, no keypresses are being registered. No matter what, any buttons I press are registered as text & put into the JTextField - so if I press enter, its never heard by the JFrame I applied the listener to.
Observe:
As you can see, typing results in the JTextField there. Even when I click out of the JTextField, it still has focus & the main frame does not 'hear' the keypresses.
How should I approach this?
Related
so I have a couple JButtons, and hundreds of JTextFields throughout a program (with CardLayout, don't worry). I want these JButtons to find the JTextField that was last in focus, so when the button is clicked, it sets the text of that field to be whatever that button is.
I have tried getFocusOwner() and getMostRecentFocusOwner() on JTextFields that were setFocusable(true)
I'm hoping there's a method already available somewhere for this.
we have getOppositeComponent() in FocusEvent.
which returns the other Component involved in this focus change. For a FOCUS_GAINED event, this is the Component that lost focus. For a FOCUS_LOST event, this is the Component that gained focus. If this focus change occurs with a native application, with a Java application in a different VM or context, or with no other Component, then null is returned.
see documentation for more detail.
or
in the focus lost of TextField assign the textfield to some variable and then use that variable in button click event.
I have a JList with a setSelectedIndex(-1). I have a JButton , "More Details" which only works if an option is selected from the JList. If no option from the JList is selected and "More Details" is pressed, nothing happens, which is expected. But as a user you want to know when that button should be pressed.
A simple JLabel with the instruction saying "Select an option to obtain more details" would suffice. However, is there a way to get the JLabel to appear on the gui with the instruction after "More Details" is clicked? I do not want the instruction to be on the GUI all the time, as the "More Details" JButton will be rarely used. As a result of this I was wondering if it is possible to place text on the same GUI the button is located on with the instructions?
It might depend a little bit on the LayoutManager you use, but all JComponents have a method setVisible(boolean). With this, you could in your ActionListeners for the JButton just call myLabel.setVisible(true).
This is quite possible. You could actually add your JLabel directly in your GUI and call setVisible(false) before showing it. When you hit the button and you want to show it, call setVisible(true)
Also, another way you can do this... Easier in many ways - is to use JOptionPane to pop up a warning / error message
nothing happens, which is expected. But as a user you want to know when that button should be pressed.
Use ListSelectionListener to monitor when the selection changes in the JList and enable/disable the button as required.
I have a button called "save changes" that will save any changes if any changes are detected in a JTextField component. For now, I assume if the user types anything, then the content has changed.
I am using a KeyListener, but from this question it sounds like using anything other than an ActionListener is wrong?
You can add a DocumentListener to the document of the JTextField. ActionListener gets called only when the used presses enter. The advantage of using a document listener is that you can also detect changes made by other means than just by typing.
Is there any way to make a JButton look like it has the input focus when it doesn't?
I am creating a search window that has a text area for the input and a 'Find Next' button to start the search. I want the 'Find Next' button to appear to have the focus even though keyboard input is going to the text area. The idea is notify the user that pressing <Enter> will start the search, similar to applications like Microsoft Word.
I know that I can paint the button myself, but I'm looking for an easier way.
This is not the same as input focus; it's just that the button should be the default button of the dialog.
Get the JRootPane using getRootPane() on your dialog/frame/window and use setDefaultButton on that instead.
L&Fs might draw the default button even differently than one having input focus, so a user might be quite confused if there are two apparent controls having focus.
The title is a bit confusing, but I will be using Java and Jframe. Basically, I want to be able to click anywhere on the form and have a "text area/box" show up (maybe use a JTextField or JTextArea ?). I want the user to be able to edit, delete and move this string around as well.
I am thinking I need an actionlistener to listen for clicks on the form. Each click will call for a new text"box" to be created. I am not sure how to make this "box" editable, deleteable, or moveable by the user though.
I need a way to store the string and co-ordinate data too. Would it be a good idea to simply extend the JTextField or JTextArea to add co-ordinate information to them? I see that swing is event based, so I need some kind of trigger to "save" the text (was thinking the enter key, but I realize I'd like the user to be able to enter multi-line strings).
Any thoughts would be appreciated. I am familiar with Java but only have a bit of experience with the UI portion.
Instead of an ActionListener you will need a MouseListener to track clicks.
Sounds like you need an undecorated JInternalFrame with a text box in it on JDesktopPane. However, I don't think you can create an undecorated JInternalFrame, maybe start with a normal JInternalFrame with a TextBox in it and create new frames on mouse clicks on the Desktop Pane. Then see if you can make the JInternalFrame more like a Window.
Another route is a custom component that does everything you need. This is possible, just a lot more custom code.