0

I'm doing a mini-program in Java, that would manage files in the computer. I tried put the JFileChooser.showSaveDialog() in the middle of the frame, by creating a panel and put it in the middle of the frame :

JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    panel.setPreferredSize(new Dimension(150,150) );

    JFileChooser chooseFile = new JFileChooser();
    chooseFile.showSaveDialog(panel);

    frame.getContentPane().add(BorderLayout.CENTER,panel);

    frame.setSize(400,400);
    frame.setVisible(true);

But actually, when the showSaveDialog() command happens, the frame becomes not visible. Can I change it?

Guy
  • 83
  • 1
  • 8

2 Answers2

1

It is not that the JFrame is not visible, but rather it was never set to be visible. I assume you want the JFrame to be visible when the showSaveDialog() is called. Due to the lack of detail, this is what I have to offer:

    javax.swing.JFrame frame = new javax.swing.JFrame();
    frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

    javax.swing.JPanel panel = new javax.swing.JPanel();
    panel.setPreferredSize(new java.awt.Dimension(150,150) );

    javax.swing.JFileChooser chooseFile = new javax.swing.JFileChooser(); frame.getContentPane().add(java.awt.BorderLayout.CENTER,panel);

    frame.setSize(400,400);
    frame.setVisible(true);
    chooseFile.showSaveDialog(panel);



EDIT: I reread your question and have the idea that you may want to place the JFileChooser on the panel for the question is ambiguous. You may have wanted this:

    javax.swing.JFrame frame = new javax.swing.JFrame();
    frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    javax.swing.JPanel panel = new javax.swing.JPanel();
    panel.setPreferredSize(new java.awt.Dimension(150,150) );
    javax.swing.JFileChooser chooseFile = new javax.swing.JFileChooser(); frame.getContentPane().add(java.awt.BorderLayout.CENTER,panel);
    chooseFile.setPreferredSize(new java.awt.Dimension(400, 400));
    frame.setSize(400,440);
    frame.setVisible(true);
    panel.add(chooseFile);<br><br>
  • Thank you, but I want the JFileChooser be in the middle of the JFrame(= BorderLayout.CENTER) - as long as the program is running; Something like this : http://prntscr.com/45zlup – Guy Jul 24 '14 at 20:48
0

The save dialog FileChooser is a modal dialog. This means that when we call chooseFile.showSaveDialog(panel); the file dialog is given focus and the user cannot interact with the panel until the dialog is closed. This is usually the behavior we want.

If we want to create some sort of file manager, then maybe you want to try adding the JFileChooser to your panel instead. Since JFileChooser extends component, you can do this, but it requires a bit more code and more of understanding of swing. This SO question addresses adding a JFileChooser to JPanel.

Community
  • 1
  • 1
Thorn
  • 4,015
  • 4
  • 23
  • 42
  • Thank you! I just removed JFileChooser.showOpenDialog() action, but now how can I access the "Save" , "Open" buttons? Is there any special listener for that or ActionListener will be good here? – Guy Jul 24 '14 at 21:14