Tag: invokeandwait

Java API“如果不在EDT上运行在EDT上”

只是对我的一些重复代码的思考: Runnable run = new Runnable() { @Override public void run() { // Some EDT code } }; if (!EventQueue.isDispatchThread()) { SwingUtilities.invokeAndWait(run); } else { run.run(); } 它并不是非常烦人,但似乎有一些专有function会为你检查这个,虽然我还没有找到它。

Java Swing:主类等到JFrame关闭

我需要一些简单的java应用程序的帮助,它使用两个jframe来获取一些输入参数。 这是我的代码草图: //second jframe, called when the button OK of the first frame is clicked public class NewParamJFrame extends JFrame{ … } //first jframe public class StartingJFrame extends JFrame{ private static NewParamJFrame newPFrame = null; private JTextField gnFilePath; private JButton btnOK; public StartingJFrame(){ //.. initComponents(); } private void initComponents(){ btnOK.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ […]

SwingWorker中的error handling

我的问题是基于理论的问题,但它确实满足了我的特定需求。 当你的SwingWorker抛出a)你可以预料到并且b)需要从中恢复并继续的exception时,你会怎么做,但是你想通知用户这个错误已经发生了? 如何获取预期的exception并通知用户而不违反“ doInBackground()没有Swing代码”规则? 考虑到这个问题,我已经开发了一个SSCCE,我想提出下面的问题。 SSCCE: import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.concurrent.ExecutionException; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JProgressBar; import javax.swing.SwingUtilities; import javax.swing.SwingWorker; public class Test { public static void main(String args[]) { final JFrame frame = new JFrame(); JButton go = new JButton(“Go.”); go.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) […]