使用条件布置模态对话框 [英] Disposing a Modal dialog using a condition

查看:83
本文介绍了使用条件布置模态对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模态jDialog,我希望在使用JOptionPane在另一个Dialog中显示错误消息后满足特定条件时进行处置.我试图在JOptionPane对话框之后使用dispose()方法,但是我的模式对话框仍然打开. 我的代码的相关部分如下:

I have a modal jDialog that i wish to dispose when a particular condition is met after showing the error message in another Dialog using JOptionPane. I have tried to use the dispose() method after the JOptionPane dialog but my modal dialog still opens up. The relevant part of my code is below:

    import java.awt.Component;
    import java.sql.*;
    import java.text.SimpleDateFormat;
    import javax.swing.JOptionPane;
    import javax.swing.table.DefaultTableModel;

    public class ReviewPatients extends javax.swing.JDialog {

    public ReviewPatients(java.awt.Frame parent, boolean modal) {
    super(parent, modal);

    jScrollPane1 = new javax.swing.JScrollPane();
    jTable1 = new javax.swing.JTable();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    jTable1.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {

        },
        new String [] {
            "Name", "Address", "Number"
        }
    ) {
        Class[] types = new Class [] {
java.lang.String.class, java.lang.String.class, java.lang.Integer.class
        };
        boolean[] canEdit = new boolean [] {
            false, false, false
        };

        public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
        }

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
        }
    });
    jScrollPane1.setViewportView(jTable1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1012, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(0, 0, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 526, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(0, 0, Short.MAX_VALUE))
    );

    pack();


    jScrollPane1.setVisible(false);

e: 
{    
           try
            {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3307/doctor","root","Pranav123");
            String query="SELECT * FROM records_table WHERE Name LIKE 'some_name';";
            Statement st = con.createStatement();
            ResultSet rs= st.executeQuery(query);



            DefaultTableModel tmodel = (DefaultTableModel) jTable1.getModel();

                //Clearing the table
                int rows=tmodel.getRowCount();
                while(rows>0)
                {
                    tmodel.removeRow(0);
                    rows--;
                }
                jTable1.setModel(tmodel);

            while(rs.next())
            {

                //Putting data into table
                tmodel.addRow(new Object[] {rs.getString(1),rs.getString(2),rs.getInt(4)});
                jTable1.setModel(tmodel);
            }

            }
            catch(Exception e)
            {
                System.out.println("Error: "+e);
            }

        if(jTable1.getRowCount()==0)
            {
                JOptionPane.showMessageDialog(this, "No records exist!");
                dispose();
                break e;
            }



         //Showing data   
        jScrollPane1.setVisible(true);


}    

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            ReviewPatients dialog = new ReviewPatients(new javax.swing.JFrame(), true);
            dialog.setVisible(true);
        }
    });
}

e是我在发生错误时用于退出该块的标签. 任何帮助将不胜感激.

e is the label that I have used to exit the block whenever an error occurs. Any help would be greatly appreciated.

推荐答案

JOptionPane中的所有静态方法都将创建模式对话框,即,代码的执行将停止,直到用户关闭该对话框为止.如果要创建非模式对话框,请直接使用JDialog.有关更多信息,请参考 Java Swing教程.

All static methods in JOptionPane create modal dialogs, i.e., execution of the code stops until the user dismisses the dialog box. If you want to create non-modal dialogs, use JDialog directly. Refer to Java Swing Tutorial for more information.

已更新:

如果我没记错,您要尝试的是在没有记录的情况下完全显示ReviewPatients对话框.如果是这样,最简单的解决方案是将dispose()替换为System.exit(1).

If I am not wrong, you are trying to do is to NOT show the ReviewPatients dialog at all when there are no records. If that is the case, the simplest solution is to just replacing dispose() with System.exit(1).

一个更好的解决方案是检查数据库中是否有记录,并且:

A better solution is check whether there are records in the database, and:

  • 如果有记录,请创建ReviewPatients对话框,并使用数据填充表.
  • 如果没有记录,请显示JOptionPane通知用户没有记录,然后终止程序.
  • If there are records, create the ReviewPatients dialog and populate the table with the data.
  • If there is no record, display the JOptionPane to inform the user there is no record, and terminate the program.

另外,请注意:尽管允许,但请尽量不要将AWT组件与Swing组件混合使用(请参阅

On a separate note: though permissible, try not to mix AWT components with Swing components (refer to another Stack Overflow answer for the reasons).

这篇关于使用条件布置模态对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆