Results 1 to 6 of 6
  1. #1
    jazob is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Card Layout with Buttons

    I've been looking at Card Layout, and I was curious about how I could use buttons with it.

    The GUI I have currently navigates between the two cards with a "previous" and "next" button. However, I would like to know if I can call the card I want to appear by name, I know my commented code is incorrect. I looked at the API, and the addLayoutComponent(String name, Component comp) method is deprecated, but it's been replaced by another addLayoutComponent method. If this is part of the solution, I don't understand how I will be able to call my cards by name later.

    Or, would it be better to just use ItemListener and a combo box?

    Here is the code:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class CardsDemo
    {
      public static void main(String[] args) 
      {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            createAndShowGUI();
          }
        });
      }
      private static void createAndShowGUI() 
      {
        JFrame frame = new JFrame("CardLayoutDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
        CardLayoutDemo demo = new CardLayoutDemo();
        demo.addComponentToPane(frame.getContentPane());
        
        frame.pack();
        frame.setVisible(true);
      }
    }
    
    class CardLayoutDemo implements ActionListener 
    {
      JPanel cards; 
      final static String BUTTONPANEL = "Card with JButtons";
      final static String TEXTPANEL = "Card with JTextField";
      
      public void addComponentToPane(Container pane) 
      {
        JPanel selection = new JPanel();
        JButton first = new JButton("Previous");
        JButton second = new JButton("Next");
        first.addActionListener(this);
        second.addActionListener(this);
        first.setActionCommand("prev");
        second.setActionCommand("next");
        selection.add(first);
        selection.add(second);
    
        JPanel card1 = new JPanel();
        card1.add(new JButton("Button 1"));
        card1.add(new JButton("Button 2"));
        card1.add(new JButton("Button 3"));
        
        JPanel card2 = new JPanel();
        card2.add(new JTextField("TextField", 20));
        cards = new JPanel(new CardLayout());
        cards.add(card1, BUTTONPANEL);
        cards.add(card2, TEXTPANEL);
        
        pane.add(selection, BorderLayout.PAGE_START);
        pane.add(cards, BorderLayout.CENTER);
      }
      
      public void actionPerformed(ActionEvent evt)
      {
        CardLayout cl = (CardLayout)(cards.getLayout());
        if (evt.getActionCommand().equals("prev"))
        {
          cl.previous(cards);
        }
        if (evt.getActionCommand().equals("next"))
        {
          cl.next(cards);
        }
        //
        //cl.show(cards, evt.getActionCommand());
        //
      }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Card Layout with Buttons

    However, I would like to know if I can call the card I want to appear by name
    That is the approach taken in the How to Use Card Layout page of Oracle's Tutorial.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Card Layout with Buttons

    Moved to AWT/Swing forum.

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

    Default Re: Card Layout with Buttons

    For example, if you add to your code above:

    Java Code:
    class CardLayoutDemo implements ActionListener {
       JPanel cards;
       final static String BUTTONPANEL = "Card with JButtons";
       final static String TEXTPANEL = "Card with JTextField";
       private static final String[] COMBO_STRINGS = {BUTTONPANEL, TEXTPANEL};
    
       public void addComponentToPane(Container pane) {
          JPanel selection = new JPanel();
          JButton first = new JButton("Previous");
          JButton second = new JButton("Next");
          first.addActionListener(this);
          second.addActionListener(this);
          first.setActionCommand("prev");
          second.setActionCommand("next");
          selection.add(first);
          selection.add(second);
          
          final JComboBox combo = new JComboBox(COMBO_STRINGS);
          selection.add(combo);
    
          combo.addActionListener(new ActionListener() {
             
             @Override
             public void actionPerformed(ActionEvent evt) {
                CardLayout cl = (CardLayout) cards.getLayout();
                cl.show(cards, combo.getSelectedItem().toString());
             }
          });

  5. #5
    jazob is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: Card Layout with Buttons

    Thank you for the post, I've never seen a combo box implemented like that, so it's good to know.
    I guess that trying to use the show method with JButtons is not ideal.
    Thanks for the help.

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

    Default Re: Card Layout with Buttons

    Quote Originally Posted by jazob View Post
    I guess that trying to use the show method with JButtons is not ideal.
    Why say that?


    Thanks for the help.
    You're welcome!

Similar Threads

  1. Image in Card Layout
    By poorbrain in forum Java Applets
    Replies: 4
    Last Post: 03-08-2011, 07:15 PM
  2. Question Card Layout, Card Management
    By lrichil in forum AWT / Swing
    Replies: 1
    Last Post: 04-22-2010, 11:11 AM
  3. Edit layout Layout please help me
    By manhtungtnk28@gmail.com in forum New To Java
    Replies: 4
    Last Post: 11-23-2009, 09:41 AM
  4. Card
    By hedonist in forum New To Java
    Replies: 3
    Last Post: 08-13-2009, 03:20 PM
  5. Card Layout
    By Gilbee in forum NetBeans
    Replies: 3
    Last Post: 03-03-2009, 10:37 PM

Posting Permissions

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