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 4 of 4

Thread: CardLayout which between JPanels

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default CardLayout which between JPanels

    I want to switch between JPanels by clicking buttons on the JPanels.

    For example:
    I have a JPanel sim with a JButton simknop and a JPanel help with JButton helpknop
    I want to switch between these 2 JPanels by clicking the buttons. When I click JButton simknop JPanel help should
    appear and when I click JButton help JPanel sim should appear.

    Below you can find the different classes:
    package testcardlayout;
     
    import java.awt.CardLayout;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class main extends JFrame
    {
     
    	JPanel cards;
    	sim sim;
    	help help;
     
    	public main()
    	{
    		this.setSize(1024,768);
    		//this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setTitle("Crazy Bombardement");
    		this.setLocation(800, 100);//standaard in de hoek van het scherm
     
    		cards = new JPanel();
    		cards.setLayout(new CardLayout());
    		sim = new sim();
    		help = new help();
     
    		cards.add(sim, "SIM");
    		cards.add(help, "HELP");	
     
    		this.add(cards);
    		this.setVisible(true);
    	}
     
    	public static void main(String[] args) 
    	{
    		new main();
    	}
     
    }

    package testcardlayout;
     
    import java.awt.CardLayout;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JPanel;
     
    public class sim extends JPanel
    {
    	JButton simknop;
     
    	public sim()
    	{
    		simknop = new JButton("simknop");
    		this.add(simknop);
    		this.setBackground(Color.black);
    	}
     
    }

    package testcardlayout;
     
    import java.awt.Color;
     
    import javax.swing.JButton;
    import javax.swing.JPanel;
     
    public class help extends JPanel
    {
    	JButton helpknop;
     
    	public help()
    	{
    		helpknop = new JButton("helpknop");
    		this.add(helpknop);
    		this.setBackground(Color.red);
    	}
     
    }

    I want to use CardLayout for this but I can't figure out how to make it work for it to listen to different ActionListeners.

    Any help is greatly appreaciated!

    Thank you in advance


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: CardLayout which between JPanels

    by clicking buttons
    That requires that the buttons have action listeners.
    See the tutorial: How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
    and also for the layout:
    http://docs.oracle.com/javase/tutori...yout/card.html

    BTW java coding conventions suggest that class names should start with uppercase letters.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: CardLayout which between JPanels

    Thank you for the reply.

    so I did addActionListener(this) to every button and implemented ActionListener with actionPerfomed method.
    But I don't understand fully how to change the Card now in cardlayout. I've already read the link you've provided,
    in the example of CardLayout they don't use seperate JPanels to change between cards (that what I'm trying to do)

    PS: i wrote these classes quickly as an example to ask my question here, thank you for reminding about the uppercase letters.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: CardLayout which between JPanels

    For a better understanding of how CardLayout works, copy the example program from the tutorial and experiment with it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. awt cardlayout help
    By op117 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 6th, 2012, 07:12 AM
  2. Problem with CardLayout
    By FireStorm in forum AWT / Java Swing
    Replies: 1
    Last Post: March 30th, 2012, 03:45 PM
  3. How to use CardLAyout without using applets???
    By divyarattan in forum AWT / Java Swing
    Replies: 4
    Last Post: August 9th, 2011, 11:19 AM
  4. Cardlayout Question
    By wagb278 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 10th, 2011, 03:23 PM
  5. Cardlayout help
    By phantomswordsmen in forum AWT / Java Swing
    Replies: 1
    Last Post: December 22nd, 2010, 02:36 PM