Use SwingUtilities to avoid deadlock in GUI mini app

This commit is contained in:
Sean Owen 2022-10-23 16:20:22 -05:00
parent 0ea0ecddc5
commit bdc7c26526

View file

@ -38,6 +38,7 @@ import javax.swing.JFrame;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JTextArea; import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants; import javax.swing.WindowConstants;
import javax.swing.text.JTextComponent; import javax.swing.text.JTextComponent;
@ -69,16 +70,26 @@ public final class GUIRunner extends JFrame {
} }
public static void main(String[] args) throws MalformedURLException { public static void main(String[] args) throws MalformedURLException {
GUIRunner runner = new GUIRunner(); SwingUtilities.invokeLater(new Runnable() {
runner.setVisible(true); public void run() {
runner.chooseImage(); GUIRunner runner = new GUIRunner();
runner.setVisible(true);
runner.chooseImage();
}
});
} }
private void chooseImage() throws MalformedURLException { private void chooseImage() {
JFileChooser fileChooser = new JFileChooser(); JFileChooser fileChooser = new JFileChooser();
fileChooser.showOpenDialog(this); fileChooser.showOpenDialog(this);
Path file = fileChooser.getSelectedFile().toPath(); Path file = fileChooser.getSelectedFile().toPath();
Icon imageIcon = new ImageIcon(file.toUri().toURL()); Icon imageIcon;
try {
imageIcon = new ImageIcon(file.toUri().toURL());
} catch (MalformedURLException muee) {
throw new IllegalArgumentException(muee);
}
setSize(imageIcon.getIconWidth(), imageIcon.getIconHeight() + 100); setSize(imageIcon.getIconWidth(), imageIcon.getIconHeight() + 100);
imageLabel.setIcon(imageIcon); imageLabel.setIcon(imageIcon);
String decodeText = getDecodeText(file); String decodeText = getDecodeText(file);