Now I know there are many, many questions on this and I've read a dozen. But I've just hit a wall, I can't make heads or tails of it.
Heres my question.
I have 3 Panel classes.
ConfigurePanel.java
ConnectServerPanel.java
RunServerPanel.java
and my JFrame class
StartUPGUI.java
This is what is initialised at startup
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
startUp = new sjdproject.GUI.ConfigurePanel();
runServer = new sjdproject.GUI.RunServerPanel();
serverConnect = new sjdproject.GUI.ConnectServerPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
jPanel1.setLayout(new java.awt.CardLayout());
jPanel1.add(startUp, "card2");
jPanel1.add(runServer, "card4");
jPanel1.add(serverConnect, "card3");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(27, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 419, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(38, 38, 38))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(30, Short.MAX_VALUE))
);
My StartUPGUI calls the StartUpPanel first. In my StartUpPanel.java I have a button which calls the setPanel method in StartUPGUI
StartUpGUI s = new StartUpGUI();
String str = "";
if(runserverbtn.isSelected()){
str = "runserver";
}
else if(connectserverbtn.isSelected()){
str = "connectserver";
}
else{
str = "";
}
s.setPanel(str);
Here is my setPanel method
void setPanel(String str){
if(str == "runserver"){
}
else if(str == "connectserver"){
}
else{
}
}
What do I need to put inside the if blocks to change panel views? I would have assumed jPanel1.something() but I don't know what that something is.
Don't compare string with
==
, it will not work. Use.equals
..if("runserver".equals(str)){
You need to use the method
.show
from theCardLayout
public void show(Container parent, String name)
- Flips to the component that was added to this layout with the specified name, using addLayoutComponent. If no such component exists, then nothing happens.See How to Use CardLayout for more details and see the API for more methods.
UPDATE
Try running this example and examine it with your code to see if you notice anything that will help
UPDATE 2
The problem is, the the button is in the
ConfigurePanel
class. Trying to create a newStartUPGUI
in that class, won't reference the same components. What you need to do is pass a reference of theStartUPGUI
to theConfigurePanel
. Something like thisAnd instantiate
ConfigurePanel
from theStartUPGUI
like this