How can I fill a JFrame with components when i press a JMenu in swing?

193 views Asked by At

I was working on a Java Swing project and I want a JMenuBar on the top of the page with JMenus, and when I select a JMenu I want that the JFrame be filled with some input fields. I tried to add panels that constitute the frame to the JMenu and when I press each JMenu the JFrame is filled by different components. My question is, can I get this result in a different way?

2

There are 2 answers

3
Mustofa Rizwan On

For the scenario of a JFrame, A JMenu bar where each menu works for different view/panels...

You should consider using CardLayout

  1. In the jframe put the jmenu at the top
  2. cover the rest of jframe with a Parent JPanel
  3. designate this JPanel as CardLayout.

    Add each JPanel as card inyour card layout

cards.add(card1, "Card 1");

cards.add(card2, "Card 2");

cards.add(card3, "Card 3");

On each menu click switch to the desired panel : by

CardLayout cardLayout = (CardLayout) cards.getLayout();
cardLayout.show(cards, "Card 2");
2
Fredy Fischer On

Here is a tiny example doing what Maverick_Mrt wrote:

package test;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;


public class CardTest extends JFrame {

public CardTest() {
    super("Card Test");
    this.getContentPane().setLayout(new BorderLayout());

    JMenuBar menuBar = new JMenuBar();
    JMenu cardsMenu = new JMenu("Cards");
    JMenuItem card1 = new JMenuItem("Card 1");
    JMenuItem card2 = new JMenuItem("Card 2");

    cardsMenu.add(card1);
    cardsMenu.add(card2);

    menuBar.add(cardsMenu);
    this.getContentPane().add(menuBar, BorderLayout.NORTH);

    JPanel cardPanel1 = new JPanel();
    cardPanel1.setLayout(new BorderLayout());
    cardPanel1.add(new JLabel("Card_ 1"), BorderLayout.CENTER);

    JPanel cardPanel2 = new JPanel();
    cardPanel2.setLayout(new BorderLayout());
    cardPanel2.add(new JLabel("Card_ 2"), BorderLayout.CENTER);

    JPanel cards = new JPanel();
    cards.setLayout(new CardLayout());

    cards.add(cardPanel1, "Card 1");
    cards.add(cardPanel2, "Card 2");

    card1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            CardLayout cardLayout = (CardLayout) cards.getLayout();
            cardLayout.show(cards, "Card 1");
        }
    });

    card2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            CardLayout cardLayout = (CardLayout) cards.getLayout();
            cardLayout.show(cards, "Card 2");
        }
    });
    this.getContentPane().add(cards,BorderLayout.CENTER);

    this.setDefaultCloseOperation(EXIT_ON_CLOSE);        
    this.pack();
    this.setVisible(true);        
}
public static void main(String args[]) {
    CardTest cardTest = new CardTest();
}

}