Hossam Din
Hossam Din

Reputation: 65

How to Hide JPanel and Open another one in other Java FIle?

I'm newbie in java GUI , so i'm facing a problem right now ... i've created a GUI using Netbeans GUI Builder .. i've Created a file called MainUI.java and gdUI.Java the MainUI.java contains the frame and buttons in which if a button is clicked the Jpanel Hides and opens the Panel from gdUI.java

here's the code i've done so far :

      private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        jPanel1.setVisible(false);


           }

and gdUI code is :

         package GUI;


         public class gdUI extends javax.swing.JPanel {

/**
 * Creates new form gdUI
 */
public gdUI() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    setBackground(new java.awt.Color(255, 153, 51));
    setMaximumSize(new java.awt.Dimension(600, 500));
    setMinimumSize(new java.awt.Dimension(600, 500));
    setPreferredSize(new java.awt.Dimension(600, 500));

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 600, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 500, Short.MAX_VALUE)
    );
}// </editor-fold>                        


// Variables declaration - do not modify                     
// End of variables declaration                   

}

when i click the button , i successfully hided the current JPanel , how can i add the other files new one ?

Thanks In Advance

Upvotes: 0

Views: 463

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 208964

The way you want to accomplish this task is using a CardLayout as pointed out by trashgod. This will allow you to switch between views, with simple Cardayout commands like next(), previous() and show(), the last allowing to show any particular component/view by name.

Upvotes: 1

Related Questions