JFileChooser always to the front

I have a JDialog in which there is one JButton which opens a JFileChooser with showOpenDialog(this). How can I manage it that the JFileChooser always stays in front of JDialog (like it does when parent is a JFrame)?
[222 byte] By [mathosa] at [2007-10-2 10:05:41]
# 1
If you set an instance of JDialog as an owner of JFileChooser andthe JFileChooser is modal (setModal(true)), then it will always stay in front.RegardsMiso
mvpa at 2007-7-13 1:21:22 > top of Java-index,Desktop,Core GUI APIs...
# 2
That's what I did, this means the dialog itself. When I click on it, the JFileChooser moves to the back and is hidden by the dialog. Because the JFileChooser is modal, you can't click anymore on to the dialog and have to get it to the front again (Alt-F4).
mathosa at 2007-7-13 1:21:22 > top of Java-index,Desktop,Core GUI APIs...
# 3
> That's what I didWell then post a simple working demo program so we can see what you did and so we don't have to waste time guessing what you did. That way we can test the program and see if we experience the same behaviour.
camickra at 2007-7-13 1:21:22 > top of Java-index,Desktop,Core GUI APIs...
# 4

Here is a demo. Run the demo with vm 1.4 (some users may still have this version) and click onto "Open dialog", then "Open file chooser". Then you click somewhere onto the dialog, the filechooser moves behind the dialog. When you run the demo with java version 5, the dialog behaves like the frame:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class FileChooserDemo extends JFrame

{

JPanel contentPane;

JButton jB_frame = new JButton("Open frame");

JButton jB_dialog = new JButton("Open dialog");

JFrame frame = new JFrame("This is a frame");

JPanel jPa_frame = new JPanel();

JButton jB_fcframe = new JButton("Open file chooser");

JDialog dialog = new JDialog(this, "This is a dialog",true);

JPanel jPa_dialog = new JPanel();

JButton jB_fcdialog = new JButton("Open file chooser");

JFileChooser fc = new JFileChooser();

public FileChooserDemo()

{

enableEvents(AWTEvent.WINDOW_EVENT_MASK);

try

{

jbInit();

}

catch(Exception e)

{

e.printStackTrace();

}

}

private void jbInit() throws Exception

{

contentPane = (JPanel) this.getContentPane();

contentPane.setLayout(null);

jB_frame.setBounds(new Rectangle(43, 160, 153, 29));

jB_frame.addActionListener(new FileChooserDemo_actionAdapter(this));

jB_dialog.setBounds(new Rectangle(211, 160, 153, 29));

jB_dialog.addActionListener(new FileChooserDemo_actionAdapter(this));

contentPane.add(jB_frame, null);

contentPane.add(jB_dialog, null);

this.setSize(new Dimension(400, 257));

this.setTitle("FileChooser Demo");

this.setLocationRelativeTo(null);

this.setVisible(true);

}

protected void processWindowEvent(WindowEvent e)

{

super.processWindowEvent(e);

if (e.getID() == WindowEvent.WINDOW_CLOSING)

{

System.exit(0);

}

}

public static void main(String[] args)

{

new FileChooserDemo();

try

{

UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");

}

catch(Exception e)

{

e.printStackTrace();

}

}

void actionPerformed(ActionEvent ae)

{

if(ae.getSource() == jB_frame)

{

frame.setSize(new Dimension(600, 400));

frame.setLocationRelativeTo(null);

jPa_frame = (JPanel)frame.getContentPane();

jPa_frame.setLayout(null);

jB_fcframe.setBounds(new Rectangle(225,300,150,29));

jB_fcframe.addActionListener(new FileChooserDemo_actionAdapter(this));

jPa_frame.add(jB_fcframe,null);

frame.setVisible(true);

}

else if(ae.getSource() == jB_dialog)

{

dialog.setSize(new Dimension(600, 400));

dialog.setLocationRelativeTo(null);

jPa_dialog = (JPanel)dialog.getContentPane();

jPa_dialog.setLayout(null);

jB_fcdialog.setBounds(new Rectangle(225,300,150,29));

jB_fcdialog.addActionListener(new FileChooserDemo_actionAdapter(this));

jPa_dialog.add(jB_fcdialog,null);

dialog.setVisible(true);

}

else if(ae.getSource() == jB_fcframe)

{

fc.showOpenDialog(frame);

}

else if(ae.getSource() == jB_fcdialog)

{

fc.showOpenDialog(dialog);

}

}

}

class FileChooserDemo_actionAdapter implements java.awt.event.ActionListener

{

FileChooserDemo adaptee;

FileChooserDemo_actionAdapter(FileChooserDemo adaptee)

{

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent ae)

{

adaptee.actionPerformed(ae);

}

}

mathosa at 2007-7-13 1:21:22 > top of Java-index,Desktop,Core GUI APIs...
# 5

> I have a JDialog in which there is one JButton which

> opens a JFileChooser with showOpenDialog(this). How

> can I manage it that the JFileChooser always stays in

> front of JDialog (like it does when parent is a

> JFrame)?

Have you tried

dialog.setModal(false);

dialog.setAlwaysOnTop(false);

anilp1a at 2007-7-13 1:21:23 > top of Java-index,Desktop,Core GUI APIs...
# 6
The method setModal(false) makes only sense in jdk 1.5 when you want to apply "setAlwaysOnTop()". In jdk 1.4 "setAlwaysOnTop()" is not avaiable.
mathosa at 2007-7-13 1:21:23 > top of Java-index,Desktop,Core GUI APIs...
# 7
would it help to think outside the box?when the JButton is clicked, in its actionPerformed() do adialog.setVisible(false);when it returns, in the dialog, do a this.setVisible(true);
anilp1a at 2007-7-13 1:21:23 > top of Java-index,Desktop,Core GUI APIs...
# 8
Mmmh... May be.I think, when the dialog disapears to reapear after the close event of the JFileChooser, it may be confusing for the user of the application.
mathosa at 2007-7-13 1:21:23 > top of Java-index,Desktop,Core GUI APIs...
# 9

I am using JDK1.4.2 on XP.

In both cases your demo works correctly. The file chooser dialog is always modal and never disappears behind its parent window.

So obviously I don't know why its not working for you. I suggestion I have is maybe to trying using the original JFrame as the parent for the JFileChooser. Don't know why it would be any different but it doesn't hurt to try.

camickra at 2007-7-13 1:21:23 > top of Java-index,Desktop,Core GUI APIs...