0

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);

    }
}

3 Answers3

2

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

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Hello, thanks for your reply. Would you mind showing me the code please so I can understand what you're saying? Thank-you Sir. –  Feb 05 '14 at 19:51
  • First tell me exactly what you are trying to acheive. You didn't really explain to well, and your code only makes half sense to me. – Paul Samsotha Feb 05 '14 at 19:53
  • Basically when the button in Panel.java is pressed / clicked; I would like it to change screen to the Panel2. –  Feb 05 '14 at 19:56
  • 1
    @JonathanSaich Start by taking a closer look at [Creating a GUI with Swing](http://docs.oracle.com/javase/tutorial/uiswing/) – MadProgrammer Feb 05 '14 at 19:56
  • Been reading over that but I can't seem to find my answer. Please help me with it. –  Feb 05 '14 at 20:05
  • 1
    @JonathanSaich Look at [**this answer**](http://stackoverflow.com/a/21460065/2587435) it's not at all hard to follow. And do what MadPogrammer suggested. Go over the tutorial. Also you seem to need to go over [**Java Basics**](http://docs.oracle.com/javase/tutorial/reallybigindex.html) Since you don't know what a constuctor is. I'd advise going over the the first few chapters of that link before you dive into GUI. – Paul Samsotha Feb 05 '14 at 20:13
0

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

 add(panel2);

See if this works.

Rom El
  • 9
  • 1
-1

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);
     }

}
Rom El
  • 9
  • 1
  • Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Feb 05 '14 at 23:12