Mouse cursor change once ENTER pressed - java

I have an image here to explain my query
As you can see in the image I have many text fields,right now the cursor is on the text field(the cursor could be on any textfield).
As you can 3 out of the last 4 fields are disabled,once I press the enter those get enabled.This everything works fine.
My query is once I press enter how do I move my cursor to the position down there(marked in red)?
This is the small snippet of code for the once enter key pressed.
((JPanel)frame.getContentPane()).getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("ENTER"), "doSomething");
((JPanel)frame.getContentPane()).getActionMap ().put("doSomething", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("profit");
// disabledField.setEnabled(true);
textbox9.setEnabled(true);
textbox10.setEnabled(true);
textbox11.setEnabled(true);
}
});

You can request focus by using JComponent#requestFocusInWindow
For example...
public void actionPerformed(ActionEvent arg0) {
System.out.println("profit");
// disabledField.setEnabled(true);
textbox9.setEnabled(true);
textbox10.setEnabled(true);
textbox11.setEnabled(true);
textbox9.requestFocusInWindow();
}
Take a look at How to use the Focus Subsystem for more details

Related

Listener on JTextField to detect completion of data entry

We are developing an application in Java Swing.
We have a text field and a combo box. The combo box values should be populated from DB based on the value entered in the text field.
The text field length is 4 characters. So, user can enter any value between 1 to 9999.
Which listener would be used to identify that the user has completed their entry in the text field so that I can populate the combo box?
..the user has completed his entry in the text field so that i can populate the combobox.
An ActionListener. When the user is done, they hit the enter key and an action event will be fired. But..
..user can enter any value between 1 to 9999.
This sounds better suited to a JSpinner using a SpinnerNumberModel and a ChangeListener to register selecting a different number.
Use the textField.getDocument().addDocumentListener(new DocumentListener()
textField.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
//TODO
}
public void removeUpdate(DocumentEvent e) {
//TODO
}
public void insertUpdate(DocumentEvent e) {
//TODO
}

Editable JCombobox avoid multiple DocumentEvents when the selection change from the popup

I have an editable JComboBox with a single listener on it.
It is a documentListener that execute some code when the user insert or remove some text inside the combobox textfield:
((JTextComponent)combobox.getEditor().getEditorComponent()).getDocument().addDocumentListener(..)
My problem is that when the user select an element from the popup and the content of the combobox textfield changes there are two events executed into the documentListener, one is a removeUpdate() corresponding to the deletion of the previous content and the other is a insertUpdate() corresponding to the insertion of the new value.
I want that only one execution of my code is done and not two. How can I avoid that the code is executed two times when the user select an entry from the popup?
I tried various combination of different listener but for now without result.
What I want in the end is that my code is execute only one time when:
- The user change the text into the combobox textfield.
- The user select an element from the combobox popup
Thanks in advance.
[EDIT 1]
As requested I updated adding SSCCE
myCombobox = new javax.swing.JComboBox<String>();
myCombobox.setEditable(true);
((JTextComponent)myCombobox.getEditor().getEditorComponent()).getDocument().addDocumentListener(
new DocumentListener(){
#Override
public void insertUpdate(DocumentEvent e) {
System.out.println("insert performed");
}
#Override
public void removeUpdate(DocumentEvent e) {
System.out.println("remove performed");
}
#Override
public void changedUpdate(DocumentEvent e) {
System.out.println("change performed");
}
});
myCombobox.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED) {
System.out.println("Action performed");
}
}
});
Note that in this case I have an ItemEvent instead of an ActionEvent because I'm continuing to modify my code searching for a solution in any case the behavior should not be influenced by this.
You can check ((JTextComponent)combobox.getEditor().getEditorComponent()).hasFocus() to be sure user types in the editor.

Editable JComboBox with dynamic entries

I made my JComboBox editable using:
myCombo.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
I encounter multible problems with the following task:
When writing into the combobox, the new content of the box should be taken and compared with a list and all entries starting with this text should be shown in the popupmenu.
So if i have a list with: "Aban" "Aben" "Aber" "Acen" "Aden"
and enter "Ab" into the box, then the pop-up should display the first 3 entries.
When clicking one of the entries (Either by keyboard selecting and pressing enter/tab or by clicking with a mouse) The ComboBox should get that value and the Popup should hide. I need to find this action as some of the elements have a note at the end (In backets which I require) but only when one of the entries is selected
Here are the most imporant parts of my code:
final JTextComponent tcA = (JTextComponent) myCombo.getEditor().getEditorComponent();
tcA.getDocument().addDocumentListener(new DocumentListener() {
public void methodUsedByinsertUpdateAndremoveUpdate(DocumentEvent e) {
String item = ((JTextComponent) myCombo.getEditor().getEditorComponent()).getText();
//Routine to get the new list in a vector, not pasted for readability
DefaultComboBoxModel newMyComboModel = new DefaultComboBoxModel(myVectorList);
myCombo.setModel(newMyComboModel);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
myCombo.showPopup();
}
});
}
}
myCombo.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(myCombo.getModel().getSize() == 1) {
//Special logic to find out if the selected item has a note
SwingUtilities.invokeLater(new Runnable() {
public void run() {
myCombo.hidePopup();
}
});
}
}
With this, i have:
Trouble with the first character (Caret position not working correctly)
Popup not automatically shown and hides when entering new character into the field
Problems with Swing GUI not being actualised
If you require more information just ask
The Glazed List recommended by peeskillet did exactly what I wanted

JtextArea cursor position after keypress of ENTER

Guys weird problem im facing
Basically i have 2 textAreas... (diplaybox and textbox)
while typing in textbox, the moment "Enter" is pressed i want all the text entered in textbox to go to displaybox... and textbox should be empty...
it all works fine, except...
after the text is transfered the cursor position of the textbox isnt on the topmost left side... it is somehow blinking on one line below that!(possibly because "ENTER" still got excecuted)... please see code
any ideas?
thanks in advance... just need the cursor to go back to the topmost left like how it is when we start typing ... without having to use KeyReleased event... something isnt feeling right... im sure this isnt he way it is actually done.. what say?
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_ENTER)// | (e.getKeyCode() == KeyEvent.VK_B))
{ //Toolkit.getDefaultToolkit().beep();
displaybox.append(textbox.getText() + "\n");
//textbox.setCaretPosition(0);
//textbox.setText("");
System.out.println(textbox.getCaretPosition());
}
}
public void keyTyped(KeyEvent e)
{}
public void keyReleased(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_ENTER)
{textbox.setCaretPosition(0);
textbox.setText("");
System.out.println(textbox.getCaretPosition());
}
}
All Swing components work by using Key Bindings. The default binding for the Enter key is to add a newline string to the text area. If you want to change the functionality of the Enter key then change the default Action. Don't attempt to use a KeyListener.
Check out Key Bindings for a program to list all the default bindings as well as a link to the Swinng tutorial on How to Use Key Bindings. If you run the program you will find that the Enter key invokes an Action identified by the "insert-break" tag in the ActionMap. So to replace the Action you can do something like:
Action enter = new AbstractAction()
{
#Override
public void actionPerformed(ActionEvent e)
{
displayBox.append( textBox.getText() + "\n" );
textBox.setText("");
}
};
textBox.getActionMap().put("insert-break", enter);
The problem with using the KeyListener is that the default Action is still invoked AFTER you process the KeyEvent.

Change behavior of JTable key actions

I have a JTable with editable cells. When I click in a cell, it enters edit mode; the same happens when I'm moving through cell using the directional arrows.
Now I want to select the cell instead of start editing, and edit the cell only when the Enter key is pressed.
If any other information is needed, please just ask for it.
Edit: Action for Enter key
class EnterAction extends AbstractAction {
#Override
public void actionPerformed(ActionEvent e) {
JTable tbl = (JTable) e.getSource();
tbl.editCellAt(tbl.getSelectedRow(), tbl.getSelectedColumn());
if (tbl.getEditorComponent() != null) {
tbl.getEditorComponent().requestFocus();
}
}
}
Now this is for left arrow action the rest of 3 are not hard to deduce from this one:
class LeftAction extends AbstractAction {
#Override
public void actionPerformed(ActionEvent e) {
JTable tbl = (JTable)e.getSource();
tbl.requestFocus();
tbl.changeSelection(tbl.getSelectedRow(), tbl.getSelectedColumn() > 0 ? tbl.getSelectedColumn()-1:tbl.getSelectedColumn(), false, false);
if(tbl.getCellEditor()!=null)
tbl.getCellEditor().stopCellEditing();
}
}
And this is how you bind this actions:
final String solve = "Solve";
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(enter, solve);
table.getActionMap().put(solve, new EnterAction());
final String sel = "Sel";
KeyStroke arrow = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0);
table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(arrow, sel);
table.getActionMap().put(sel, new LeftAction());
Oh,i almost forgot,to select the cell instead of edit on Mouse Click:
public static MouseListener mAdapterTable = new MouseListener()
{
#Override
public void mousePressed(MouseEvent e)
{
JTable tbl=((JTable)e.getComponent());
if(tbl.isEditing())
{
tbl.getCellEditor().stopCellEditing();
}
}
#Override
public void mouseClicked(MouseEvent e) {
JTable tbl=((JTable)e.getComponent());
if(tbl.isEditing() )
tbl.getCellEditor().stopCellEditing();
}
#Override
public void mouseReleased(MouseEvent e) {
JTable tbl=((JTable)e.getComponent());
if(tbl.isEditing() )
tbl.getCellEditor().stopCellEditing();
}
};
The EventListner must be added to table like so:
table.addMouseListener(mAdapterTable);
Use Key Bindings for this. Most Look & Feel implementations already bind F2 to the table's startEditing action, but you add a different binding:
tree.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "startEditing");
This will effectively replace the previous binding of Enter to the table's selectNextRowCell action.
Here is what i would do:
First enable the single cell selection for the JTable
Create a KeyAdapter or KeyListener for the JTable or for the JPanel,
what contains your table.
In the KeyAdapter's keyPressed() method enter the edit mode of the
selected cell, something like this:
http://www.exampledepot.com/egs/javax.swing.table/StopEdit.html
You can check in the keyPressed() method, if the user pressed the right button for editing. I'm not sure, if the normal (double click) editing is disabled in your table, then what happens, if you try to edit it programmatically, but if it doesn't work, then you can enable the editing on the selected cell, when the user presses the edit button, then when he/she finished, disable it again.

Categories