JLabel keyboad alternative - java

The first paragraph of the JLabel API documentation states:
"A label does not react to input events. As a result, it cannot get the keyboard focus. A label can, however, display a keyboard alternative as a convenience for a nearby component that has a keyboard alternative but can't display it."
I'm pretty sure I understand the concept of not being able to gain focus for keyboard events. What I'm not sure about is what it means when it says "A label can display a keyboard alternative as a convenience for a nearby component that has a keyboard alternative but can't display it".
What is a keyboard alternative? Why wouldn't a nearby component be able to display it? How does a label display a keyboard alternative for a near-by component?

What it means is, with a combination of JLabel#setDisplayedMnemonic and JLabel#setLabelFor you can configure the label to display a short cut key to the user that when activiated, with transfer focus to the associated component.
The mnemonic is a single character within the text of the label, which when the activation key is held down (Alt on windows), will allow the user to transfer focus to the associated field.
For example, if you had a label with the text First name:, you could set the mnemonic to F, which would allow the user to press Alt+F to focus the associated field.
Under windows, when you hold down the Alt key, it will display an underscore character under the mnemonic character. So Look and Feels will always display this underscore and some may highlight the fact in other ways

How does a label display a keyboard alternative for a near-by component?
Say you have a label "First Name" followed by a text field. You can use:
JTextField textField = new JTextField(10);
JLabel label = new JLabel("First Name");
label.setLabelFor( textField );
label.setDisplayedMnemonic(KeyEvent.VK_F);
Now when the user uses Alt-F focus will be placed on the related text field.

Related

Java KeyBindings not working after entering text in jTextField?

Basically, I have a java program that acts like a media player, PLAY PAUSE FAST FORWARD options etc.
Which also have keybindings.
However, in another JPanel I also have a JTextField that allows the user to enter some text to act as commentary.
If the user decides to write some text, then this JTextField is focused and they key bindings don't work anymore.
But if I did setFocusable(false) to all containers and child containers, the user won't be able to enter text into the JTextfield.
How do I solve this?
JComponent#getInputMap uses WHEN_FOCUSED by default.
Depending on your needs you can use WHEN_ANCESTOR_OF_FOCUSED_COMPONENT or WHEN_IN_FOCUSED_WINDOW to change the level of focus you component needs in order to respond to key input
See JComponent#getInputMap(int) and How to Use Key Bindings for more details

set hidden text into JTextfield in java

I'm trying to create one search toolbar that appears inside the letters hidden stick. when I press the hidden letters that when the bar disappears and there's no hiding the letters that appear. I've tried to capture events keyPressed entering the search bar, but I do not really feel comfortable with that way. if anyone has a better way, I hope everyone helps do.cam Thanks!
photo illustration
Check out the Text Prompt.
You can control when the prompt disappears. You can also control the color and style of prompt.
The Text Prompt uses a DocumentListener and a FocusListener to determine when the prompt should be displayed.
You can use the default settings with a single line of code:
JTextField textField = new JTextField(10);
TextPrompt tp7 = new TextPrompt("First Name", textField);

Reading alt code JTextArea

I have a barcode that reads :
"SerialNumberALT09ProductNumber"
where ALT09 = Tab.
I read the number in to a JTextArea, if i write the number manually with the tab key on my keyboard i get a valid input i can then text.trim().split("\t");
to get a valid input to get respective serial number and product number. But when reading the barcode who sends ALT09 it does not read anything.
How do i get the JTextArea to accept ALT09 as Tab (or as alternative split on the location).
JTextArea is not getting Tab at all. Tab is interrupted by whole GUI interface to switch to next editable field. Of course you can bend this rule, intercept Tab on parent container and force it to send it to the child JTextArea and then you can even write your won method for KeyPressed event and insert Tab character into the text but it's good approach because it changes user experience. User expects Tab to go to next field but for this particular text area you say that it should be delimiter for your text? Another reason - Tab is similar for Space - so in user experience it's not clear was input right or not.
To avoid all these troubles why not take simple approach:
SerialNumber=ProductNumber
It is clear, visible, predictable, understandable and most off all - doesn't required your question ;)

Java chat client jtextfield vs jtextarea

I am writting a simple server chat client using gui to make the chat box and so on. I am simply wondering if I should use jtextarea instead of jtextfield if someone wants to write a long message since jtextfield does not allow word wrap.
Does it matter if I use jtextarea of jtextfield or is there a specific reason not to use jtextarea as my input box?
My guess:
You'll probably need both a JTextField and a JTextArea (or other multi-line text component).
The JTextField would be for the user to type in their chat messages to be sent. It would be editable.
and the JTextArea would show the incoming chats as well as the chats the user has sent. It would not be editable.
Consider placing the JTextArea in a JScrollPane
Consider placing both in a BorderLayout-using container with the JScrollPane that holds the JTextArea placed in the CENTER position and the JTextField in the SOUTH position.
A JTextField is a little easier to work with. You can add an ActionListener to it so that when the user hits enter the Action can be invoked an the message will be sent. Of course you can still have a "Send" JButton. the user can click on as well.
With a JTextArea, the Enter key will add a new line, so it you want the user to be able to sent the text when Enter is used you will need to customize the processing, maybe by using Key Bindings. I would prefer the text area because I like to see as much of the text as possible, like I am doing now as I repond to this question.
From oracle docs and personal experience, the JTextField makes life a lot easier as the ActionEventListener is set by default so the actions(characters or text) set in the field are copied to the JTextArea which is like a log of activity carried out in the field.
So the ball is in your court. You can use it JTextField together with JTextArea or JTextArea alone and write the listener class to suit you purpose and certainerly added a listenable event e.g a button.
Besides, always specify the size of the field to avoid layout issues that may compromise the Ui of your application as there is always a dynamic resizing if the size is not specified out rightly.

Hotkeys creation for java swing form

How to Create hot keys for form which is made by java swing?for example the form is create for student details means if I press ALT+N means the cursor will goto that name entering field.ALT+R means the cursor will goto Reg.no entering Field.same like as mark1(m1),mark(2) and so on.At that same time the form contains save,exit buttons if I press CTRL+S means the Save button will select.CTRL+X means the exit button will select.how to do this?
See How to Use Key Bindings, then use that knowledge in conjunction with requestFocusInWindow().
CTRL+X means the exit button will select.
See also setMnemonic(char) for buttons and setAccelerator(KeyStroke) for menu items. Or more generally, constructing those controls using an Action that has the values configured.
Please refer the following link
http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html
It might be a good place to start.
means if I press ALT+N means the cursor will goto that name entering field
This is generally done by using JLabel/JTextField pairs. Something like:
JLabel firstNameLabel = new JLabel("First Name");
JTextField firstNameTextField(15);
firstNameLabel.setLabelFor( firstNameTextField );
firstNameLabel.setDisplayedMnemonic( 'F' );
panel.add( firstNameLabel );
panel.add( firstNameTextField );
Then using Alt-F will set focus on the text field.
The Key Bindings will be done automatically for you.

Categories

Resources