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.
Related
I'm having difficulty in getting resources that will help me with an issue. I want to add a button that will let me insert text anywhere in my JFrame so I can click and add a text
Before that, I have been using JTextField and JTextArea, but these are useful in a predefined space (I believe).
What I want is like the classic Add Text button in Paint ("A" button) in which when you click on it, you will have the text cursor (like an "I") and then click somewhere in the frame so I can actually add text in that area. Which I call "adding text dynamically". Unfortunately I don't have any code to show because I'm not sure how to build the code from scratch or maybe use a method in Swing, but I'm more than keen to explain better if this is too generic for you guys.
You're going to need to be familiar with Java graphics and associated methods, particularly drawString(), so start by reading the official Graphics Tutorial.
Then learn about MouseListeners, which will help you recognize when and where the user clicks in your JFrame.
Then learn about KeyListeners, which will allow you to detect when your user is typing. You may wish to use a key bind instead, but as described, your UI is relatively simple with only one focusable component, so keylisteners are probably OK.
This general strategy should help you take a flying leap at the problem:
detect mouseclick with mouselistener
draw an insertion bar
detect keypress with keylistener
use graphics.drawstring to draw the character specified by the keypress
redraw the insertion bar
Make sure your keylistener handles the enter key, so that you know when to erase the insertion bar. You're going to paint your JFrame in between each of those steps.
The result will be crude, to say the least, and you'll notice that you have to handle a lot of issues that crop up along the way before things start to look professional. For example, you'll have to deal with font metrics. Not every character is going to be the same width, so you have to calculate the X argument to your drawString() based on the charWidth of whatever character the user types. You might do well to start with a monospaced font for that reason.
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?
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 probably a quick question but here it goes. I am making a game (new to programming) and I want a text field in the middle of my game that I can easily add to kind of like a console. You will be able to view the beginning from late into the game.
Example:
Welcome to the game.
Added when you click a button
More text is added.
You click a different button
More text is added.
I am currently using a JLabel for this but it's a .setText which is inconvenient when I want to have tons of text throughout my game. As well, how could I add a scrollbar to this?
I'd guess you are looking for JTextArea .
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.