1. I have some question regarding inserting two panels from a class to a JFrame from another class, both having same package of GUI. How to do this?

2.And how to make JFrame to display different panels during application run?
Example, I have a JButton in class A which after pressing, the JFrame will load another panel(panel from class B, example) which is inside the main class that stores the JFrame.

part of class A and the class where JFrame is stored is provided below. In this example I plan to add leftPanel and RightPanel from TestBookingGUI into the JFrame of FrameGUI class

package com.GUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class FrameGUI 
{
    JFrame frame;
    JMenuBar mb;
    JMenu  file;
    JMenuItem exit;
    TestBookingGUI b;

    public FrameGUI()
    {
        b = new TestBookingGUI();


        frame = new JFrame("Test Booking System");
        frame.setLayout(new GridLayout(1,1));

        mb = new JMenuBar();
        file = new JMenu("File");
        exit = new JMenuItem("Exit");             
    }

    public void initializeFrame()
    {
        b.launchFrame();

        frame.setPreferredSize(new Dimension(350, 500));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        file.add(exit);
        mb.add(file);
        frame.setJMenuBar(mb);

        frame.add(leftPanel);
        frame.add(rightPanel);
        //pack and display frame
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        FrameGUI a = new FrameGUI();
        a.initializeFrame();
    }

}



package com.GUI;

import javax.swing.*;
import java.awt.*; 
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.*;

public class TestBookingGUI 
{
    //private JFrame frame;
    private JPanel leftPanel,rightPanel,panel1,panel2,panel3;
    /**other component here**/

    public TestBookingGUI()
    {
        //other component here   
    }

    public void launchFrame()
    {
        //other component here

        panel1 = new JPanel();
        panel1.setLayout(new GridLayout(1,5));
        panel1.add(dayCombo);
        panel1.add(slashLabel1);
        panel1.add(monthCombo);
        panel1.add(slashLabel2);
        panel1.add(yearCombo);

        panel2 = new JPanel();
        panel2.setLayout(new GridLayout(1,6));
        panel2.add(hourCombo);
        panel2.add(colonLabel);
        panel2.add(minuteCombo);
        panel2.add(forLabel);
        panel2.add(numberOfHours);
        panel2.add(hoursLabel);

        panel3 = new JPanel();
        panel3.setLayout(new GridLayout(1,6));
        panel3.add(subjectLabel);
        panel3.add(subjectCombo);

        bg1 = new ButtonGroup();
        bg1.add(midtermButton);
        bg1.add(labButton);
        bg1.add(quizButton);


        leftPanel = new JPanel();
        leftPanel.setLayout(new GridLayout(20,1));
        leftPanel.add(label1);
        leftPanel.add(panel1);
        leftPanel.add(emptyLabel1);
        leftPanel.add(label2);
        leftPanel.add(venueCombo);
        leftPanel.add(emptyLabel2);
        leftPanel.add(label3);
        leftPanel.add(checkButton);
        leftPanel.add(emptyLabel3);
        leftPanel.add(label5);
        leftPanel.add(panel2);
        leftPanel.add(emptyLabel4);
        leftPanel.add(label6);
        leftPanel.add(panel3);
        leftPanel.add(testLabel);
        leftPanel.add(testLabel);
        leftPanel.add(midtermButton);
        leftPanel.add(labButton);
        leftPanel.add(quizButton);

        rightPanel = new JPanel();
        rightPanel.setLayout(new GridLayout(1,1));
        availableTimeArea.setText("Available time slot will\nbe displayed here.");
        //rightPanel.add(label4);
        rightPanel.add(availableTimeArea);


    }

}

Your listener in the class A with the button would have to gain access to the panels in class B, because they are private you would have to add an accessor method (package or public access) in order to gain access to the panels. you would also have to make sure that the launchFrame() method of the second listed class.

As for your second question, if you mean choosing different panel at runtime and keeping it then it's as simple as if (something) frame.add(panel1); else frame.add(panel2);

If you mean swapping between two things at runtime, it's a bit more complicated:

frame.remove(curPanel);//remove the current panel being displayed
frame.add(newPanel);//add the panel that you want to display next
frame.validate();//this is needed to make the change work
frame.repaint();//this will force the frame to display the new panel
curPanel=newPanel;//keep track of the current panel for later removal
This article has been dead for over six months. Start a new discussion instead.