java - 当 JDialog 可见时执行 SQL 语句?

标签 java mysql multithreading joptionpane jdialog

我正在将 JTable 保存到我的 SQL 数据库中。没有问题。但是,我想制作某种在加载数据库数据时保留在屏幕上的对话框。我使用了 JDialog 和 JOptionPane:

final JOptionPane pane = new JOptionPane("Loading", JOptionPane.INFORMATION_MESSAGE,
                JOptionPane.DEFAULT_OPTION, null, new Object[] {}, null);
        final JDialog dialog = new JDialog();
        dialog.setTitle("Loading");
        dialog.setModal(true);
        dialog.setContentPane(pane);
        dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
        dialog.pack();
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);

现在,我希望在执行 SQL 语句后立即关闭该对话框,但是该对话框似乎阻止了该语句的执行,因为线程显然被它阻塞了。那么,如何在对话框显示时执行 SQL 语句,然后在执行语句后自行关闭?

最佳答案

我想你可以在你的代码中添加一个propertchange监听器。 发现类似的问题:[Java Swing - Close JDialog from external Thread 1

该帖子的答案如下:

停止自动关闭对话框

By default, when the user clicks a JOptionPane-created button, the dialog closes. But what if you want to check the user's answer before closing the dialog? In this case, you must implement your own property change listener so that when the user clicks a button, the dialog does not automatically close.

DialogDemo contains two dialogs that implement a property change listener. One of these dialogs is a custom modal dialog, implemented in CustomDialog, that uses JOptionPane both to get the standard icon and to get layout assistance. The other dialog, whose code is below, uses a standard Yes/No JOptionPane. Though this dialog is rather useless as written, its code is simple enough that you can use it as a template for more complex dialogs.

Besides setting the property change listener, the following code also calls the JDialog's setDefaultCloseOperation method and implements a window listener that handles the window close attempt properly. If you do not care to be notified when the user closes the window explicitly, then ignore the bold code.

final JOptionPane optionPane = new JOptionPane(
                "The only way to close this dialog is by\n"
                + "pressing one of the following buttons.\n"
                + "Do you understand?",
                JOptionPane.QUESTION_MESSAGE,
                JOptionPane.YES_NO_OPTION);

final JDialog dialog = new JDialog(frame, 
                             "Click a button",
                             true);
dialog.setContentPane(optionPane);
dialog.setDefaultCloseOperation(
    JDialog.DO_NOTHING_ON_CLOSE);
dialog.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent we) {
        setLabel("Thwarted user attempt to close window.");
    }
});
optionPane.addPropertyChangeListener(
    new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent e) {
            String prop = e.getPropertyName();

            if (dialog.isVisible() 
             && (e.getSource() == optionPane)
             && (prop.equals(JOptionPane.VALUE_PROPERTY))) {
                //If you were going to check something
                //before closing the window, you'd do
                //it here.
                dialog.setVisible(false);
            }
        }
    });
dialog.pack();
dialog.setVisible(true);

int value = ((Integer)optionPane.getValue()).intValue();
if (value == JOptionPane.YES_OPTION) {
    setLabel("Good.");
} else if (value == JOptionPane.NO_OPTION) {
    setLabel("Try using the window decorations "
             + "to close the non-auto-closing dialog. "
             + "You can't!");
}

关于java - 当 JDialog 可见时执行 SQL 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59444249/

相关文章:

计算质数 PThread

c - 使用多线程的段错误

c++ - 错误 C2248 : 'std::promise<_Ty>::promise' : cannot access private member declared in class 'std::promise<_Ty>'

java - 以板格式将 2D 数组打印到控制台

mysql - 排除相关记录 - 关联犯罪

php - 如何使用 gettext 翻译 MySQL 中的数据?

c# - MySql 中 EF5 的最小可用整数

java - 为什么ServiceReference要实现Comparable?

java - 在 Java 简单程序中跳出 While 循环

java - 将字节数组转换为 float