views:

6415

answers:

6

I have created a Java application (project) in NetBeans, in which I have designed a JFrame with menu bar, and different JPanels. I want these JPanels to appear inside the JFrame on action of different menu items, so that whenever the menu items are clicked different JPanels should appear inside the JFrame. I have designed both JFrame & JPanel separately, but I couldn't link them together.

Please help me out friends.

+4  A: 

You could use Card Layout for this. A Card Layout can contain many components (JPanel in your case), and you can switch between them. It's easy to add a card layout in the netbeans palette.

Doc:

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/CardLayout.html

Yngve Sneen Lindal
the problem is not with the layout, actually after clicking on the menu item, its not doing ANYTHING means it does not perform any action, in action performed i used "new JPanel1();", is tht correct, or what else should i add in the code to add JPanel in JFrame during run time.
You could have all your panels created at creation of the frame. I would maybe suggest that you have a main panel with a CardLayout: mainPanel.setLayout(new java.awt.CardLayout());then you could just: mainPanel.add("new", new); where new is a JPanel
Yngve Sneen Lindal
To change your view to the panel "new" you do:CardLayout layout = (CardLayout) mainPanel.getLayout();layout.show(mainPanel, "new");This would be the code in your action listener. Maybe there's some other more elegant ways to do this, but this will work.
Yngve Sneen Lindal
A: 

If you want the menus to react to clicks, you need to add Listeners to the menu items, Which in response will show the correct panel.

Kyle G
A: 

The Matisse GUI builder has pretty good support for the "JTabbedPane" control, if that's what you're looking for. You can drag the TabbedPane container into your forms, then drag other containers onto it to create new tabs.

If you're looking for more advanced behavior, such as hiding/showing different containers when the user presses different buttons, you will need to write some code; the GUI builder isn't equipped to handle this.

Outlaw Programmer
A: 

after changing the panel in JFrame do the frameObj.pack();

+1  A: 

It appears from one of your comments that you wish to dynamically create the JPanels when the buttons are clicked. If this is the case then CardLayout is not ideal. It is relatively easy to achieve the same effect yourself. The code will look something like this:

public void actionPerformed(ActionEvent event) {
    Container contentPane = frame.getContentPane();
    contentPane.removeAll();
    contentPane.add(new YourPanel());
    contentPane.invalidate();
    contentPane.repaint();
}

That assumes that the changing panel is the only component in the frame. If it is not then add a JPanel with a BorderLayout to the content pane in Matisse and then add the new panels to that rather than the content pane.

Russ Hayward
A: 

First you save it as a .java file. This is the main jframe class. You run it first. Then you see the external panel add to it by constructor.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on Apr 20, 2010, 5:20:26 PM
 */

package game;

import javax.swing.SwingUtilities;

/**
 *
 * @author S.M. Mahmudul hasan
 */
public class MainFrame extends javax.swing.JFrame {

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

    /** 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() {

        firstPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout firstPanelLayout = new javax.swing.GroupLayout(firstPanel);
        firstPanel.setLayout(firstPanelLayout);
        firstPanelLayout.setHorizontalGroup(
            firstPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 457, Short.MAX_VALUE)
        );
        firstPanelLayout.setVerticalGroup(
            firstPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 398, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(firstPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(firstPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }

    private void addPanel() {
        MainPanel m=new MainPanel();
        this.getContentPane().remove(0);
        firstPanel.removeAll();
        javax.swing.GroupLayout firstPanelLayout = new javax.swing.GroupLayout(firstPanel);
        firstPanel.setLayout(firstPanelLayout);
        firstPanelLayout.setHorizontalGroup(
            firstPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(firstPanelLayout.createSequentialGroup()
                .addGap(78, 78, 78)
                .addComponent(m, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(79, Short.MAX_VALUE))

        );
        firstPanelLayout.setVerticalGroup(
            firstPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(firstPanelLayout.createSequentialGroup()
                .addGap(25, 25, 25)

                .addComponent(m, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(23, 23, 23))
        );
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(firstPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(firstPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        SwingUtilities.updateComponentTreeUI(this.getContentPane());
    }

    // Variables declaration - do not modify
    private javax.swing.JPanel firstPanel;
    // End of variables declaration

}

Panel class:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainPanel.java
 *
 * Created on Apr 20, 2010, 5:21:03 PM
 */

package game;

import java.awt.Graphics;

/**
 *
 * @author S.M. Mahmudul hasan
 */
public class MainPanel extends javax.swing.JPanel {

    /** Creates new form MainPanel */
    public MainPanel() {
        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() {

        jLabel1 = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createTitledBorder("paintable area"));

        jLabel1.setText("Paintable area");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(86, 86, 86)
                .addComponent(jLabel1)
                .addContainerGap(128, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(103, 103, 103)
                .addComponent(jLabel1)
                .addContainerGap(119, Short.MAX_VALUE))
        );
    }// </editor-fold>

  @Override public void paintComponent(Graphics g) {
         super.paintComponent(g);    // paints background
         g.drawString("shohan", 50, 100);
    }

    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    // End of variables declaration

}
shohan