有没有一种方法在Java Swing中打开多个无模式JDialog实例而不禁用其组件?

huangapple go评论78阅读模式
英文:

Is there a way to open multiple instances of modeless JDialog in java swing without disabling its components?

问题

我有一个GUI,其中包含一个名为“View”的JLabel:

有没有一种方法在Java Swing中打开多个无模式JDialog实例而不禁用其组件?

与其相连的是一个mouseClickEvent。在单击“View”JLabel时,我可以打开多个模式less JDialog实例。

JDialog本身包含几个禁用的JTextFields和一个带有mouseClickEvent的JLabel,充当“Close”按钮:

有没有一种方法在Java Swing中打开多个无模式JDialog实例而不禁用其组件?

然而,在打开多个JDialog时,“close”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实例中“close”JLabel的mouseClickEvent的情况下打开多个实例吗?

英文:

I have a GUI which consist of a JLabel named "View":

有没有一种方法在Java Swing中打开多个无模式JDialog实例而不禁用其组件?

with a mouseClickEvent attached to it. Upon clicking on the "View" JLabel, i can open multiple instances of a modeless JDialog.

The JDialog itself consist of several disabled JTextFields and a JLabel with a mouseClickEvent attached to it which act as a "Close" button:

有没有一种方法在Java Swing中打开多个无模式JDialog实例而不禁用其组件?

However upon opening more than one JDialog, the "close" JLabel is disabled and the mouseClickEvent does not work anymore.

Here is my code:

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();
}

Is there a way to open multiple instances while still retaining the mouseClickEvent of the "close" JLabel in each opened instance of the JDialog?

答案1

得分: 2

> JDialog本身包含几个禁用的JTextFields和一个附加有mouseClickEvent的JLabel,充当"关闭"按钮。

为什么要使用带有MouseListener的JLabel来关闭对话框?JLabel并不是设计用来进行此类处理的。

对于"关闭"功能,使用带有"ActionListener"的JButton

如果您想要按钮看起来像一个标签,可以使用:

button.setBorderPainted(false);

> mouseClickEvent不再起作用。

viewDoctorDialog = new javax.swing.JDialog(topFrame,"查看医生详细信息",false);

在我看来,您有一个实例变量用于跟踪打开的对话框。当您创建第二个对话框时,您更改变量的引用,使其指向第二个对话框,因此您不再有原始对话框的引用。

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

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

因此,您甚至不需要一个特殊的变量来跟踪对话框。

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

这将使您更轻松地构造代码,使每个类仅包含与该类相关的ActionListeners。主类将包含显示子对话框的监听器。子对话框将具有用于"关闭"对话框的监听器。

英文:

> The JDialog itself consist of several disabled JTextFields and a JLabel with a mouseClickEvent attached to it which act as a "close" button.

Why would use use a JLabel with a MouseListener to close the dialog? A JLabel was not designed to do processing like this.

Use a JButton with an ActionListener for the "Close" functionality.

If you want the button to look like a label then you can use:

button.setBorderPainted( false );

> the mouseClickEvent does not work anymore.

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

It looks to me like you have an instance variable to track the open dialog. When you create the second dialog, you change the reference of the variable to point to the second dialog so you no longer have a reference to the original dialog.

So when using JButton, as suggested above, the code in your ActionListener for the "Close" button would then be something like:

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

So you don't even need a special variable to track the dialog.

Your "View Doctor Detials" dialog should be defined in a separate class. All the variables needed for that class should be defined in that class, not your main class that displays the dialog. So when you create an instance of the class you would pass in the JTable.

This will make it easier to structure your code so that each class only contains ActionListeners that are relevant to the class. So the main class will contain listeners to display the child dialogs. The child dialogs will have listener to "Close" the dialogs.

huangapple
  • 本文由 发表于 2020年8月19日 01:50:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63474069.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定