Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using JFileChooser from modal dialog

739 views
Skip to first unread message

Fred

unread,
Feb 9, 2012, 12:59:29 PM2/9/12
to
How do I pop up and use a JFileChooser from a modal dialog?

I find the methods setModalExclusionType and setModalityType, but they
are methods of Window and Dialog; when I pop the file chooser using
showDialog(), how do I get access to the window or frame used to
display it so that I can set its modality?
--
Fred K

Fred

unread,
Feb 9, 2012, 5:40:53 PM2/9/12
to
OK, gott it. Just pass the modal dialog (that is popping the
FileChooser) to the showDialog() method instead of null (We are using
a single FileChooser for the app, which can be popped up from any
number of modal or non-modal dialogs)
--
Fred K

markspace

unread,
Feb 10, 2012, 2:07:22 AM2/10/12
to
On 2/9/2012 2:40 PM, Fred wrote:
> On Feb 9, 9:59 am, Fred<fred.l.kleinschm...@gmail.com> wrote:
>> How do I pop up and use a JFileChooser from a modal dialog?


<http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html>

From that page:

//Create a file chooser
final JFileChooser fc = new JFileChooser();
...
//In response to a button click:
int returnVal = fc.showOpenDialog(aComponent);


>>
>> I find the methods setModalExclusionType and setModalityType, but they
>> are methods of Window and Dialog; when I pop the file chooser using
>> showDialog(), how do I get access to the window or frame used to
>> display it so that I can set its modality?
>
> OK, gott it. Just pass the modal dialog (that is popping the
> FileChooser) to the showDialog() method instead of null


Hmm, not sure about that one. According to that same web page:

"The argument to the showOpenDialog method specifies the parent
component for the dialog. The parent component affects the position of
the dialog and the frame that the dialog depends on."

Other stuff there too looks important. I'm not sure how you should be
manipulating the properties you mention, or why.



> (We are using
> a single FileChooser for the app, which can be popped up from any
> number of modal or non-modal dialogs)


Interestingly, this is correct. I forget where I read this now, but the
JFileChooser is designed to be used repeatedly. It will save the users
previous directory viewed, for example, so that the user doesn't have to
click the browsing buttions to select the same directory each time the
dialog box is viewed.



gosmi...@gmail.com

unread,
Sep 10, 2013, 3:26:37 PM9/10/13
to
try
{
// open a new file chooser dialog box.
final JFileChooser aFileChooser = new JFileChooser();

aFileChooser.setDragEnabled(false);
aFileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
aFileChooser.setMultiSelectionEnabled(true);
aFileChooser.setAcceptAllFileFilterUsed(true);
aFileChooser.setApproveButtonText("Select");

final JFrame aFrame = new JFrame("File Viewer");
aFrame.getContentPane().add(aFileChooser, BorderLayout.CENTER);
aFrame.pack();
aFrame.setLocationRelativeTo(null);
aFrame.setAlwaysOnTop(true);
aFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
aFrame.setVisible(true);

aFileChooser.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
try
{
if ((e.getActionCommand()).equals(JFileChooser.APPROVE_SELECTION))
{
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

// user has selected a file
File aFile = aFileChooser.getSelectedFile();

// create text are to display the file
JTextArea aText = new JTextArea();
aText.setWrapStyleWord(true);
aText.setEditable(false);
aText.read(new FileReader(aFile.getAbsolutePath()), null);

JScrollPane aScrollPane = new JScrollPane(aText);

// display newly selected file in a seperate frame.
JFrame aFrame2 = new JFrame(aFile.getName());
aFrame2.getContentPane().add(aScrollPane, BorderLayout.CENTER);
aFrame2.pack();
aFrame2.setLocationRelativeTo(null);
aFrame2.setAlwaysOnTop(true);
aFrame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
aFrame2.setVisible(true);
}
else
{
aFrame.dispose();
}
}

catch (Exception ex)
{
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
Errors.displayErrorDialog(ex, "Exception");
}
}
});
}

catch(Exception ex)
{
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
Errors.displayErrorDialog(ex, "Exception");
}

This makes sure the File Chooser is Modal and AlwaysOnTop

John B. Matthews

unread,
Sep 11, 2013, 6:51:42 AM9/11/13
to
In article <c713853a-73c4-4bf3...@googlegroups.com>,
gosmi...@gmail.com wrote:

> On Thursday, February 9, 2012 12:59:29 PM UTC-5, FredK wrote:
> > How do I pop up and use a JFileChooser from a modal dialog?
> >
> > I find the methods setModalExclusionType and setModalityType,
> > but they are methods of Window and Dialog; when I pop the file
> > chooser using showDialog(), how do I get access to the window
> > or frame used to display it so that I can set its modality?
[...]
> aFrame.setAlwaysOnTop(true);
[...]
> This makes sure the File Chooser is Modal and AlwaysOnTop

I've addressed some issues with the fragment shown in the complete
example below:

- Rather than multiple frames, use a modeless dialog for the
chooser.

- Consider AbstractAction to encapsulate functionality.

- "Note: some platforms might not support always-on-top windows." [1]

- Swing GUI objects should be constructed and manipulated _only_ on
the event dispatch thread. [2]

- Instead of getAbsolutePath(), use the FileReader constructor that
accepts a file [not shown].

SSCCE:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.AbstractAction;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class Test {

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Test().display();
}
});
}

private void display() {
JFileChooser chooser = new JFileChooser();
chooser.setDialogType(JFileChooser.OPEN_DIALOG);
chooser.setApproveButtonText("Select");

JTextArea text = new JTextArea(16, 40);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationByPlatform(true);
f.add(text, BorderLayout.CENTER);
f.pack();
f.setVisible(true);

JDialog d = new JDialog();
d.add(chooser, BorderLayout.CENTER);
d.pack();
d.setLocationRelativeTo(null);
d.setAlwaysOnTop(true);
d.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
chooser.addActionListener(new SelectAction(chooser, text));
d.setVisible(true);
}

private static class SelectAction extends AbstractAction {

private JFileChooser chooser;
private JTextArea text;

public SelectAction(JFileChooser chooser, JTextArea text) {
this.chooser = chooser;
this.text = text;
}

@Override
public void actionPerformed(ActionEvent e) {
if ((e.getActionCommand())
.equals(JFileChooser.APPROVE_SELECTION)) {
File file = chooser.getSelectedFile();
text.append(file.getPath() + "\n");
}
}
}
}

[1] <http://docs.oracle.com/javase/7/docs/api/java/awt/Window.html>
[2] <http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
0 new messages