Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By gimbal2

Thread: Having trouble with CardLayout ? Please help..

  1. #1
    Join Date
    Dec 2013
    Location
    Dublin
    Posts
    32
    Rep Power
    0

    Default Having trouble with CardLayout ? Please help..

    Hi my name is Eric.

    I am pretty new to Java and programming in general. I am currently doing a work experience module where me and many other inexperience Java programmers are working on a program.

    I am trying to use Cardlayout so we can seamlessly switch between panels when required.

    I have a number of classes that extend JPanel and in their constructors I have placed the relavent buttons, textfields etc for the specific panel.

    I am using one JFrame where I have set the background to the required image and colour. The panels I am using are transparent so the image is always visible and the buttons sit over it.

    My problem is I cannot get the panels to appear when using CardLayout. I have been messing around with this for a number of days.

    I can get CardLayout to work with normal JPanels and it s leading me to believe it may have something to do with the fact I am using subclasses of JPanel.

    I really do not know and would some help and advice on the matter. Below I will post one JPanel class, my BaseFrame class and my CardLayout class should you need to see more code please let me know..

    BaseFrame class:

    Java Code:
    package com.uandi.arqhive.frame;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class BaseFrame extends JFrame{
    
    	private static final long serialVersionUID = 1L;
    	private JPanel iconPanel;
    	private ArqhiveImage iconImage;
    	private JLabel imageLabel;
    	private JLabel webLabel;
    
    	public BaseFrame(){
    		
    		this.setLayout( null );
    		iconImage = new ArqhiveImage();
    		imageLabel = new JLabel( new ImageIcon( iconImage.getArqhiveImage() ));
    		webLabel = new JLabel( "www.uandimusicagency.com" );
    
    		
    		try {
                this.setContentPane( new JPanel() {
                    BufferedImage image = ImageIO.read(new File(
                            "C:\\Users\\Eric\\Documents\\Java\\EclipseProjects"
                            + "\\ArQhive\\src\\res\\Gui_bkgrnd.jpg" ));
                    
                    public void paintComponent(Graphics g) {
                        super.paintComponent(g);
                        g.drawImage( image, 0, 0, 820, 622, this );
                    } // end paintCompnent
                }); // end of buffered image
            } catch ( IOException exception ) {
                System.out.println( "Image cannot be found !!" );
            } // end catch
    		
    		this.getContentPane().setLayout( null );
    		
    		iconPanel = new JPanel();
    		iconPanel.add( imageLabel );
    		iconPanel.setBackground( Color.black );
    		iconPanel.setBounds( 140, 210, 358, 85 );
    		add( iconPanel );
    		
    		webLabel.setFont( new Font( "Arial Black", Font.BOLD, 13 ));
    		webLabel.setForeground( Color.WHITE );
    		webLabel.setBounds( 260, 290, 240, 25 );
    		add( webLabel );
    		
    		
    
    	} // end of constructor
    } // end of class
    RegisterPanel:

    Java Code:
    package com.uandi.arqhive.panels;
    
    import java.awt.Color;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    import com.uandi.arqhive.frame.BaseFrame;
    import com.uandi.arqhive.login.ArqhiveButton;
    import com.uandi.arqhive.login.ArqhiveField;
    
    public class RegisterPanel extends JPanel //implements FocusListener
    {
    
    	private ArqhiveField name;
    	private ArqhiveField email;
    	private ArqhiveField organizationName;
    	private ArqhiveField password;
    	private ArqhiveField passConfirmed;
    	private ArqhiveField[] focusArray = { 
    			name, email, organizationName, password, passConfirmed };
    	
    	private JComboBox< String > registerType;
    	
    	private String[] customerTypes = { "Artist", "Client" };
    	private String organization = "Organization";
    	
    	private ArqhiveButton register;
    	private ArqhiveButton back;
    	
    	
    	public RegisterPanel(){
    		
    		setOpaque( false );
    		setLayout( null );
    		setSize( 800, 620 );
    		
    		registerType = new JComboBox< String >( customerTypes );
    		registerType.setBackground( Color.BLACK );
    		registerType.setForeground( Color.WHITE );
    		registerType.setBounds( 610, 20, 150, 20 );
    		add( registerType );
    		
    		name = new ArqhiveField( "Full Name" );
    		name.setBounds( 610, 50, 150, 20 );
    		name.setCaretColor( Color.WHITE );
    		add( name );
    		
    		email = new ArqhiveField( "Email" );
    		email.setBounds( 610, 80, 150, 20 );
    		email.setCaretColor( Color.WHITE );
    		add( email );
    		
    		organizationName = new ArqhiveField( organization );
    		organizationName.setBounds( 610, 110, 150, 20 );
    		organizationName.setCaretColor( Color.WHITE );
    		add( organizationName );
    		
    		password = new ArqhiveField( "Password" );
    		password.setBounds( 610, 140, 150, 20 );
    		password.setCaretColor( Color.WHITE );
    		add( password );
    		
    		passConfirmed = new ArqhiveField( "Confirm Password" );
    		passConfirmed.setBounds( 610, 170, 150, 20 );
    		passConfirmed.setCaretColor( Color.BLACK );
    		add( passConfirmed );
    		
    		registerType.addFocusListener(
    				new FocusListener(){
    			
    					@Override
    					public void focusGained(FocusEvent e) {
    						organization = "Band Name";
    						
    					} // focusGained
    
    					@Override
    					public void focusLost(FocusEvent e) {
    						organization = "Company Name";
    						
    					} // focusLost
    				}); // addItemListener()
    		
    		
    	} // constructor
    	
    	public static void main(String[] args) {
    		
    		BaseFrame bf = new BaseFrame();
    		RegisterPanel rp = new RegisterPanel();
    		bf.setVisible( true );
    		bf.setSize( 800, 620 );
    		bf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    		bf.add( rp );
    	
    	} // main
    	
    } // class
    TestRun:

    Java Code:
    package com.uandi.arqhive.panels;
    
    import java.awt.CardLayout;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    import com.uandi.arqhive.frame.BaseFrame;
    import com.uandi.arqhive.login.ArqhiveButton;
    import com.uandi.arqhive.login.HomePanel;
    
    public class TestRun extends BaseFrame{
    	
    	private CardLayout layout;
    	private JPanel master;
    	
    	private WelcomePanel welcome;
    	private ArqhiveButton login;
    	private ArqhiveButton reg;
    	private ArqhiveButton info;
    	
    	private HomePanel home;
    	private RegisterPanel register;
    
    	public TestRun(){
    		
    		setLayout( null );
    		
    		layout = new CardLayout();
    		master = new JPanel( layout );	
    		
    		welcome = new WelcomePanel();
    		master.add( welcome, "one" );
    		
    		home = new HomePanel();
    		master.add( home, "two" );
    		
    		register = new RegisterPanel();
    		master.add( register, "three" );
    		
    		layout.show( master, "one" );
    		layout.layoutContainer( master );
    		add( master );
    		
    		
    		this.setVisible( true );
    		this.setSize( 800, 620 );
    		this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    		
    		
    	} // constructor
    	
    	public static void main(String[] args) {
    		
    		TestRun run = new TestRun();
    		
    	} // main
    	
    } // class
    Again should you need to see my other classes pleases let me know.

    Thank you...

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,016
    Rep Power
    6

    Default Re: Having trouble with CardLayout ? Please help..

    As soon as I spot setLayout(null), generally there is no need to dig any further. Remove those lines so you ARE using layout managers and see if that makes a difference. At this point your target is to get something to appear, the step after that will be to get it to appear the way you want it to. With layout managers.
    Castro__711 likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Join Date
    Dec 2013
    Location
    Dublin
    Posts
    32
    Rep Power
    0

    Default Re: Having trouble with CardLayout ? Please help..

    Thanks for the swift reply Gimbal2. Really appreciate it.

    I have commented out the setLayout( null ); in all of my classes with no change.

    When I do not use CardLayout and just add the panels to the frame they appear perfect it is only when I use CardLayout that no panels appear?

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,016
    Rep Power
    6

    Default Re: Having trouble with CardLayout ? Please help..

    I don't have too much experience with CardLayout I'm afraid, you'd have to wait for one of our local Swing experts to spot this thread. Did you play with the examples you can find in the Swing tutorial?

    How to Use CardLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Join Date
    Dec 2013
    Location
    Dublin
    Posts
    32
    Rep Power
    0

    Default Re: Having trouble with CardLayout ? Please help..

    No problem thanks for your help Gimbal2. I have watched afew online tutorials and have it working in a simplistic program but cant get it going in this.

    Again thanks for your input and take care.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,016
    Rep Power
    6

    Default Re: Having trouble with CardLayout ? Please help..

    Oh wait, I see you use setSize() everywhere. Try setPreferredSize() in stead!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Join Date
    Dec 2013
    Location
    Dublin
    Posts
    32
    Rep Power
    0

    Default Re: Having trouble with CardLayout ? Please help..

    Thanks but I am sure that has little bearing on my issue.

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,016
    Rep Power
    6

    Default Re: Having trouble with CardLayout ? Please help..

    Without trying, you can't be sure. You don't call setSize() yourself, that is something Swing must do for you with the help of the layoutmanagers.

    The fact that "things don't appear" is a further hint that you're doing something wrong in that area. I wouldn't dismiss it so easily if I were you.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    Join Date
    Dec 2013
    Location
    Dublin
    Posts
    32
    Rep Power
    0

    Default Re: Having trouble with CardLayout ? Please help..

    Thanks again man but when you set your layout manager to null and you are using setBounds() to get complete control on your placement of buttons etc .

    Anyways I have since tried it and no joy.

    I want my JFrame and JPanels that size so naturally I use setSize()..

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: Having trouble with CardLayout ? Please help..

    Mixing layout management and absolute positioning is asking for trouble. I suggest you use setPreferredSize on your various panels. Then issue a Frame.pack() to validate and resize the frame. The goal here is to get CardLayout working. You can tweak the placement of components later. Take one step at a time and do not combine goals. And using CardLayout is nicely explained in the tutorials. Did you check them out as Gimbal suggested?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,016
    Rep Power
    6

    Default Re: Having trouble with CardLayout ? Please help..

    yeah if you are going to stick to using null-layouts, then I am not going to even attempt to further help you. Good luck with that.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    Join Date
    Dec 2013
    Location
    Dublin
    Posts
    32
    Rep Power
    0

    Default Re: Having trouble with CardLayout ? Please help..

    I have seen the tutorials and have watched some youtube videos before gimbal suggested.. I have a fairly basic understanding I have got it working on a simple level but cannot with the above code.. I appreciate your help Jim thanks..

    Gimbal I have already thanked you for your contribution, if you think you have given me notable help though you may think again.

Similar Threads

  1. CardLayout... maybe
    By NotTooOld in forum New To Java
    Replies: 0
    Last Post: 05-07-2013, 04:45 PM
  2. CardLayout Help
    By David M. in forum New To Java
    Replies: 4
    Last Post: 08-06-2011, 02:57 AM
  3. CardLayout Trouble
    By eLancaster in forum AWT / Swing
    Replies: 2
    Last Post: 06-23-2011, 11:04 AM
  4. Help with CardLayout
    By Kyle227 in forum New To Java
    Replies: 4
    Last Post: 05-28-2010, 01:03 AM
  5. Regarding CardLayout
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-07-2008, 07:52 PM

Tags for this Thread

Posting Permissions

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