JDialog Focus issues in JRE 1.6
I have an Applet (signed) in a web page that opens up a modal JDialog to get a date using an array of labels which the user can navigate through using the keyboard or mouse.
In JRE 1.5.0_05 this worked great but on using exactly the same code with JRE 1.6, the JDialog does not appear to get the focus after I call setVisible(true). Only when I click in the dialog do the focus event methods get fired.
I've made all the fields in the dialog focusable and I've tried calling requestFocus() before I show the dialog (tried this on EventQueue.InvokeLater() too) all to no avail.
Can anyone explain this change in focus behaviour and, ideally, suggest a solution?
[689 byte] By [
sanxa] at [2007-11-27 10:21:23]

# 2
Thanks for the prompt reply. Unfortunately that didn't work, tho I cannot see why as the code is definitely being called.
I've stripped all the date stuff out the panel to leave just 2 buttons in the dialog. Here is the class...
package test;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class DateChooser extends JDialog
implements MouseListener, FocusListener,
KeyListener, ActionListener
{
private static final long serialVersionUID = -6725186298620856660;
private JButton ok;// "Ok" button.
private JButton cancel;// "Cancel" button.
public boolean focusLost = false; // focus change caused close
public boolean okClicked = false;
private void construct()
{
// Turn off window border
setUndecorated(true);
addFocusListener(this);
// Create content pane
JPanel content = new JPanel();
content.setBorder(BorderFactory.createLineBorder(WindowsBorderColour.m_borderCol,1));
content.addFocusListener(this);
// Create the OK button
//
ok = new JButton("OK");
ok.addActionListener(this);
ok.setFocusable(true);
ok.addKeyListener(this);
ok.addFocusListener(this);
ok.setOpaque(false);
// Create the cancel button
//
cancel = new JButton("Cancel");
cancel.addActionListener(this);
cancel.setFocusable(true);
cancel.addKeyListener(this);
cancel.addFocusListener(this);
cancel.setOpaque(false);
// Create a buttons panel
//
JPanel buttons = new JPanel();
buttons.add(ok);
buttons.add(cancel);
buttons.setBackground(SystemColor.inactiveCaption);
buttons.addFocusListener(this);
// Add all the controls to the content panel
//
content.setLayout(getContentPane().getLayout());
content.add("South", buttons);
content.doLayout();
// Update the dialog
//
setContentPane(content);
pack();
setResizable(false);
buttons.requestFocus();
}
/**
* Called when the "Ok" button is pressed. Just sets a flag and hides the
* dialog box.
*/
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == ok) okClicked = true;
setVisible(false);
}
public void focusGained(FocusEvent e)
{
System.out.println("****** Date got focus " + e.getSource());
}
public void focusLost(FocusEvent e)
{
System.out.println("****** Date lost focus" + e.getSource());
}
public void keyPressed(KeyEvent e)
{
System.out.println("Key pressed " + e);
// Deal with panel specific keys
//
switch (e.getKeyCode())
{
case KeyEvent.VK_ESCAPE:
setVisible(false);
break;
case KeyEvent.VK_TAB:
Component comp = getFocusOwner();
if (e.isShiftDown())
comp.transferFocusBackward();
else
comp.transferFocus();
break;
case KeyEvent.VK_ENTER:
if (e.getSource() != cancel) okClicked = true;
setVisible(false);
break;
}
}
public void mouseClicked(MouseEvent e)
{
System.out.println("Mouse clicked" + e);
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public DateChooser(Dialog owner, String title)
{
super(owner, title, true);
construct();
}
public DateChooser(Dialog owner)
{
super(owner, true);
construct();
}
public DateChooser(Frame owner, String title)
{
super(owner, title, true);
construct();
}
public DateChooser(Frame owner)
{
super(owner, true);
construct();
}
public Date select(Date date)
{
System.out.println("Select >>>");
this.addWindowListener(new java.awt.event.WindowAdapter()
{
public void windowOpened(java.awt.event.WindowEvent e)
{
System.out.println("Window opened");
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
System.out.println("requestFocusInWindow");
ok.requestFocusInWindow();
}
});
}
});
okClicked = false;
System.out.println("Before set visible");
setVisible(true);
System.out.println("After set visible");
if (!okClicked) return null;
System.out.println("Select <<<");
return new Date();
}
public Date select()
{
return select(new Date());
}
}
Which is called from the applet using...
Frame frame = new Frame();
DateChooser dc = new DateChooser(frame);
dc.setSize(200,200);
Date newDate = dc.select();
The trace I get out is...
Select >>>
Before set visible
Window opened
requestFocusInWindow
If I press a key nothing happens but as soon as I press the mouse down on a button I get the trace...
****** Date got focus javax.swing.JButton[,49,5,47x23, ...
and then when I press a key, I get the events as expected...
Key pressed java.awt.event.KeyEvent[KEY_PRESSED,keyCode=27,keyText=Escape,...
The mouse down must do something to set the focus so I think I may try and decompile that to see what it does. If you have any further thoughts I'd appreciate them!
sanxa at 2007-7-28 17:09:41 >

# 3
You put the listener in the wrong place...
I've changed your code to the following and works fine for me...
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class DateChooser extends JDialog implements MouseListener, FocusListener, KeyListener, ActionListener {
private static final long serialVersionUID = -6725186298620856660;
private JButton ok; // "Ok" button.
private JButton cancel; // "Cancel" button.
public boolean focusLost = false; // focus change caused close
public boolean okClicked = false;
private void construct() {
// Turn off window border
setUndecorated(true);
addFocusListener(this);
// Create content pane
JPanel content = new JPanel();
//content.setBorder(BorderFactory.createLineBorder(WindowsBorderColour.m_borderCol,1));
content.addFocusListener(this);
// Create the OK button
//
ok = new JButton("OK");
ok.addActionListener(this);
ok.setFocusable(true);
ok.addKeyListener(this);
ok.addFocusListener(this);
ok.setOpaque(false);
// Create the cancel button
//
cancel = new JButton("Cancel");
cancel.addActionListener(this);
cancel.setFocusable(true);
cancel.addKeyListener(this);
cancel.addFocusListener(this);
cancel.setOpaque(false);
// Create a buttons panel
//
JPanel buttons = new JPanel();
buttons.add(ok);
buttons.add(cancel);
buttons.setBackground(SystemColor.inactiveCaption);
buttons.addFocusListener(this);
// Add all the controls to the content panel
//
content.setLayout(getContentPane().getLayout());
content.add("South", buttons);
content.doLayout();
// Update the dialog
//
setContentPane(content);
pack();
setResizable(false);
// Listener should be here
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent e) {
System.out.println("Window opened");
SwingUtilities.invokeLater(new Runnable() {
public void run() {
System.out.println("requestFocusInWindow");
ok.requestFocusInWindow();
}
});
}
});
// Not needed
//buttons.requestFocus();
}
/**
* Called when the "Ok" button is pressed. Just sets a flag and hides the
* dialog box.
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource() == ok)
okClicked = true;
setVisible(false);
}
public void focusGained(FocusEvent e) {
System.out.println("****** Date got focus " + e.getSource());
}
public void focusLost(FocusEvent e) {
System.out.println("****** Date lost focus" + e.getSource());
}
public void keyPressed(KeyEvent e) {
System.out.println("Key pressed " + e);
// Deal with panel specific keys
//
switch (e.getKeyCode()) {
case KeyEvent.VK_ESCAPE:
setVisible(false);
break;
case KeyEvent.VK_TAB:
Component comp = getFocusOwner();
if (e.isShiftDown())
comp.transferFocusBackward();
else
comp.transferFocus();
break;
case KeyEvent.VK_ENTER:
if (e.getSource() != cancel)
okClicked = true;
setVisible(false);
break;
}
}
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse clicked" + e);
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public DateChooser(Dialog owner, String title) {
super(owner, title, true);
construct();
}
public DateChooser(Dialog owner) {
super(owner, true);
construct();
}
public DateChooser(Frame owner, String title) {
super(owner, title, true);
construct();
}
public DateChooser(Frame owner) {
super(owner, true);
construct();
}
public Date select(Date date) {
System.out.println("Select >>>");
// Wrong Place!!!!
//this.addWindowListener(new java.awt.event.WindowAdapter()
//{
//public void windowOpened(java.awt.event.WindowEvent e)
//{
//System.out.println("Window opened");
//SwingUtilities.invokeLater(new Runnable()
//{
//public void run()
//{
//System.out.println("requestFocusInWindow");
//ok.requestFocusInWindow();
//}
//});
//}
//});
okClicked = false;
System.out.println("Before set visible");
setVisible(true);
System.out.println("After set visible");
if (!okClicked)
return null;
System.out.println("Select <<<");
return new Date();
}
public Date select() {
return select(new Date());
}
}