0

I am writing a program that will have a large amount of panels used. I want to put each panel in their own file so that it will be more easily organized.

I am using cardlayout

My problem is that in the other class files, I can't change the visibility of another panel because the frame is in the main class and the panel will be in another class.

I wrote some sample code that encapsulates the problem. The 1st panel is in the main class, and it can switch to the second, outside panel, but the outside panel cannot switch to the main class' panel.

Here is my main class:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Arrow{
public JPanel panelHouse;
public JFrame frame;
public int total = 3000;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Arrow window = new Arrow();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Arrow() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(400, 400, 909, 572);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new CardLayout(0, 0));

    JPanel panelHouse = new JPanel();
    panelHouse.setBounds(0, 0, 10, 10);
    frame.getContentPane().add(panelHouse, "Housing");
    panelHouse.setLayout(null);

    Flash Flash1 = new Flash();
    frame.getContentPane().add(Flash1, "Flash");
    Flash1.setLayout(null);

    JLabel lblHouseRent = new JLabel("RENT");
    lblHouseRent.setFont(new Font("Times New Roman", Font.PLAIN, 20));
    lblHouseRent.setBounds(99, 53, 73, 35);
    panelHouse.add(lblHouseRent);

    JLabel lblHouseOwn = new JLabel("OWN");
    lblHouseOwn.setFont(new Font("Times New Roman", Font.PLAIN, 20));
    lblHouseOwn.setBounds(476, 66, 60, 22);
    panelHouse.add(lblHouseOwn);


    JButton btnHouseNext = new JButton("NEXT STATION");
    btnHouseNext.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {   
            Flash1.setVisible(true);
            panelHouse.setVisible(false);
        }
    });//Close housingNext button actionlistener
    btnHouseNext.setFont(new Font("Times New Roman", Font.PLAIN, 20));
    btnHouseNext.setBounds(344, 371, 184, 48);
    panelHouse.add(btnHouseNext);

    JLabel lblHousing = new JLabel("HOUSING");
    lblHousing.setFont(new Font("Times New Roman", Font.PLAIN, 22));
    lblHousing.setBounds(388, 11, 102, 27);
    panelHouse.add(lblHousing);

}//close initialize

    public JPanel getPanelHouse() {
        return panelHouse;
    }
}//close class body

Here is the second, outside class.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Flash extends JPanel{
    private static final long serialVersionUID = 1L;
    public Flash() {
        Arrow a = null;
        JPanel Flash1 = new JPanel();
        setBounds(0, 0, 10, 10);
        setLayout(null);
        setBackground(Color.orange);

        JButton buttonFlash = new JButton("NEXT STATION");
        buttonFlash.addActionListener(new ActionListener() {
        @SuppressWarnings("null")
        public void actionPerformed(ActionEvent arg0) {
            Flash1.setVisible(false);
            a.getPanelHouse().setVisible(true);
            }
        });
        buttonFlash.setFont(new Font("Times New Roman", Font.PLAIN, 20));
        buttonFlash.setBounds(344, 371, 184, 48);
        add(buttonFlash);
        setVisible(true);
        }
}

I need to initialize Arrow to something while in the Flash class, but it sends me a null void warning because technically there isn't anything there.

Any ideas or help would be awesome!

Josh
  • 71
  • 1
  • 2
  • 11
  • 1) `Flash1.setLayout(null);` Please don't use `null-layout`, instead use a layout manager or [combinations of them](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html), don't set the bounds manually, see [Null layout is evil](http://www.leepoint.net/GUI/layouts/nulllayout.html) and the answers in [this question](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) for more information about why you should avoid it... – Frakcool Feb 15 '17 at 23:28
  • 2) Please follow the [naming conventions](http://www.oracle.com/technetwork/java/codeconventions-135099.html) `Flash1` should start with a lowerCaseFirstLetter as UpperCaseFirstLetter is for classes only and not for variables or method names – Frakcool Feb 15 '17 at 23:28
  • 1
    What you seem to be describing is the functionality of MVC, what you're after is some kind of controller to make decisions about how to manage the `CardLayout`, you might consider having a look at [this example](http://stackoverflow.com/questions/29571722/java-application-with-multiple-scenes/29639672#29639672), [this example](http://stackoverflow.com/questions/31602113/listener-placement-adhering-to-the-traditional-non-mediator-mvc-pattern/31604919#31604919) and [this example](http://stackoverflow.com/questions/27663306/open-a-jpanel-after-pressing-a-button-in-a-jframe/27663749#27663749) – MadProgrammer Feb 15 '17 at 23:29
  • My program I plan on implementing this on is more or less a quiz. The user is give a series of options during the 1st half, then in 2nd half they receive questions they have to make decisions on based on previous answers. For example, if they chose option 1 they they get situation 1, if they chose option 2 they get situation 2. So would you suggest I attempt to figure out your middle example, or study the CardLayout page off the oracle website? @MadProgrammer – Josh Feb 16 '17 at 21:01
  • @Josh Both. Essentially, the `CardLayout` is a mechanism which will allow you to add n number of components to a container, but simply display one at a time. You can map the "key"/"name" used when adding the components to the `CardLayout` to your models/views so it's easier to figure out what you need to display when. Remember, a controller can be a controller for another controller ;) ... – MadProgrammer Feb 16 '17 at 21:34
  • @Josh that is, you could have a controller for the `CardLayout`, but still have a MVC for just the individual question, the "card layout controller" would then have the ability to make decisions about what should be displayed next, probably based on information coming from the currently active controller. All this would need to be mapped out in some meaningful way, I'd probably start with a clear idea of the decision tree on paper first – MadProgrammer Feb 16 '17 at 21:35
  • @MadProgrammer The problem is I have no experience in writing an MVC, controllers, or such. I am a computer science major, but that has never been covered and I am learning through YouTube and and stackoverflow. Would your middle example you posted the link for be the best option to attempt to replicate? And how much would I need to change the code. I am aiming for more or less the same endgame (I think). – Josh Feb 16 '17 at 21:38
  • @Josh MVC is a pretty common paradigm in programming, they really should have a class dedicated to software patterns, but they didn't teach me that at Uni either :P. The middle example might be a good place to start, but you're going to have to understand the concept and how it works and then figure out how to apply it to your needs. How much re-work is going to be involved? That's an impossible question to answer – MadProgrammer Feb 16 '17 at 22:07

0 Answers0