0

Okay so I know a question on JPanels In JPanels has been answered already but I've studied it and I'm confident it doesn't answer my problem.

I'm creating a gui using a card layout to swap between screens and essentially after some processes I want a panel which displays something from the processes then a panel I had shown previously

here is a very simplified (runnable) version of my code with my problem:

import javax.swing.JFrame;

public class Center {

    public gui frame = new gui();
    public logic doTheThing = new logic();

    public int passTheParcel = -1;

    public Center(){

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

         /*I know compairing identical terms but it won't loop forever 
         passTheParcel will change*/
         while (-1 == passTheParcel){ 

              if(frame.panel2Pressed == true){
                  passTheParcel = doTheThing.makeRandom();
                  frame.refreshPanel2(passTheParcel);
              }
              frame.repaint();
          }
    }
    public static void main(String[] args) {
        Center example = new Center();
        }
}

the gui:

import java.awt.CardLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class gui extends JFrame {

    public static JPanel contentPanel = new JPanel(new CardLayout());
    public JPanel panel1 = new JPanel();
    public JPanel panel2 = new JPanel();
    public JPanel panel3 = new JPanel();
    public JButton goToPanel1, goToPanel2, goToPanel3; 
    public JLabel info, Panel3Label;
    public boolean panel2Pressed = false;

    public static CardLayout cardLayout = (CardLayout)contentPanel.getLayout();

    GridBagConstraints gbc = new GridBagConstraints();

    public gui() {
        super("test");

        panel1.setLayout(new GridBagLayout());
        panel2.setLayout(new GridBagLayout());
        panel3.setLayout(new GridBagLayout());

        gbc.insets = new Insets(20, 20, 0, 0); 

        //populating first panel
        goToPanel2 = new JButton("go to Panel2");
        gbc.gridx = 0;
        gbc.gridy = 1;
        panel1.add(goToPanel2, gbc);

        goToPanel3 = new JButton("go to Panel3");
        gbc.gridx = 0;
        gbc.gridy = 2;
        panel1.add(goToPanel3, gbc);

        //populate second Panel
        info = new JLabel("place holder");
        gbc.gridx = 0;
        gbc.gridy = 1;
        panel2.add(info, gbc);

        //populate third Panel 
        Panel3Label = new JLabel("this is Panel3");
        gbc.gridx = 0;
        gbc.gridy = 1;
        panel3.add(Panel3Label, gbc);

        goToPanel1 = new JButton("go to Panel1");
        gbc.gridx = 0;
        gbc.gridy = 2;
        panel3.add(goToPanel1, gbc);

        //create and add action listener
        ButtonHandler handler = new ButtonHandler();

        goToPanel1.addActionListener(handler);
        goToPanel2.addActionListener(handler);
        goToPanel3.addActionListener(handler);

        // set up the card layout 
        getContentPane().add(contentPanel);

        contentPanel.add(panel1, "panel1");
        contentPanel.add(panel2, "panel2");
        contentPanel.add(panel3, "panel3");

        cardLayout.show(contentPanel, "panel1");
    }

    public void refreshPanel2(int random){
       //setting text in label to the random number

       info.setText((""+random));

       if(random > 25){
           gbc.gridx = 0;
           gbc.gridy = 2;
           panel2.add(panel3,gbc);

           panel2.revalidate();
          panel2.repaint();
       }
   }

    public class ButtonHandler implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
             if (e.getSource() == goToPanel1){
                 cardLayout.show(contentPanel, "panel1");
             }
             if (e.getSource() == goToPanel2){
                panel2Pressed = true;
                cardLayout.show(contentPanel, "panel2");
             }
             if (e.getSource() == goToPanel3){
                 cardLayout.show(contentPanel, "panel3");
             }
        }
    }
}

and finally the logic class:

/*this class may seem useless but it represents a much larger piece of code
its function is simply to return something so panel2 can use it later */

import java.util.Random;

public class logic {

    public Random random = new Random();
    public int RandomNumber;

    public int makeRandom(){

        RandomNumber = random.nextInt(100);
        System.out.println(RandomNumber);
        return RandomNumber;
    }
}

my problem is thus: when goToPanel2 button is pressed, it will indeed show panel2 the label will be changed to a random number however even when this number is greater than 25 panel3 will not be displayed. Please run for yourself and advise.

  • Start by having a look at [Model-View-Controller](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller). Essentially, what you need is some kind of controller, which can work with the model to decide which view should be shown when – MadProgrammer Jan 24 '16 at 21:16
  • [A little long winded example](http://stackoverflow.com/questions/31602113/listener-placement-adhering-to-the-traditional-non-mediator-mvc-pattern/31604919#31604919), but conceptually what you're looking for – MadProgrammer Jan 24 '16 at 21:19
  • [Example](http://stackoverflow.com/questions/23352226/singleton-with-cardlayout-wont-show-card-when-another-class-calls-singleton-ins/23352348#23352348), [example](http://stackoverflow.com/questions/18350197/cant-call-a-the-third-page-via-the-2nd-page/18350351#18350351), [example](http://stackoverflow.com/questions/24296505/how-to-effectively-use-cardlayout-in-java-in-order-to-switch-from-panel-using-bu/24296872#24296872) – MadProgrammer Jan 24 '16 at 21:22
  • *"I know a question on JPanels In JPanels has been answered already"* No, it's been asked & answered multiple times. Which one are you referring to? Link to it. – Andrew Thompson Jan 24 '16 at 22:12
  • @Andrew Thompson This is the [question](http://stackoverflow.com/questions/913139/adding-additional-jpanels-to-a-jpanel) I was referring to, I've looked at more but this one seems to imply what I've written should work. –  Jan 24 '16 at 23:04

0 Answers0