Possible to have JList navigation keystrokes while JTextField hasfocus.

D

Daniel Pitts

I have a JTextField which the user can enter data, and this affects the
state of a JList.

I'd like the user to be able to type in the JTextField, but then press
the up-arrow or down-arrow to adjust the current selection.

What would be the best approach? I can listen for the up/down keystrokes
myself, and adjust the selection myself, but that seems to be a
short-cut hack that might lead to inconsistent behavior.

Is there another approach?

Thanks,
Daniel.
 
J

John B. Matthews

Daniel Pitts said:
I have a JTextField which the user can enter data, and this affects the
state of a JList.

I'd like the user to be able to type in the JTextField, but then press
the up-arrow or down-arrow to adjust the current selection.

What would be the best approach? I can listen for the up/down keystrokes
myself, and adjust the selection myself, but that seems to be a
short-cut hack that might lead to inconsistent behavior.

Is there another approach?

You might look into key bindings:

<http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html>

I was intrigued to find that some bindings are Look & Feel dependent:

<http://tips4java.wordpress.com/2008/10/10/key-bindings/>

In particular, on Mac OS X, caret-begin-line and caret-end-line bind to
{UP, HOME, ctrl-A, ...} and {DOWN, END, ctrl-E, ...}; on Motif and
Metal, they bind to {HOME} and {END}.

You should be able to use UP/DOWN in a JTextField without any adverse
effects.
 
F

Fred

You might look into key bindings:

<http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html>

I was intrigued to find that some bindings are Look & Feel dependent:

<http://tips4java.wordpress.com/2008/10/10/key-bindings/>

In particular, on Mac OS X, caret-begin-line and caret-end-line bind to
{UP, HOME, ctrl-A, ...} and {DOWN, END, ctrl-E, ...}; on Motif and
Metal, they bind to {HOME} and {END}.

You should be able to use UP/DOWN in a JTextField without any adverse
effects.

You could try adding menu items that, when selected,
cause the list selection to change.
Then set the arrow keys as the accelerators -
assuming that the arrow keys are not needed for
any other widgets in the dialog. If the arrow keys ARE
needed elsewhere, you could use Ctrl+Up, etc., as the
accelerator.
 
M

Michael Rauscher

Daniel said:
I have a JTextField which the user can enter data, and this affects the
state of a JList.

I'd like the user to be able to type in the JTextField, but then press
the up-arrow or down-arrow to adjust the current selection.

What would be the best approach? I can listen for the up/down keystrokes
myself, and adjust the selection myself, but that seems to be a
short-cut hack that might lead to inconsistent behavior.

Is there another approach?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test {

private void createAndShowGUI() {
final JList list = new JList(new String[]{"A", "B", "C"});

InputMap im =
list.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0),
"selectNextRow");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0),
"selectPreviousRow");
list.setInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, im);

JTextField textField = new JTextField(20);

JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(new JScrollPane(list));
frame.add(textField, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}

public static final void main(String args[]) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Test().createAndShowGUI();
}
});
}
}

HTH
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top