I want to traverse horizontally through a JTable, when pressing enter. I've tried with JTable.changeSelection but it doesnt seem to work. Any ideas how to change the traversal behavior?
Read up on Key Bindings. You would want to "share an Action with a different KeyStroke"
Enter is vertical traverse , TAB is horisontal, just hold Enter event and generate Tab event or call function for Tab Event. But you should set up next properties:
table.setSelectionMode(
ListSelectionModel.SINGLE_SELECTION);
JTable.getInputMap(JInternalFrame.WHEN_ANCESTOR_OF _FOCUSED_COMPONENT)
.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "SOME_ACTION");
JTable.getInputMap(JInternalFrame.WHEN_FOCUSED)
.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "SOME_ACTION");
JTable.getActionMap().put("SOME_ACTION", actions);
actions = new AbstractAction() {
public void actionPerformed(ActionEvent ae) {
//This action will get fired on Enter Key
}
};
Related
I use mouse to drag a selection area in some JTable cells, the selection area is the yellow, so can anyone told me exactly how to clear the contents of selected cells by pressing "Delete" key in keyboard or a JButton?
The captured pic of selected cells:
Create an Acton to find the selected cells and clear the text. The easiest way is to loop through each cell in the table.
The basics of the Action would be something like:
Action clearAction = new Action()
{
#Override
public void actionPerformed(ActionEvent e)
{
for (each row in the table)
for (each column in the row)
if (table.isCellSelected(...))
table.setValueAt("", ...);
}
}
Then you create a button to invoke the Action:
JButton clearButton = new JButton( "Clear" );
clearButton.addActionListener( clearAction );
If you also want to use the Delete key then you can use Key Bindings to share the same Action.
The basic logic to add a new Key Binding to the JTable would be:
String keyStrokeAndKey = "DELETE";
KeyStroke keyStroke = KeyStroke.getKeyStroke(keyStrokeAndKey);
table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(keyStroke, keyStrokeAndKey);
table.getActionMap().put(keyStrokeAndKey, action);
Check out Key Bindings for more information.
In SWT, we can set the tab oder by using the code below:
composite.setTabList(new Control[]{button1, button3});
Is there any way to change the key traversal from Tab to Enter? My application connect to a bar code reader and it just support Enter key after read the barcode
There is a TraverseListener in SWT that can be used to change the effect of traversal keys.
For example, a traverse listener can be used to focus the next field on Enter like this:
text.addTraverseListener(new TraverseListener() {
#Override
public void keyTraversed(TraverseEvent event) {
if (event.detail == SWT.TRAVERSE_RETURN) {
event.doit = false;
// focus next control
}
}
});
Setting the doit flag of the event to false consumes the event and prevents it from causing the default action - if any. In a multi-line text field, the Enter key may begin a new line unless the event was consumed by a listener.
so I have this part of my code (I cannot post everything because it's just too long and so far this is the only problem with it). Our professor assigned us to make our own Assembler just like MARIE and we are having a trouble with these lines of code:
else if(get.charAt(0)=='B')//input
{
inputfield.setEditable(true);
//INSERT LISTENER HERE!
AC.setText(inputfield.getText());
System.out.println(""+col);
//insert action here - HALP
}
The entire thing gets a value from a table sort of like an Instruction in Hex and if the Intruction starts with B like B000 then it will toggle the input textbox which is named inputfield. It works fine but we need to add a key listener in the part where it says //INSERT LISTENER HERE! for when the user will press enter the AC.setText(inputfield.getText()); will be executed. How should we do it? I mean we tried actionListener but it sort of stops the loop unless another button is clicked. We need another way that when the user presses enter it automatically resumes the execution.
Thank you.
Add action listener to textfield. ActionEvent will occur when you press Enter while editing in textfield.
JTextField field = new JTextField();
field.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// action to perform when one hits "Enter"
}
});
I have to shift the cells up/down depending upon the up/down arrow key pressed from the keyboard. I am adding a KeyListener (in fact KeyAdapter) on the JTable to achieve it using the keyPressed() method. Now what's happening is that when I am pressing the alphanumeric keys, then I am able to get the selected row using table.getSelectedRow(), but when I am pressing the arrow keys its giving me "-1" always. That means no row is shown selected. I also tried to set the focus on table but it did not work. The code I am usin is as follows:
private void settingKeys() {
controller.getStandardActionDetailsTableEquates().addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e) {
System.out.println("key pressed!!");
controller.getStandardActionDetailsTableEquates().setRowSelectionAllowed(true);
int selectedRow = controller.getStandardActionDetailsTableEquates().getSelectedRow();
System.out.println("selectedRow : " + selectedRow);
controller.getStandardActionDetailsTableEquates().requestFocusInWindow();
if (e.getKeyCode() == KeyEvent.VK_UP) {
System.out.println("up arrow key pressed");
Component editor = controller.getStandardActionDetailsTableEquates().getEditorComponent();
editor.requestFocusInWindow();
System.out.println("cursor : " + controller.getStandardActionDetailsTableEquates().getCursor());
System.out.println("value : " + controller.getStandardActionDetailsTableEquates().getSelectedRow());
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
System.out.println("down arrow key pressed");
}
}
});
}
I even tried to get the cursor position, but could not get it. Also, the editor found did not worked (shown in code). The "selected row" and "value" values are pining as "-1".
Please provide solution to this.
Don't use a KeyListener; use Key Bindings. JTable has the following default bindings in the WHEN_ANCESTOR_OF_FOCUSED_COMPONENT input map:
VK_UP has the key "Table.selectPreviousRow"
VK_DOWN has the key "Table.selectNextRow".
You can replace the bindings with Action instances that update your TableModel as desired. This example may guide you. A complete listing can be obtained using the key binding utility cited here. Implementations may be found in the table's UI delegate, BasicTableUI, et al. The EditorKit actions, examined here, are also worth a look. Alternatively, consider a custom Comparator in your RowSorter, as shown here.
I have a JTextField represents a day in a week, such as "Friday", when I click on it, I want to have a choice such as "1st of month, 3rd of month or last of month", so I came up with two options :
<1> Hold down a number or letter, let's say "2" or "L", then click on "Friday" means 2nd (or last) Friday of the month, in this case, how to get the number while mouse clicks on the JTextField ?
<2> Right mouse click on the "Friday" JTextField, drop down a menu, with either buttons or checkboxes that let me choose, then close the menu and get the value.
My code look like this so far :
private final JTextField[] dayHeadings=new JTextField[]{new JTextField("Su"),
new JTextField("Mo"),
new JTextField("Tu"),
new JTextField("We"),
new JTextField("Th"),
new JTextField("Fr"),
new JTextField("Sa")};
......
for (int ii=0; ii < dayHeadings.length; ii++)
{
dayHeadings[ii].setEditable(false);
dayHeadings[ii].setFocusable(false);
dayHeadings[ii].addMouseListener(new MouseAdapter() { public void mouseClicked(final MouseEvent evt) { onHeadingClicked(evt); } });
add(dayHeadings[ii],new AbsoluteConstraints(x,38,X_Cell_Size+1,Y_Cell_Size+1));
}
......
void onHeadingClicked(final java.awt.event.MouseEvent evt)
{
final javax.swing.JTextField fld=(javax.swing.JTextField) evt.getSource();
...
}
How to do either of the above, are they doable in Java ?
getModifiers is actually that what I needed. a sample for the modifiers can be found
here
Option 1:
There is no way to do this in one step. You would need to add a KeyListner to track whenever a key is pressed and then save the character value. Then you would need to add a MouseListener to listener for mousePressed events. When the mousePressed event fires you would need to to check which character is saved and then do your processing. Therefore your listener would to implement both the KeyListener and MouseListener interfaces.
Option 2:
You need to add a mouse listener and listen for a right mouse click, then display a popup menu.
I think option 2 is more intuitive and more easily done. Its always easier to work with one hand then be forced to use two hands.
Another, lazier way to do it would be using getModifiers() on the mouseclick event. It shows which modifier keys (ctrl, alt, shift, etc), if any, were pressed during the mouse click. Using these buttons isn't as intuitive as a drop down menu or numbers in my opinion, but could work.
Read more here