【问题标题】:Is there a way to open multiple instances of modeless JDialog in java swing without disabling its components?有没有办法在 java swing 中打开多个无模式 JDialog 实例而不禁用其组件?
【发布时间】:2020-08-18 17:50:43
【问题描述】:

我有一个由名为“View”的 JLabel 组成的 GUI:

附加了一个mouseClickEvent。单击“查看”JLabel 后,我可以打开无模式 JDialog 的多个实例。

JDialog 本身由几个禁用的 JTextField 和一个带有 mouseClickEvent 的 JLabel 组成,它充当“关闭”按钮:

但是,在打开多个 JDialog 时,“关闭”JLabel 被禁用,并且 mouseClickEvent 不再起作用。

这是我的代码:

private void viewDoctorClickedEvent(java.awt.event.MouseEvent evt) 
{                                        
        javax.swing.JFrame topFrame = (javax.swing.JFrame) 
        javax.swing.SwingUtilities.getWindowAncestor(this);
        viewDoctorDialog = new javax.swing.JDialog(topFrame, "View Doctor Details", false);
        
        viewDoctorDialog.setMinimumSize(new java.awt.Dimension(580, 350));
        viewDoctorDialog.setResizable(false);
        
        viewDoctorDialog.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
        viewDoctorDialog.setLocationRelativeTo(topFrame);

        // This part is used to collect the values from the selected row of a Jtable and set the 
        // values to the disabled JTextFields in the JDialog
        try
        {
            int selectedTableRow = doctorListTB.getSelectedRow();

            String doctorName = doctorListTB.getValueAt(selectedTableRow, 0).toString();
            String doctorEmail = doctorListTB.getValueAt(selectedTableRow, 1).toString();
            String doctorPassword = doctorListTB.getValueAt(selectedTableRow, 2).toString();
            String doctorAddress = doctorListTB.getValueAt(selectedTableRow, 3).toString();
            String doctorPhone = doctorListTB.getValueAt(selectedTableRow, 4).toString();
            String doctorDepartment = doctorListTB.getValueAt(selectedTableRow, 5).toString();

            viewDoctorNameTF.setText(doctorName);
            viewDoctorEmailTF.setText(doctorEmail);
            viewDoctorPasswordTF.setText(doctorPassword);
            viewDoctorAddressTF.setText(doctorAddress);
            viewDoctorPhoneTF.setText(doctorPhone);
            viewDoctorDepartmentTF.setText(doctorDepartment);
            
            viewDoctorDialog.add(viewDoctorDialogPanel);
            viewDoctorDialog.setVisible(true);
        }
        catch(Exception e)
        {
            javax.swing.JOptionPane.showMessageDialog(viewDoctorDialogPanel, "Please select a row to view.", "No data to view.", javax.swing.JOptionPane.ERROR_MESSAGE);
        }
    }
    
    // Mouse Click Event to close the JDialog upon clicking on the "close" JLabel
    private void closeViewButtonClickedEvent(java.awt.event.MouseEvent evt) 
    {                                             
          viewDoctorDialog.dispose();
    }

有没有办法打开多个实例,同时在每个打开的 JDialog 实例中仍保留“关闭”JLabel 的 mouseClickEvent?

【问题讨论】:

标签: java swing jlabel jdialog


【解决方案1】:

JDialog 本身由几个禁用的 JTextField 和一个带有 mouseClickEvent 的 JLabel 组成,它充当“关闭”按钮。

为什么要使用带有 MouseListener 的 JLabel 来关闭对话框? JLabel 的设计初衷不是为了进行这样的处理。

使用 JButtonActionListener 来实现“关闭”功能。

如果你想让按钮看起来像一个标签,那么你可以使用:

button.setBorderPainted( false );

mouseClickEvent 不再起作用。

viewDoctorDialog = new javax.swing.JDialog(topFrame, "View Doctor Details", false);
    

在我看来,您有一个实例变量来跟踪打开的对话框。创建第二个对话框时,您将变量的引用更改为指向第二个对话框,这样您就不再拥有对原始对话框的引用。

因此,当使用JButton 时,如上所述,ActionListener 中“关闭”按钮的代码将类似于:

Jbutton button = (JButton)event.getSource();
Window window = SwingUtilities.windowForComponent( button );
window.dispose();

所以你甚至不需要一个特殊的变量来跟踪对话。

您的“查看医生详细信息”对话框应在单独的类中定义。该类所需的所有变量都应在该类中定义,而不是在显示对话框的主类中定义。因此,当您创建该类的实例时,您将传入 JTable。

这将使您的代码结构更容易,以便每个类仅包含与该类相关的 ActionListener。所以主类将包含监听器来显示子对话框。子对话框将具有“关闭”对话框的侦听器。

【讨论】:

  • 我已经实施了这个解决方案并且效果很好。感谢您的评论。
猜你喜欢
  • 2011-09-25
  • 1970-01-01
  • 2012-02-28
  • 2020-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
相关资源
最近更新 更多