Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with keystrokes and JOptionPane  RSS feed

 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have registered a keystroke with JButton in the following way

KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0,true);
button.registerKeyboardAction(buttonListener, enter, JComponent.WHEN_FOCUSED);

It worked fine....
when the button is clicked with enter key, iam doing some validations and accordingly dusplaying the error message using JOPtionPane in the following way

JOptionPane.showOptionDialog(this, "Invalid Filed", "error", JOptionPane.OK_OPTION, JOptionPane.ERROR_MESSAGE, null, ok, new String[]{"ok"});

the dialog will open.......here comes the problem
when the user clicks the enter key for "ok" button in option dialog...it is not closing at all....

i.e. The JoptionDialog is closing and opening immidiately...

I need help in resolving the problem

thanks

Note: Iam using Default Look and Feel




 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need some suggestions in solving this problem....

The JOptionPane is not closing at all with the enter key, keep on pressing again and again..
but when i use the spacebar the optionpane is closing....
let me know the solution for this problem...
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> let me know the solution for this problem...

your problem description can be produced numerous ways, so to avoid guessing,
post a small program that demonstrates the problem, so we can see exactly what you're doing.

just a frame with a single textfield and button and your validation/error optionPane

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of

try

 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use your own buttons in JOptionPane you have to call optionPane.setValue(value);

 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply

here is the sample code




Case:
Press Enter key (continuously) when the name filed is empty....



 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have posted the code...
can any one suggest me in solving the roblem
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I have posted the code...
> can any one suggest me in solving the roblem

not everyone is in your timezone (its 5.30am now, where I am)

//KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0,true);
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0,false);
 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried, but it didnot worked...
 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Mikko Kohtamäki.... I have tried modifying the code this way , but it didnot worked

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I have tried, but it didnot worked...

the change worked fine for me in the sample you posted,
so if it doesn't work in your main program, you have omitted some code.
 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is wat i have done......can you tell me where did went wrong

 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well now that I understanded your problem (when pressing ENTER in option dialog it is closed then action gives the focus for text field and ENTER is still pressed so the dialog opens again)

I wroted some code and added 'long performed' variable for actions, there must be better solution for this but right now I didn't know what else

 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried this, but the problem is...... the opened dialog is not closing with the enter key...
 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well I use WindowsLookAndFeel.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
don't know what you're trying to do with the pane.setVale(..) stuff,
but the original code works fine (after changing the keystroke parameter to false)

 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know and wont remember if I tried it, but 'false' means on press of ENTER and if you close this option dialog with press of ENTER and give focus for the field then (ENTER is down) and then field fires again and dialog is shown again.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the false parameter is related to keyRelease (true).
so when true, keyPress closes the dialog, textfield regains focus,
then the optionPane's keyRelease is processed by the textfield - so it shows the optionpane again
 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for the reply..
----------
Well I use WindowsLookAndFeel.
----------

I have already said in a note that iam using Default Look and Feel...
Is there any other way of solving it

-------
don't know what you're trying to do with the pane.setVale(..) stuff,
but the original code works fine (after changing the keystroke parameter to false)
-------
Mr Michael Dunn. I have tried changing the keystroke parameter to false....it didnot worked for me...did you do any other modifications to the code?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Mr Michael Dunn. I have tried changing the keystroke parameter to false....it didnot worked for me...did you do any other modifications to the code?

added a column count to the textfield (should have no effect on the problem).
removed grouplayout, just used the default (flowlayout/borderlayout, again should have no effect,
but I've never used grouplayout, so not 100% sure)

here's the code that works - run, click into textfield, press enter etc

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!