Re: Help! Swing & Threads
blmblm@myrealbox.com wrote:
In article <PPmdnaftaqUszePZRVn-hg@comcast.com>,
Eric Sosman <esosman@acm-dot-org.invalid> wrote:
JFileChooser doesn't block, or at any rate not for very
[...]
Well .... JFileChooser, as far as I can tell, isn't really itself
a GUI component, is it? The GUI components are the dialogs it pops
up, when showOpenDialog() or showSaveDialog() is called. And those
are modal dialogs, so they do indeed block any action on other GUI
components.
JFileChooser is a GUI component. It extends JComponent and it even has
L&F delegate thingy. It happens to have some convenience methods that
open it in a modal dialog box, for use if for some reason you don't
think modal interfaces are criminal.
When a modal dialog is shown, the EDT isn't actually blocked as such.
Sure the method doesn't return until the dialog is closed, but from
within that method further events are dispatched. Events such as mouse
clicks on modalised windows are discarded, but repaint events for
instance get through. If there are other listeners reacting to the same
event, the behaviour can be somewhat unexpected. Sometimes an
EventQueue.invokeLater can help by letting other listeners for the event
run first.
Try this experiment: Write yourself a little GUI that fires
up *two* JFileChoosers simultaneously, and browse through a
couple different parts of your disk in side-by-side choosers.
The experiment itself won't teach you much, but pondering what's
going on might.
Below is my attempt to do -- well, something that's as close as
I can figure out to what you described. It doesn't behave as
you describe, though, since both the open dialogs are modal.
What am I doing differently from what you had in mind?
Don't use modal dialogs...
class FileChoice {
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
swing();
}
});
}
private static void swing() {
javax.swing.JFrame frame = new javax.swing.JFrame(
"File Choice"
);
frame.setDefaultCloseOperation(
javax.swing.WindowConstants.DISPOSE_ON_CLOSE
);
frame.setLayout(new javax.swing.BoxLayout(
frame.getContentPane(), // How daft is this?
javax.swing.BoxLayout.PAGE_AXIS
));
frame.add(new javax.swing.JFileChooser());
frame.add(new javax.swing.JFileChooser());
frame.pack();
frame.setVisible(true);
}
}
Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/