Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: .show is called in card layout, but the frame didn't update

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    9
    My Mood
    Depressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default .show is called in card layout, but the frame didn't update

    I am not sure what is going on to be honest, I have another card layout system in my code and it works fine.

    Summary of what I noticed :
    1. there are no errors.
    2. when I try to switch from Panel A to Panel B, the whole thing freezed.
    3. when I try to switch from B to A, the .show() line has been ran, but A didn't show up and B keep running.
    4. println are put all over the place(I removed most of them now) and I am quite sure the .show() command is being excute but nothing happens.
    I'll be monitoring this post closely if any of you want addition information.

    Core.java (where the main is in, also the Frame and the card layout in question, this is the code that most likely to have something wrong with it)
    package TheGame;
     
    import java.awt.*;
    import javax.swing.*;
     
    public class Core extends JFrame {
     
    	public static Core instance;
    	JPanel contentPanel;
    	ResPanel resPanel;
    	OptPanel optPanel;
    	CardLayout cl;
    	JPanel container;
     
    	public static Core getInstance(){
    		if(instance == null){
    			instance = new Core();
    		}
    		return instance;
    	}
     
        private Core() {
        	cl = new CardLayout();
        	container = new JPanel();
        	container.setLayout(cl);
     
    		optPanel = new OptPanel();
        	contentPanel = new JPanel();
        	contentPanel.setLayout(new BorderLayout());
            resPanel = ResPanel.getInstance();
            contentPanel.add(resPanel, BorderLayout.NORTH);
     
            container.add(contentPanel, "2");
            container.add(optPanel, "3");
            container.setVisible(true);
            add(container);
            setSize(800, 600);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
       }
     
        public static void main(String[] args) {
            Core core = new Core();
            core.setLocationRelativeTo(null);
            core.setVisible(true);
            core.showContent();
    //      core.showOption();
        }
        public void showContent(){
        	cl.show(container, "" + 2);
        }
        public void showOption(){
        	cl.show(container, "" + 3);
        }
    }


    OptPanel.java (This is the option panel, the card layout .show method is being called at the end of the code in responce to the apply/cancel button being clicked)
    package TheGame;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class OptPanel extends JPanel implements ActionListener{
     
    	private Core core;
    	private JButton oCancel;
    	private JPanel down;
     
    	public OptPanel(){
    		down = new JPanel();
    		oCancel = new JButton("Cancel");
    		oCancel.addActionListener(this);
    		setLayout(new BorderLayout());
      		down.add(oCancel);
      		add(down, BorderLayout.SOUTH);
       		setVisible(true);
    	}
     
    	public void actionPerformed(ActionEvent e) {
    		JButton s = (JButton)e.getSource();
    		core = Core.getInstance();
        	if(s.getText().equals("Cancel")){
       			core.showContent();
        	}
    	}
    }

    ResPanel.java (This is my Resource Panel which is where the button to call the option menu is at, it is also located at the very end of the file)
    package TheGame;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class ResPanel extends JPanel implements ActionListener{
     
    	public static ResPanel instance;
    	private JButton rOption;
    	private Core core;
     
    	public static ResPanel getInstance(){
    		if(instance == null){
    			instance = new ResPanel();
    		}
    		return instance;
    	}
     
    	private ResPanel() {
    		rOption = new JButton("Options");
    		rOption.addActionListener(this);
     
      		setLayout(new FlowLayout());
       		add(rOption);
       		setVisible(true);
    	}
     
    	public void actionPerformed(ActionEvent e) {
    		JButton s = (JButton)e.getSource();
    		 core = Core.getInstance();
        	if(s.getText().equals("Options")){
    			core.showOption();
        	}
    	}
    }
    Last edited by nggdowt; November 9th, 2011 at 02:01 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,354
    Thanks
    182
    Thanked 832 Times in 775 Posts
    Blog Entries
    5

    Default Re: .show is called in card layout, but the frame didn't update

    It might help to boil the code down to an SSCCE. Trying to run code like this with multiple classes does not make it easy for us to try and debug to help you

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    9
    My Mood
    Depressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: .show is called in card layout, but the frame didn't update

    Solved t myself,
    in Core.java.

    The line
    Core core = new Core();
    in the main method should be
     Core core = Core.getInstance();

Similar Threads

  1. Replies: 1
    Last Post: November 2nd, 2011, 12:00 PM
  2. Replies: 1
    Last Post: April 14th, 2011, 08:50 AM
  3. Is it possible to use card layout for menu and menu items?
    By jai2rul in forum AWT / Java Swing
    Replies: 0
    Last Post: April 11th, 2011, 09:19 AM
  4. Beginner: Show Image in Label when Results Show Up
    By Big Bundy in forum Java Theory & Questions
    Replies: 3
    Last Post: April 4th, 2011, 03:43 PM
  5. Replies: 0
    Last Post: February 2nd, 2010, 08:20 AM