user3001857
user3001857

Reputation:

Why won't my JPanel show?

So I'm just checking and when I click my button it won't show my JPanel, any idea why?

Thanks.

I want the third class to show, really do appreciate the help - Thanks allot.

First class - JFrame class.

import javax.swing.JFrame;

public class Frame {
    public static void main(String[] args ) {
        JFrame frame = new JFrame("JFrame Demo");
        Panel panel1 = new Panel();

        frame.add(panel1);


        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 500);
        frame.setVisible(true);

    }
}

Second class - Panel 1

import javax.swing.JPanel;
import java.awt.CardLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Panel extends JPanel{
    public Panel() {
        setLayout(null);
        final Panel2 panel2 = new Panel2();


        JButton btnNewButton = new JButton("New button");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                setVisible(false);
                panel2.setVisible(true);


            }
        });
        btnNewButton.setBounds(62, 197, 224, 122);
        add(btnNewButton);
    }
}

Third class - Panel 2 (I want this to show)

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.CardLayout;
import javax.swing.JTextField;


public class Panel2 extends JPanel {
    private JTextField textField;
    public Panel2() {

        setLayout(null);
        setVisible(true);
        textField = new JTextField();
        textField.setBounds(84, 84, 290, 77);
        add(textField);
        textField.setColumns(10);

    }
}

Upvotes: 0

Views: 3853

Answers (3)

Rom El
Rom El

Reputation: 9

Modify Panel.java to look like below. Tell me if this is good for your needs:

import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Panel extends JPanel{

    Panel2 panel2 = null;
    JButton btnNewButton = null;

    public Panel() {
         setLayout(null);
         panel2 = new Panel2();

         panel2.setBounds(5,5,300,500);
         add(panel2);
         showPanel2(false);

          btnNewButton = new JButton("New button");
          btnNewButton.setBounds(62, 197, 224, 122);
          add(btnNewButton);
          showButton(true);

          btnNewButton.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {

                showButton(false);
                showPanel2(true);


            }
         });

     }

     public void showPanel2(boolean bshow)
     {
         panel2.setVisible(bshow);
     }

     public void showButton(boolean bshow)
     {
          btnNewButton.setVisible(bshow);
     }

}

Upvotes: -1

Paul Samsotha
Paul Samsotha

Reputation: 208944

You never add panel2 to anything. A JPanel isn't like a JFrame where setVisible makes it magically appear. You need to add it to a container. Just add it to your Panel.

  • Also avoid using null layouts. Learn to use Layout Managers

  • Also see Initial Threads. You want to run your swing apps from the Event Dispatch Thread like this

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new Frame();
            }
        });
    }
    
  • This looks like a case where you may have been trying to do something along the lines of what a CardLayout achieves. See this example for a basic use. Also see How to Use Card Layout

Upvotes: 2

Rom El
Rom El

Reputation: 9

In the second class, after the second line in the constructor, have you tried?

 add(panel2);

See if this works.

Upvotes: 0

Related Questions