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.
Related
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);
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.
So im creating a server, and that works great, however I am a bit stuck on the GUI. You see, I would like it to look just like Command Prompt, where only the next line is editable, and it does not let you delete any other text. So right now I have:
JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta);
Then the frame stuff...
f.setTitle("Server");
f.setBounds(ss.width - 600, 50, 550, 350);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//added window listener so closes socket connection first
f.setAlwaysOnTop(true);
Then adding it:
f.add(sc);
jt.setBackground(Color.BLACK);
jt.setForeground(Color.WHITE);
//jt.setEditable(false);
Finally, the method I use to output to the TextArea:
public static void append(String text) {
jt.append(text);
jt.append("\n\n"+System.getProperty("user.name")+" / "+getIp()+" > ");
jt.setCaretPosition(jt.getDocument().getLength());
}
Now I need to assign a String to what the user types into the JTextArea after they press enter:>?
jt.addActionListener(...{
public void ActioEvent(ActionEvent e){
String text = JTextArea.getLines().getLastLine().getText().replace(System.getProperty("user.name")+" / "+getIp()+" > ", "");
}
});
Maybe something like that?
Then I need to have it so that only the part after the ">" is editable?
The way to do this is with a DocumentFilter. This is a fairly obscure and little-used part of Java, and is far from easy to use. However it allows you to insert a DocumentFilter between the UI (where rich text content is edited) and the underlying model (the content). You pass all the 'insert' and 'remove' operations through the filter, which can either accept, refuse or modify them. You can code the filter to only permit modifications to the command line, and not to the prompt.
As I say, this is a pretty hard piece of coding, and the Document/DocumentFilter structure has a lot of complexity that your particular application doesn't need. However it does provide you with the facilities you need.
There is a tutorial in the standard Java doc pages, but not an advanced one, and very few examples that I know of are out there on the web.
ProtectedTextComponent (thanks camickr) provides an example of how to do something similar.
Use a Collection a JTextField.
Let the user type on a JTextField, and once he presses enter, move the control to the next JTextField while making the above JTextField uneditable and also remove the JScrollPane from it.
Hope this helps.
I also agree that the JTextArea/JTextField approach is the common and simpler approach.
However if you want to complicate your life a little then you can check out Protected Text Component which will do most of the logic for you.
The current implemtation of the ProtectedDocument only allows you to add protection to the Document, not remove it so the first thing you will need to do is add a method to "Clear" all the protect pieces of text. This is easy enough, you just clear the entries in the Map used by the class.
Next you will need to replace the default "Enter" Action used by the JTextPane. You do this by playing with the Key Bindings of the text area. See Key Bindings for some basic information. In your custom Action you would first need to invoke the newly created "clear(...)" method. Then you would add you text to the text area. Finally you would protect all the text but the last "x" number of characters.
I used to make my UI by making declaring new JTextField in a for cycle while attaching an action listener by anon class to each JTextField, which means that you have to press enter fire an event which will read the text of the field and put it into an array here is the code
Getting data from JTextField that is one of several local variables without a few minor changes . Now I have to modify it to so that i press a button like Apply in order to have values written into an array. While I found two ways to do this wonder what is the optimal way to do this .
The horrible way . Create an array that acts as temporary storage ,
replace ActionListeners with DocumentListeners that will place
the values into this temp array . And a button that will on press
iterate through the the temp array placing its values into the
target array.
A better way which I found while searching , create a JTextField
array as public and simply have a button that will on press iterate
through the JTextField array and place its values into the target
array.
Adder is an example that maintains a List<JFormattedTextField> to enforce formatting. It uses a PropertyChangeListener & FocusListener to update on navigation events, such as the default key binding to Tab and Shift-Tab.
I have a list of textfields, and a list of buttons. These are laid out on a form with the buttons next to the textfields. Let's say there are 5 of each, they are 1 through 5. In reality, the number will be created at runtime.
When the user clicks a button, a new form is opened, which guides the user through creating a string. When they finish on that form, a text string is written in to the text field next to the button.
I'd like to create a relationship between each button and text field so that I know which text field to write in based on which button was clicked.
Is there a Java standard here? If not, any suggestions appreciated.
Thanks
Well either wrap or extend JButton to add a JTextfield field. Upon creation of the Button, pass the associated textfield to it and then you can pass along that info.
I'd like to create a relationship between each button and text field
Create an ActionListener class that takes the text field as a parameter. Something like:
JTextField textField = new JTextField();
JButton button = new JButton(...);
button.addActionListener( new FormPopupListener( textField ) );
Then you can save the text field as a variable in your listener class and when the dialog closes you can update the text field.
You have two lists, why not use the index in the list? Button at index 0 relates to textfield at index 0 and so on. Another alternative would be wrap them in an object that contains the button and the textfield and run a single list.
I would use the second route but there is nothing wrong with just using the index of the list.
Basically you can subclass JButton with additional attribute index. You can populate the index when creating the buttons and it will refer to index of text field in array of text fields. If you want you can even have a name given to each text field and add them to a map and then give the name to button to lookup the required text field.