-
Refresh CardLayout
Hi
I have several JPanel in a CardLayout. One JPanel is for the login, the second is for the Profil information.
When I login, I set the user ID in "static public userID" then I which to the layout "Profil". The problem is that the profil label is set on mainCardLayout.userID and it is showing an empty String.
I'm assuming that because I've added my layout Login and Profil together in the init() the Profil is still set with empty userID String from the begining.
How do I refresh layout Profil from the cardlayout?
-
Probably it's me, but I'm a bit confused as to what you're doing and what problems you're having. Some suggestions:
- Clarify your problem including description, sample code, details of the problem itself. Describe everything as if we don't know what your code looks like, what your program structure, as if we can't read your mind.
- I worry when I see you mentioning static anything. I don't see anything in your description that suggests that any variables or methods should be static other than your main method.
- Is your problem that you're changing a String variable and not seeing a change reflected in a JLabel? If so, that makes sense since you can only change the JLabel by calling setText(...), that's it.
- What do you mean by "refresh layout"?
Best of luck!
-
I'm trying to change the label in page2 from page1. On load I've set the text to "default" but once it's done I can't change it anymore.
The reason why I'm using userID as static in mainCardLayout is to creat something similar to a session.
mainFrame
Code:
import javax.swing.JFrame;
public class mainFrame {
public static void main (String[] args) {
JFrame frame = new JFrame("Business");
frame.getContentPane().add(new mainCardPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
mainCardPanel
Code:
import java.awt.*;
import javax.swing.*;
public class mainCardPanel extends JPanel {
private CardLayout cardlayout = new CardLayout();
public static final String Card_Page1 = "Page1";
public static final String Card_Page2 = "Page2";
private Page1 page1 = new Page1(this);
private Page2 page2 = new Page2(this);
public static String userID = "default";
public mainCardPanel() {
setLayout(cardlayout);
add(page1, Card_Page1);
add(page2, Card_Page2);
}
public void swapPanel(String nextPanelName) {
cardlayout.show(this, nextPanelName);
super.validate();
}
}
Page1
Code:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Page1 extends JPanel{
private JButton tempBTN;
public Page1(final mainCardPanel JP) {
tempBTN = new JButton ("Change layout");
tempBTN.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
changePage(JP);
}
});
setPreferredSize (new Dimension (319, 222));
setLayout (null);
add (tempBTN);
tempBTN.setBounds (20, 10, 135, 25);
}
protected void changePage(mainCardPanel JP) {
JP.userID = "Changed text";
JP.swapPanel(JP.Card_Page2);
}
}
Page2
Code:
import java.awt.*;
import javax.swing.*;
public class Page2 extends JPanel{
private JLabel tempLB;
public Page2(mainCardPanel JP) {
tempLB = new JLabel (JP.userID);
setPreferredSize (new Dimension (319, 222));
setLayout (null);
add (tempLB);
tempLB.setBounds (20, 10, 107, 25);
}
}