简体   繁体   中英

Accessing a JPanel on the JFrame from an opened JDialog

Hi I am making a simple desktop application and I am designing UI. I am using NetBeans to design it quickly. I did see lot of websites and blogs to find the answer, but couldn't land up with the right one. I am new to UI designing. Your answers/suggestion would be appreciated.

The question is:

I am having a single JFrame in the application. This Jframe has several JPanel which are set to Visible /Invisible state when user logs the application. Two of the panels are set to invisible state as default (I used Pre-Init Code set to False for them).

User on this JFrame can open a JDialog by clicking on the one of the button on this JFrame. (JFrame and JDialog are defined in separate classes under same package)

As soon as JDialog box is opened user have choice to select some options(Jlabels) and click on the Create button. (This Create button is a JLabel on the JDialog).

As soon as user is clicking on the Create button of the opened JDialog, the JDialog closes(I used Dialog.this.dispose() for this on the MouseReleaseEvent for this JLabel.) This closes the JDialog. But additional to closing of this JDialog on click, I want to set the visible state of the JPanel on the JFrame to true.

Summary: How can I setVisible(true) for the JPanel on the JFrame while having a MouseReleaseEvent on a JLabel on JDialog?

I am not able to access the JPanel of the Jframe on the JDialog class. (How can i access the Jpanel and make it visible specifically on the MouseEvent on JLabel of Jrame)

Thanks in advance.

JDialog class is:

public class NewModelDialog extends javax.swing.JDialog {


public NewModelDialog(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
}

Create.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseReleased(java.awt.event.MouseEvent evt) {
            CreateMouseReleased(evt);
        }
    });
    getContentPane().add(Create, new org.netbeans.lib.awtextra.AbsoluteConstraints(624, 444, 40, 20));

    pack();
    setLocationRelativeTo(null);
}                          
private void initComponents() {

    Create = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("New JDialog");
    getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
Create.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseReleased(java.awt.event.MouseEvent evt) {
            CreateMouseReleased(evt);
        }
    });
    getContentPane().add(Create, new org.netbeans.lib.awtextra.AbsoluteConstraints(624, 444, 40, 20));

    pack();
    setLocationRelativeTo(null);
}
private void CreateMouseReleased(java.awt.event.MouseEvent evt) {                                     

    NewModelDialog.this.dispose();

}   
public static void main(String args[]) {



    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
                           NewModelDialog dialog = new NewModelDialog(new javax.swing.JFrame(), true);
            dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                @Override
                public void windowClosing(java.awt.event.WindowEvent e) {
                   dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

                }
            });
            dialog.setVisible(true);

        }
    });
    }
 public javax.swing.JLabel Create; }

JFrameClass is:

public class GeneralEditorUI extends javax.swing.JFrame {


public GeneralEditorUI() {
    initComponents();
    setExtendedState(MAXIMIZED_BOTH);
}


private void initComponents() {


    SymbolViewer = new javax.swing.JPanel();


    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("JFrameClass");

    SymbolViewer.setBackground(new java.awt.Color(204, 204, 204));
    SymbolViewer.setPreferredSize(new java.awt.Dimension(36, 36));
    SymbolViewer.setVisible(false);

    javax.swing.GroupLayout SymbolViewerLayout = new javax.swing.GroupLayout(SymbolViewer);
    SymbolViewer.setLayout(SymbolViewerLayout);
    SymbolViewerLayout.setHorizontalGroup(
        SymbolViewerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(SymbolViewerLayout.createSequentialGroup()
            .addComponent(SymbolViewerLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGap(28, 28, 28)
            .addComponent(Close_SymbolViewer, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
    );
    SymbolViewerLayout.setVerticalGroup(
        SymbolViewerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(SymbolViewerLayout.createSequentialGroup()
            .addGroup(SymbolViewerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(Close_SymbolViewer)
                .addComponent(SymbolViewerLabel))
            .addGap(0, 85, Short.MAX_VALUE))
    );

}                       



public static void main(String args[]) {


    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new GeneralEditorUI().setVisible(true);
                        }
    });

}

public javax.swing.JPanel SymbolViewer;

}

This Jframe has several JPanel which are set to Visible /Invisible state when user logs the application

You should be using a Card Layout and let the layout manage the visibility of the panels. Read the section from the Swing tutorial on How to Use CardLayout for more information and examples.

How can I setVisible(true) for the JPanel on the JFrame while having a MouseReleaseEvent on a JLabel on JDialog

You can use the getOwner() method of JDialog to get the frame. Once you have a reference to the frame you can invoke your methods that changes the panel display in the CardLayout .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM