-1

I am trying to add another JPanel from another class into my main JPanel. A random number is generated and if it is a certain value it will import a new Panel with movie info into my main Panel to be displayed. I am not sure why my panel is not showing when I click my button.

public class NetflixFrame extends JFrame {

public JButton button;

public NetflixFrame() {
    super ("App");

    MovieSuggestion port = new MovieSuggestion();

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(new Dimension(800,800));

    button = new JButton("Start");

    JPanel primary = new JPanel();
    primary.setBackground(Color.GREEN);
    primary.setPreferredSize(new Dimension(800,800));
    primary.add(button);

    button.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            int score1 = (int )(Math.random() * 11 + 1);

            if(score1<=10) {
                port.Panel1();
            }
        }
    });

    add(primary);
    setVisible(true);
}
  }

Panel to be added:

public class MovieSuggestion extends JFrame{

public static Component Panel1() {

    ImageIcon image = new ImageIcon("./src/Hook.jpg");

    JPanel first = new JPanel();
    first.setPreferredSize(new Dimension(250,200));
    first.setBackground(Color.GREEN);
    JLabel imageLabel = new JLabel("Favorite Movie:", image, SwingConstants.CENTER);
    imageLabel.setHorizontalTextPosition(SwingConstants.CENTER);
    imageLabel.setVerticalTextPosition(SwingConstants.TOP);
    imageLabel.setFont(new Font("Times New Roman", Font.PLAIN, 20));
    first.add(imageLabel, BorderLayout.WEST);

    return first;

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jibblz
  • 13
  • 4
  • 1
    1) Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently... – Andrew Thompson Nov 05 '15 at 23:38
  • 1
    .. 4) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. 5) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) 6) `new Font("Times New Roman", Font.PLAIN, 20)` Use `Font.SERIF` instead of `"Times New Roman"` for x-plat. – Andrew Thompson Nov 05 '15 at 23:38

1 Answers1

1

You are calling port.Panel1() which returns a JPanel, but you are not doing anything with it.

If you want display it into your main JPanel, then you need to add the returned JPanel to the main JPanel.

Put this inside your if statement: primary.add(port.Panel1());

Shahzad
  • 2,033
  • 1
  • 16
  • 23