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: Cardlayout Question

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Cardlayout Question

    I am experimenting with Cardlayout and have hit a snag. I hope I can explain this so it is understandable.

    I have three (for now) JPanels that are added to the Cardlayout and I can switch between them using menu items and buttons on the Toolbar of the JFrame. These JPanels are classes created using NetBeans GUI designer (each panel is a different Class). The contents of the Panels (cards) are set by the constructor of the Panel; for example a text field in the panel has the value set at time of panel construction.

    MY QUESTION is how do I set values when a panel (card) is displayed? These panels will eventually be database record data entry & edit screens. So, when a card is displayed it will need to access a database and populate fields from the result set.

    Can I create a method of the Panel that gets control when the Panel becomes active (top card, visible, etc.)? I tried adding a ComponentListener for when a panel is shown, but that didn't work (get control). Can I call a method of the Panel's Class in the button action that switches panels (cards)? If so, how? I can't seem to find the right code to access a public method of the Panel class from the Class of the main Frame.

    Hope you understand what I'm asking and can help.
    Thanks, Jim
    Last edited by wagb278; April 10th, 2011 at 03:26 PM. Reason: Solved


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,270
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Cardlayout Question

    There are many ways you could go about doing this. Load the data upon instantiation of the JPanels, load when you change the CardLayout (in an actionListener), add additional actionListeners to the buttons/menu items which fire a controller to load the data. The most straightforward approach would be just to load the data when you instantiate the JPanels, pass the data to each JPanel which is then responsible for placing the info in the correct place.

  3. The Following User Says Thank You to copeg For This Useful Post:

    wagb278 (April 10th, 2011)

  4. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cardlayout Question

    Thanks @copeg - I discovered my problem and fixed it. Basically I was using the wrong Class name in my code. I can now invoke the public method of my JPanel (card) class to update values when I switch to that card.

Similar Threads

  1. Cardlayout help
    By phantomswordsmen in forum AWT / Java Swing
    Replies: 1
    Last Post: December 22nd, 2010, 01:36 PM