0
votes

I'm working on a GUI assignment and I encountered a problem that I cannot figure out. I have a JFrame with multiple JPanels, one of those JPanels contains a CardLayout with multiple JPanels. Since we are more people working on the project we decided to make a separate class (that extends JPanel) for each panel that's gonna be inside the CardLayout.

The problem is accessing components of the JFrame from the JPanel classes.

To give you an example, I have a JLabel somewhere in the JFrame that serves as a status bar and I want to change the text of the status bar when a button is pressed on the SaleMain panel (a class SaleMain that extends JPanel, contained in the CardLayout).

Another example, inside another panel EditCustomer (also a JPanel class, included in the CardLayout), I'd like to have a button with an action listener that will change the current panel (the one containing the button) to a different panel from the CardLayout.

Hope I made it as clear as possible, thank you guys in advance for helping me :)

1
I'd suggest creating a manager class in which you create all GUI objects and passing it to each of these objects so that when you want to interract with another e.g. Panel you do that through the manager classGeorgeG
So I make a new class in which I define all the components as instance variables and then in the JFrame I won't create a new JLabel for example , but just say " label = manager.getLabel() " , and when I want to change the label from the JPanel I will make method inside the manager and then call it from JPanel as manager.setLabel() and it will change in the JFrame as well ?Near
Create classes extending JFrame, JPanel and other GUI components that might need to be modified by other components. In the constructors make it so they need a Manager obj as a parameter. Create a Manager in which you initialize your JFrame and all the GUI components passing the Manager object as this. Then you define the getters for each of these components. When you want to modify a GUI component you execute something like managerObj.getPanel1().method()GeorgeG

1 Answers

3
votes

The model / view / controller pattern (MVC) is useful for creating a GUI. By separating your model from your view, you can concentrate on one part of your GUI at a time.

You create a model for your GUI that contains the information you want to present on your GUI.

I have a JLabel somewhere in the JFrame that serves as a status bar and I want to change the text of the status bar when a button is pressed on the SaleMain panel

Put the text in your model, and in the action listener for the button, you have the text put in the status bar.

I'd like to have a button with an action listener that will change the current panel (the one containing the button) to a different panel from the CardLayout.

Then do so. The action listener is a controller that can change the view.

Take a look at my article, Dice Game, to see how a Java Swing application implements the MVC pattern and JPanel switching.