Switch selection between button and text field - java

I'm writing a piece of software in Java, with a GUI programmed using swing. The user has to do this two operations in a loop:
write some words in a text field (let's call it A) and press Enter
click a button (let's call it B)
After Enter is pressed, some other fields show some data. After the button is clicked, all text fields are cleared and the procedure restarts. The text field to fill in is always A and the button to click is always B. This is what I would like to implement:
After the text field is filled and Enter is pressed, the button is somehow selected so that if the user presses Enter again the button is clicked
After the button is pressed, the text field is selected and in "write mode", so that the user can immediately start typing
What is the easiest method to implement this? Below I'm reporting what I have already tried.
I tried this to make the button selected immediately after Enter is pressed in the text field:
this.getRootPane().setDefaultButton(ButtonNextQuestion);
but when I press Enter, it is registered both by the text field and the button and I cannot see intermediate data.
I tried also with these lines:
ButtonNextQuestion.requestFocus();
ButtonNextQuestion.setSelected(true);
in this case everything seems working, but when I press enter the button is not clicked.

Thanks to AdamK!
The focus to the button was given like this:
ButtonNextQuestion.requestFocus();
ButtonNextQuestion.setSelected(true);
I had to add a listener to the button like this:
private void ButtonNextQuestionKeyPressed(java.awt.event.KeyEvent evt){
if(evt.getKeyCode() == 10) {//ENTER pressed: click
ButtonNextQuestion.doClick();
}
}
Then again to automatically select and make the text field ready to be written I used:
TextYourAnswer.requestFocus();

Related

Change button color after updating text field

There are 20 JTextField and one calculate button in panel.
after entering data to text field and I click button, program calculate as well.
What I want to do, after first click for button, is there any updates in any text fields, button colors should be change as green (it means you have to know something already change and you have to calculate again).
How can I proceed with that, should I put action listener for each text field or any other short code exist?
Your question is not clear. As far my concern to yr question you can track button click event and put code there for check jtextfield value and on the bases of value you can take decision to change the color of the button

Enter key on JFrame

So what I've been attempting to do is essentially have it set to when the enter key is pressed on my JFrame in java it detects it so I can do an action like start a game by switching to the next screen. So I was wondering if anyone could help me figure out how to do this in particular. Just being able to have something that constantly checks to see if the Enter Key has been pressed on the JFrame
have it set to when the enter key is pressed on my JFrame in java it detects it so I can do an action like start a game by switching to the next screen.
How do people know to use the Enter key?
Typically you would have a button like "Start Game" for the user to click.
You can easily make this the default button by using:
frame.getRootPane().setDefaultButton( startGameButton );
Now the user will be able to either:
click on the button
use the Enter key to invoke the button.

How do you detect when a user commits a JComboBox selection via mouse click in Java Swing?

I've spent over a day now trying to figure this out. I've tried MouseListener, ItemListener, and ActionListener examples I've found, but none seem to do what I need.
Here's what I've got: I've got a JComboBox that works well. The user can type in the text box and it autocompletes, and there's a drop-down and everything. The purpose of the box is for performing searches. The user types a search and hits enter, which causes the search results to be highlighted in the main pane. The user can also arrow through the drop-down and initiate the result highlight by hitting enter from the dropdown.
Here's the problem: The user can click an item in the dropdown under the text field and the text field fills in with that value, but the search results are not highlighted. The user still has to hit enter after having clicked what they want.
Here's what I want: I want to detect the moment the user clicks the dropdown menu item they want so that I can use that event to call my method which highlights the result in the main pane. I don't want them to have to hit the enter key after clicking an item in the dropdown.
I've got a key adapter that catches the enter-key press to call the method which highlights results and it works well. The method it calls is called seekAll().
When I tried the mouse listener, it only seemed to work on the text field and not the dropdown. When I tried the ItemListener, I could not distinguish between items being highlighted/hovered/arrowed across and the click making the drop-down menu selection. The result of the action listener was similar to my attempt with the ItemListener.
Here's an example of the ActionListener I tried:
public abstract class HeaderFinderBox {
private WideComboBox searchTermBox;
public HeaderFinderBox() {
final String[] labels = setupData();
//just returns an array of strings
this.searchTermBox = GUIFactory.createWideComboBox(labels);
searchTermBox.setEditable(true);
searchTermBox.setBorder(null);
searchTermBox.setBackground(GUIFactory.DARK_BG);
AutoCompleteDecorator.decorate(searchTermBox);
//This adds my key listener
//searchTermBox.getEditor().getEditorComponent()
// .addKeyListener(new BoxKeyListener());
searchTermBox.addActionListener(new SearchListener());
}
class SearchListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
LogBuffer.println("Action performed on item from the combo box");
//seekAll();
}
}
}
I commented the call to seekAll() because I get null pointer exceptions when the you type your first character into the text box and the field blanks. With that call commented out, I get the "Action performed on item from the combo box" message whenever these things happen:
Twice for each character typed on the keyboard
Once when you hit enter
3 times when you click an item in the dropdown
3 times after arrowing to an item in the dropdown and then hitting enter
The only one of these that I want a single hook for doing something is 1 firing upon the user clicking an item in the dropdown. That would be number 3 in the list above, but it fires 3 times for a single click. And how do I differentiate that event from all the others (1, 2, & 4)?
If ActionListener is supposed to be able to ONLY give me a single actionPerformed upon click of an item in the dropdown, then what am I doing wrong?

Have edittext show different keys than what was pressed

Im trying to have an edittext which would prompt a keyboard to pop-up when the user selects the edit text. When the user starts typing, it should fill the edittext with the keystrokes that the user puts in... but once a boolean value changes, then the keystrokes that get put will be different than what the user puts in.
If the boolean value is changed, then typing any key in the edittext updates will update, and lets say a "x" gets put in place instead of pressing "z".
The way that I can see about cracking this would be a lengthy keyUp() method that includes ALL keys and puts in their "x" when they get pressed if the boolean is true. Are there any methods or suggestions as to what I could do insead of the lengthy keyUp method. The keyup method doesn't work with
The boolean method that would change would start when a "." gets pressed in the edit test.
Take a look at the TextWatcher class. It will allow you to intercept and manipulate user inputs as appropriate (depending on your boolean flag's value) as each character is entered. You can set a TextWatcher on an EditText using the addTextChangedListener method.

To detect the paste event in addressbar in gwt

How to detect that pasting event is fired in the address bar through GWT? When we paste the thing from textbox to the addressbar then how to detect that pasting event in gwt.
What if a user decides to type an address instead of pasting it?
A standard approach is to use OnValueChangeEvent on a Textbox. It fires when a Textbox loses focus AND a value in the Textbox has changed, or when the Enter button is pressed while focus is still on the Textbox.
An alternative approach is to add a button, like "Take me there", that a user can press after a user is done entering the URL (either by pasting it or typing it).

Categories

Resources