• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

key listener not detected

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am using KeyListener in my code, to detect a keyPressed Event on a JDialog pop-up window. But the event is not detected.
This is the code used -

class MyDialog extends JDialog
{
public MyDialog()
{
// code for the contents of the JDialog

addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_F1);
{
// some more code....
}
}
});
// other code....
}}

Please let me where I am going wrong in this and why the key pressed event is not detected.

Thanks in advance!!!
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is the dialog modal?

do you have this line
addKeyListener(new KeyAdapter()...

before or after this line
setVisible(true);
 
ram shyam
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

Yes, the dialog is modal.

Here is the complete code.

public MyDialog()
{
setModal(true);
setTitle("....");

// Create a JPanel - myPanel
// Add some fields in this panel

Object[] array = {myPanel};

// Create a JOptionPane - optionPane
//add the myPanel, ok , Cancel buttons

setContentPane(optionPane);

//here is the keyListener code -

addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_F1);
{
// some code
}
}
}
});

optionPane.addPropertyChangeListener(new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent e)
{
// code to perform some operation based on OK / Cancel
setVisible(false); // after Ok/Cancel is pressed.
}

} );

setSize(300, 300);
}

This is the code. Please let me know what is wrong in this code.

Thanks in advance!!!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
keyListeners work when the component has focus.

your code looks as though the dialog would never receive focus,
focus going between the components on the panel, and the optionpane buttons

for an F1 (help?) key, use KeyBindings

http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html
 
ram shyam
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Many thanks for your help and valuable time!!


The issue got resolved now. Thanks a lot once again!

Regards,
Ram Shyam
 
ram shyam
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have one more query in this.
I have got the required action implemented when "F1" is pressed with getInputMap()and get getActionMap() as suggested by you. But the problem is that, I am opening a Help screen when "F1" is pressed. This help screen cannot be accessed until I press OK/Cancel or close the MyDialog which detects the key bindings.

Please let me know whether this is the default behavior or any solution to this.

Many thanks in advance!!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the 'help' dialog needs to have the original dialog as the parent
 
ram shyam
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

My GUI works as follows -

I have a page containing JTable in which if I click a row, a JDialog is popped-up. In this JDialog only, I have used the key bindings as follows-

Action anAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
//code
}
};
optionPane.getInputMap().put(KeyStroke.getKeyStroke("F1"), "showHelp");
optionPane.getActionMap().put("showHelp",anAction);


On pressing F1 on the pop-up, I am able to get the help screen up but not able to access it unless i close the pop-up. As per your suggestion, I am not able to get the clear picture of how to set the parent here.
Please explain me in detail to solve this issue.

Many thanks in advance!!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dialog1 = In this JDialog only, I have used the key bindings as follows-
dialog2 = On pressing F1 on the pop-up, I am able to get the help screen

when you create dialog2
JDialog dialog2 = new JDialog(dialog1);
 
ram shyam
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks for your response.

I will implement the same and get back to you.

Many thanks for your help!!
 
ram shyam
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The help screen I am using is of type JFrame. So, constructor JFrame(new JDialog()) is not defined.
Hence, your solution doesnt work for me. Moreover, these help screens are already defined and being used by many other pages in the GUI. So, the new pop-up I implemented should also use the same JFrame help screen, but the problem is that, the screen is not accessible.

Please let me know if there is any other way of implementing this.

Many thanks in advance!!!
 
ram shyam
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can anyone help me in resolving this issue?

Many thanks in advance!!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I doubt you're going to achieve what you want using a JFrame for the help screen.

you can still create the frame, not set it to visible, then do something like this
 
ram shyam
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

From your code, I understand that-

parent - the dialog pop-up window from my JTable.
dialog - the help screen.

But, what is the frame here?
As I mentioned earlier, the help screen should be of type JFrame and I cannot change my dialog pop-up to JFrame.
So, still the problem is not resolved :-(

Please explain how can this be resolved.

Many thanks in advance!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> But, what is the frame here?

frame comes from your earlier post
"The help screen I am using is of type JFrame."
 
ram shyam
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Sorry for the delayed response as I was held up in other tasks.

Currently my implementation is as follows -

I have a parent screen containing a JTable from which I get a JDialog. This dialog has its content pane set to a JOptionPane and this optionpane is popped-up. In this optionPane, if I press F1, I get the JFrame help screen but its not accessible.

Since the JoptionPane is used as the contentpane for the JDialog, how can I set the frame as its content now?

Please let me know how to resolve this.

Many thanks for your help!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic