473,480 Members | 1,849 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Adding KeyListeners to JOptionPane

118 New Member
I want to customise JOptionPane and add a KeyListener to it. I want to behave in a certain manner when the enter key is pressed , but from the look of things, the key listener I am adding to the JDialog is not working. The code is running and not giving errors but when it comes to printing the statements, when I press enter , nothing is happening .

Can anyone help on this one?

Expand|Select|Wrap|Line Numbers
  1. JOptionPane  pane=new JOptionPane("AAAA",JOptionPane.WARNING_MESSAGE,JOptionPane.YES_NO_OPTION);
  2.  
  3.          JDialog dialog = pane.createDialog(this, "AAAA");       
  4.          dialog.setVisible(true);  
  5.          dialog.addKeyListener (new KeyAdapter () {
  6.             public void keyPressed(KeyEvent ke){
  7.                 char c = ke.getKeyChar ();
  8.                  if ( (c == KeyEvent.VK_ENTER)) {
  9.                      System.out.println("Enter has been pressed");
  10.                  }
  11.  
  12.             }
  13.              public void keyTyped (KeyEvent ke) {
  14.                  char c = ke.getKeyChar ();
  15.                  if ( (c == KeyEvent.VK_ENTER)) {
  16.                      System.out.println("Enter has been typed");
  17.                  }                 
  18.              }
  19.  
Dec 3 '07 #1
3 7439
JosAH
11,448 Recognized Expert MVP
When you set a modal dialog to visible it blocks the current thread until its
execution terminates, iow: you're setting your keylistener after the party is over.

Why are you using such low level events anyway?

kind regards,

Jos
Dec 3 '07 #2
heat84
118 New Member
When you set a modal dialog to visible it blocks the current thread until its
execution terminates, iow: you're setting your keylistener after the party is over.

Why are you using such low level events anyway?

kind regards,

Jos
Will it maintain the modal nature when I create a dialog from the pane? What other options are there besides using the KeyAdapter or the KeyListener for event listening ?
Dec 3 '07 #3
heat84
118 New Member
Thank JosAh , I have discovered where I was missing it.
Dec 4 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
12826
by: - Steve - | last post by:
Example String testString = "Testing \t tabs"; JOptionPane.showMessageDialog(null, testString, "TEST", JOptionPane.INFORMATION_MESSAGE); Instead of getting the output "Testing tabs", I...
1
4831
by: jazzy | last post by:
Hi, I am new to Java Programming and I am trying to write code using JOption pane to prompt user to type information and then have string output command to display information on the screen. I...
2
3037
by: jek339 | last post by:
Hey, I'm totally new to Java, and I'm taking a class. My current assignment involves inputting 2 names and corresponding bdays for each name using the JOptionPane.showInputDialog etc. Anyway, I'm...
2
3329
by: ragaman123 | last post by:
Hello there, I am designing a battleships game in java and am trying to output to the screen a simple message when a user sinks a ship. I have been trying to use JOptionPane to output a message,...
0
1536
oll3i
by: oll3i | last post by:
import javax.swing.*; import java.awt.event.*; import java.util.ArrayList; import java.util.List; import java.util.concurrent.*; import java.lang.reflect.*; public class Exec1 extends JFrame...
3
5040
by: majorecono | last post by:
What I'm bad at is the dialog box part. This is the code Ive come up with... if you see a way to improve what im thinkng of.... post it please. Program should.... 1. Ask user to type in a...
1
2109
by: Smarteric | last post by:
import javax.swing.JOptionPane; public class MilesPerGallon { public static void main(String args) { String miles; String gallons; double MPG; double miles1, gallons1;
5
4528
by: Ramola | last post by:
hi, I was wondering if i can customise a JOptionPane ??? ex : A JOptionPane by default has a background color gray............. can i change it ?? can i put some images in the background ? Its...
10
1650
by: dudeishfish | last post by:
Hey Everyone! After using Devshed (possibly the worst forum I have ever encountered) I have come here with my questions and answers about Java, Python, VB, Basic C++, HTML, Game Maker, and...
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7059
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6758
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7010
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5362
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4499
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3011
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.