I'm building a chat program. The user has the option to press a JButton SEND or just press ENTER on the keyboard to send the message. This is my code.
private void chatTextAreaKeyPressed(java.awt.event.KeyEvent evt) {
if(evt.getKeyCode() == KeyEvent.VK_ENTER) {
this.sendButtonActionPerformed(null);
this.chatTextArea.setText(null); // Clear JTextBox
}
}
The problem with this, is that after pressing ENTER, it sets the JTextBox with a empty new line. So that whatever I type next will always be on the second line instead of starting with an empty text box.
Anyone has any ideas? Much appreciated.
You need to consume the event with evt.consume() to ensure it isn't processed by the text field itself.
This indicates that all processing of the event has finished and no other listeners should act upon the event.
Related
I have the following Shell listener code
private class ShellListener extends ShellAdapter
{
#Override
public void shellClosed( ShellEvent e )
{
}
#Override
public void shellDeactivated( ShellEvent e )
{
}
}
I need to be able to trap when the Shell loses focus, ie: the user goes to another application. The shellDeactivated() does that. I also need to know when I explicitly close() the Shell. The shellClosed() does that.
However when a user clicks on the [x] icon at the top/right corner of the Shell, shellDeactivated() fires, then shellClosed() fires. I need to be able to ignore the shellDeactivated() when the [x] is clicked.
The ShellEvent does not have any pertinent information, it just holds the Shell object, not which Shell control initiated the event.
Is there any way I can trap for the [x] click?
Grumble grumble
Ok, inside shellClosed() I pop-up a message asking the user if they really want to quit. It seems that this is considered a lost focus event (true enough). That is what was firing the shellDeactivated() event.
So the shellClosed() fires, I show a pop-up message, which fires the shellDeactivated() event. And things happen out of order. A simple flag and back to normal :-)
I'm trying to make an interface for a login/register app and I have, in the email box (jTextField) an example as text (example#gmail.com) but when I run my program when I click that box to write my email on it, I have to delete my set text to write what I want.
What I thought to do was to create 2 jTextFields, the one behind not editable and the one forward where I'd put my text. So there are two things I don't know how to do:
put the forward jTextField invisible so we can see the behind
one
make the text on the behind jTextField disappear when I click the front one
Thanks for trying the help.
Can easily done with FocusGained and focuseLost events
private void txtEmailFocusGained(java.awt.event.FocusEvent evt) {
if (txtEmail.getText().equals("example#example.com")) {
txtEmail.setText(null);
}
}
private void txtEmailFocusLost(java.awt.event.FocusEvent evt) {
if ( txtEmail.getText().equals("")) {
txtEmail.setText("example#example.com");
}
}
I have a Form with various textboxes(say around 10) .After the user fills value in each textbox, it is validated on focuslost event for the textbox.
public void focusLost(FocusEvent e)
{
JTextField tf = (JTextField)(e.getSource());
String finalVal = tf.getText();
try
{
validate(finalVal);
}
catch(NmfException ex)
{
JOptionPane.showMessageDialog(parent, message, title,
JOptionPane.ERROR_MESSAGE);//Error Message is passed
/* Error pop up is displayed when validation fails. Message text with an 'Ok' button is displayed and the code waits for ok to be clicked to execute rest of the code*/
tf.setText(defaultVal);//Value is reset to default value
return;
}
}
The form has a 'Add' button which gets the values from the UI(from the textbox) and sends it to the server.Ideally, since the values are validated at each textfield the value sent to the server should be valid inputs.
But my issue is, when an invalid input is given to a textfield(say -5 an invalid input) and 'Add' button is clicked at once.
The focusLost event is triggered and the pop up is obtained,while the code waits for the 'OK' button in pop up to be pressed,the next event of button clicked is also called.So before the defaultVal can be set as textfield value,the Add button operation is done(there is no further validation in add operation) and invalid inputs are sent to the server.
How can ensure that Add operation is called only after the focusLost event operation is done.Please suggest a fix for the issue? What would be a best practice for such a scenario?
Set one Flag which should be check while click on 'Add'.
So if all validation should be true/OK then send to server.
if flag is false/invalid, while click on 'Add' then give user prompt
with error message.
As per your scenario if any one try to add invalid value then
focusLost event makes Flag -> false, and vice-versa.
Likewise need to design architecture of coding.
You could also use a mouse listener on the text fields, and validate in the mouseExited method
I have a textbox with attached ModifyListener.
In implemented modifyText(ModifyEvent e) I execute desired functionality.
The problem with that, that this event is triggered on every text change.
I don't want it to trigger if text was altered programmaticly (by setting text via code).
I want it to trigger only when user changes the code (I can't use keylistener because it will be triggered also when user click on arrow buttons and etc, it also won't detect if user copy&paste text)
You could unregister your ModifyListener before calling setText(..) and reregister it afterwards.
How about textBox.addKeyListener(...) and textBox.addMouseListener(...) instead of ModifyListener?
You can try using Focusout listener.... then you will get the value which user has entered only once.
Text text;
text.addListener(SWT.FocusOut, new Listener() {
#Override
public void handleEvent(Event arg0) {
//Your code here.....
}
});
I have a problem with the focus traversal system in Java. When I tab between components in a pane in my application everything works fine Tab moves the focus to the next component.
Some of my components perform validation on loss of focus, if the validation returns errors then the screens save button is disabled.
My problem occurs when the validated component is followed by the save button.
Tab removes focus from the validated component and begins the asynchronous process of assigning focus to the next component that is enabled (The Save Button)
Next my validation kicks in and disables the save button
The asynchronous process then finished and attempts to assign focus to the now disabled Save button.
The Focus now becomes trapped and tabbing no longer shifts focus because no component actually has the focus.
Has anyone else come across this problem, how did you solve the problem of having the validation and disablement carried out before the focus traversal event started?
You could use an InputVerifier to validate the text field. In this case focus will be placed back on the text field in error.
Or you could change your focus listener to handle this situation. Something like:
FocusListener fl = new FocusAdapter()
{
public void focusLost(final FocusEvent e)
{
JTextField tf = (JTextField)e.getSource();
if (tf.getDocument().getLength() < 1)
{
System.out.println("Error");
button.setEnabled( false );
Component c = e.getOppositeComponent();
if (c instanceof JButton
&& c.isEnabled() == false)
{
tf.requestFocusInWindow();
}
}
else
button.setEnabled( true );
}
};