Results 1 to 3 of 3
  1. #1
    soulmed is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default 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?
    Newb today, Pro tomorrow,
    Soulmed

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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!

  3. #3
    soulmed is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    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
    Java 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
    Java 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
    Java 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
    Java 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);
        }
    }
    Newb today, Pro tomorrow,
    Soulmed

Similar Threads

  1. Question about cardlayout
    By scheffetz in forum New To Java
    Replies: 1
    Last Post: 04-02-2011, 09:05 PM
  2. Using a FlowLayout on top of a CardLayout
    By snieuw in forum New To Java
    Replies: 1
    Last Post: 11-08-2010, 07:03 PM
  3. Help with CardLayout
    By Kyle227 in forum New To Java
    Replies: 4
    Last Post: 05-28-2010, 02:03 AM
  4. Regarding CardLayout
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-07-2008, 08:52 PM
  5. Using previous with CardLayout
    By uncopywritable in forum New To Java
    Replies: 2
    Last Post: 08-05-2007, 10:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •