Results 1 to 8 of 8
  1. #1
    amcniece is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default jButtons to jPanels

    I am new to Netbeans and Swing

    I have a mainPanel with various buttons on the panel (jButtons)

    What is the easiest way to have each button show a new, different panel

    I.e
    jButton1 -> jPanel2
    jButton2 - > jPanel3
    etc.

    I have been using cardLayout for this but have been having trouble with the
    cl.show(Container, String) command.

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

    Default

    Quote Originally Posted by amcniece View Post
    I am new to Netbeans and Swing

    I have a mainPanel with various buttons on the panel (jButtons)

    What is the easiest way to have each button show a new, different panel

    I.e
    jButton1 -> jPanel2
    jButton2 - > jPanel3
    etc.

    I have been using cardLayout for this but have been having trouble with the
    cl.show(Container, String) command.
    CardLayout is the way to go, most definitely. What trouble are you having with that method?

    As a side note, many here feel that folks learn Swing coding better if they avoid using NetBean's code generation but rather learn to first code Swing by hand.

  3. #3
    amcniece is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    Mainly I am having trouble just understanding what parameters it is asking for and how those parameters relate to the panel that i am trying to reach.
    Last edited by amcniece; 05-13-2011 at 12:59 AM.

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

    Default

    Quote Originally Posted by amcniece View Post
    Mainly I am having trouble just understanding what parameters it is asking for and how those parameters relate to the panel that i am trying to reach.
    The first parameter is a reference to the container (probably a JPanel) that is using the CardLayout, that holds the "cards", and the second parameter is to the String that was used when you added a component to the CardLayout-using container. If you need more specific help, you may wish to show us some code.

  5. #5
    amcniece is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    Java Code:
    //Initializers
          JPanel cards;
                final String ACCOUNT = "Card with ACCOUNT INFO";
                final String DEPOSIT = "Card with DEPOSIT";
                final String WITHDRAW = "Card with WITHDRAW"; 
    
                //Where the components controlled by the CardLayout are initialized:
                //Create the "cards". Not using 'new' cause they have already been initialized.
                JPanel card1 = jPanel2;
                JPanel card2 = jPanel6;
                JPanel card3 = jPanel9;
    //Create the panel that contains the "cards".
                cards = new JPanel(new CardLayout());
                cards.add(card1, ACCOUNT);
                cards.add(card2, DEPOSIT);
                cards.add(card3, WITHDRAW);
    
    .....
    ....
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
          CardLayout cl = (CardLayout) (mainPanel.getLayout());
           cl.show( ? , ? );
    What would I use in cl.show if I wanted jButton1 to display jPanel2?

    cl.show(jPanel2, card2) ?

    Thanks for the help.
    Last edited by Fubarable; 05-13-2011 at 02:26 AM.

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

    Default

    Quote Originally Posted by amcniece View Post
    What would I use in cl.show if I wanted jButton1 to display jPanel2?

    cl.show(jPanel2, card2) ?

    Thanks for the help.
    I've added code tags to the code in your your post above to help make it readable.

    As mentioned previously, the first parameter to this method is a reference to the container that is using the CardLayout, here cards, and the second parameter is to the String that was used when you added a component to the CardLayout-using container, and for card2, that's DEPOSIT. So you could possibly do:

    Java Code:
    cl.show(cards, DEPOSIT);

  7. #7
    amcniece is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    I realize it is hard to figure out with the code not in hand.

    Code:
    cl.show(cards, DEPOSIT)

    Is coming back with errors
    'cannot find symbol'
    variable cards and variable DEPOSIT

    Any ideas?

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

    Default

    Quote Originally Posted by amcniece View Post
    I realize it is hard to figure out with the code not in hand.

    Code:
    cl.show(cards, DEPOSIT)

    Is coming back with errors
    'cannot find symbol'
    variable cards and variable DEPOSIT

    Any ideas?
    You need a reference to the cards JPanel and the DEPOSIT String at the point in your code where you're calling cl.show(...).

    I would make DEPOSIT and the other Strings constants -- public final static String. Then you could simply get a reference to it by dereferencing it off of the class it is contained in.

Similar Threads

  1. Replies: 2
    Last Post: 04-26-2011, 03:50 PM
  2. Problems regarding JPanels in JPanels
    By ColtonPhillips in forum AWT / Swing
    Replies: 2
    Last Post: 07-19-2010, 09:33 PM
  3. Help with JButtons...
    By ashton in forum New To Java
    Replies: 8
    Last Post: 01-26-2009, 10:38 AM
  4. JButtons
    By jadaleus in forum Advanced Java
    Replies: 4
    Last Post: 10-17-2008, 03:49 AM
  5. JButtons
    By fgasimzade in forum SWT / JFace
    Replies: 1
    Last Post: 12-25-2007, 06:39 AM

Posting Permissions

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