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);
}
}
> 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);
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.