0

I am having problems adding a panel to my main JFrame and hiding it right away, only making it visilble when a button it pressed. here is my code. Looking for any insight as to what the problem is. Also the label I try to add to the panel doesnt show up either.

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


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
 *
 * @author Tristan
 */
public class MainWindow extends JFrame implements ActionListener{
    public static final int WIDTH = 600;
    public static final int HEIGHT = 700;

    private JPanel addPanel;

public MainWindow()
{
    super("Day Planner");
    setSize(WIDTH, HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());


    JLabel intro1 = new JLabel("Welcome to your Day Planner", JLabel.CENTER);
    add(intro1, BorderLayout.NORTH);

    JLabel intro2 = new JLabel("Please choose an option from the menu bar above.", JLabel.CENTER);
    add(intro2, BorderLayout.CENTER);

    JMenu commands = new JMenu("Commands");

    JMenuItem addOption = new JMenuItem("Add");
    addOption.addActionListener(this);
    commands.add(addOption);

    JMenuItem searchOption = new JMenuItem("Search");
    searchOption.addActionListener(this);
    commands.add(searchOption);

    JMenuBar menuBar = new JMenuBar();
    menuBar.add(commands);
    setJMenuBar(menuBar);

    JButton button = new JButton("Add");
    button.addActionListener(this);
    add(button, BorderLayout.SOUTH);

    //add panel
    addPanel = new JPanel();
    addPanel.setLayout(new BorderLayout());
    addPanel.setSize(600,400);
    addPanel.setBackground(Color.CYAN);
    addPanel.add(new JLabel("add panel"), BorderLayout.CENTER);
    add(addPanel, BorderLayout.CENTER);
    addPanel.setVisible(false);


}

 @Override
 public void actionPerformed(ActionEvent ae)
 {
     /*String menuChoice = ae.getActionCommand();

     if (menuChoice.equals("Add")){
         addPanel.setVisible(true);
     }*/
     add(addPanel);
     //addPanel.setVisible(true);
 }
}
Tristan Pearce
  • 89
  • 1
  • 3
  • 12

3 Answers3

2

I have no issue with your example.

You may want to...

1- Make sure you've launched your UI in the context of the Event Dispatching Thread

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            }

            MainWindow frame = new MainWindow();
            frame.setVisible(true);
        }
    });
}

2- Try calling repaint after addPanel.setVisible(true)
3- Try calling invalidate after addPanel.setVisible(true) but before repaint if that doesn't work.

Much better solution is to use Card Layout for this kind of work

UPDATED

After spending some time reading through the code, what I think you seem to be concerned about is the fact that you're "intro" label isn't showing up...

This easily explained. Only one component can exists at any given position within a BorderLayout, so when you add you addPanel, even though it's invisible, it will clobber the intro2 label (effectively removing it from the container).

Below is an example using CardLayout

public class CardWindow extends JFrame implements ActionListener {

    public static final int WIDTH = 600;
    public static final int HEIGHT = 700;
    private JPanel addPanel;

    private JPanel cardPane;
    private CardLayout cardLayout;
    private final JLabel intro2;

    public CardWindow() {
        super("Day Planner");
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        cardPane = new JPanel((cardLayout = new CardLayout()));
        add(cardPane, BorderLayout.CENTER);


        JLabel intro1 = new JLabel("Welcome to your Day Planner", JLabel.CENTER);
        add(intro1, BorderLayout.NORTH);

        intro2 = new JLabel("Please choose an option from the menu bar above.", JLabel.CENTER);
        cardPane.add(intro2, "intro");

        JMenu commands = new JMenu("Commands");

        JMenuItem addOption = new JMenuItem("Add");
        addOption.addActionListener(this);
        commands.add(addOption);

        JMenuItem searchOption = new JMenuItem("Search");
        searchOption.addActionListener(this);
        commands.add(searchOption);

        JMenuBar menuBar = new JMenuBar();
        menuBar.add(commands);
        setJMenuBar(menuBar);

        JButton button = new JButton("Add");
        button.addActionListener(this);
        add(button, BorderLayout.SOUTH);

        //add panel
        addPanel = new JPanel();
        addPanel.setLayout(new BorderLayout());
        addPanel.setSize(600, 400);
        addPanel.setBackground(Color.CYAN);
        addPanel.add(new JLabel("add panel"), BorderLayout.CENTER);
        addPanel.setVisible(false);
        cardPane.add(addPanel, "Add");

        cardLayout.show(cardPane, "intro");

    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        String menuChoice = ae.getActionCommand();
        System.out.println(menuChoice);
        if (menuChoice.equals("Add")) {
            cardLayout.show(cardPane, "Add");
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                CardWindow frame = new CardWindow();
                frame.setVisible(true);
            }
        });
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • well i need the panels to show up and hide when options from the commands menu bar are chosen. The button was there to see if i could hide with that instead. – Tristan Pearce Dec 03 '12 at 00:17
  • Won't make any difference. The means by which you show the panels is irrelevant, the basic concept remains the same. I'd still use a `CardLayout` for this work, because it will make you're life considerably easier in the long wrong – MadProgrammer Dec 03 '12 at 00:19
  • @TristanPearce I've updated the answer with an example I think will solve you basic problem – MadProgrammer Dec 03 '12 at 00:29
1

Labels are showing up because you have added a panel after adding the labels on the frame so basically the panels are overlapping the labels. Also to show different panels you can use

 panel.setVisible(true); //For the panel you want to show and false for others

or you can use CardLayout which makes panels as cards and shows one of them at a time.

Fyre
  • 1,180
  • 8
  • 12
  • i only try to add one label on the panel that says "add panel". I want the other ones to be covered up by the panel. And i do use the panel.setVisible(false) and it does not hide the panel. – Tristan Pearce Dec 03 '12 at 00:03
  • probably should have actually tried this before discounting it... thanks. – Tristan Pearce Dec 03 '12 at 03:20
1

Just edited the code a little but it seems to work -

enter image description here

public class MainWindow extends JFrame implements ActionListener{
    public static final int WIDTH = 600;
    public static final int HEIGHT = 700;

    private JPanel addPanel;

    public static void main(String[] args) {
        MainWindow mainWindow = new MainWindow();
        mainWindow.setVisible(true);
    }
public MainWindow()
{
    super("Day Planner");
    setSize(WIDTH, HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());


    JLabel intro1 = new JLabel("Welcome to your Day Planner", JLabel.CENTER);
    add(intro1, BorderLayout.NORTH);

    JLabel intro2 = new JLabel("Please choose an option from the menu bar above.", JLabel.CENTER);
    add(intro2, BorderLayout.CENTER);

    JMenu commands = new JMenu("Commands");

    JMenuItem addOption = new JMenuItem("Add");
    addOption.addActionListener(this);
    commands.add(addOption);

    JMenuItem searchOption = new JMenuItem("Search");
    searchOption.addActionListener(this);
    commands.add(searchOption);

    JMenuBar menuBar = new JMenuBar();
    menuBar.add(commands);
    setJMenuBar(menuBar);

    JButton button = new JButton("Add");
    button.addActionListener(this);
    add(button, BorderLayout.SOUTH);

    //add panel
    addPanel = new JPanel();
    addPanel.setLayout(new BorderLayout());
    addPanel.setSize(600,400);
    addPanel.setBackground(Color.CYAN);
    addPanel.add(new JLabel("add panel"), BorderLayout.CENTER);
    add(addPanel, BorderLayout.CENTER);
    addPanel.setVisible(false);


}

 @Override
 public void actionPerformed(ActionEvent ae)
 {
     String menuChoice = ae.getActionCommand();

     if (menuChoice.equals("Add")){
         addPanel.setVisible(true);
     }
     add(addPanel);
     //addPanel.setVisible(true);
 }
}
Chan
  • 2,601
  • 6
  • 28
  • 45
  • No edit but just uncommented the lines... The example code is working... Plus refer this question for CardLayout http://stackoverflow.com/questions/11338229/how-to-make-an-added-jpanel-visible-inside-a-parent-jpanel/11338493#11338493 – Chan Dec 03 '12 at 00:13
  • so is there a problem with netbeans or something then? No matter what I do it will not change my program. The action listener shouldn't effect the initial setup of the panel.. – Tristan Pearce Dec 03 '12 at 00:21
  • the panel colour isnt changing nor is the label appearing, and it isnt hidden when i start the program. – Tristan Pearce Dec 03 '12 at 00:22
  • this is after i press the button – Chan Dec 03 '12 at 01:52