Results 1 to 2 of 2
  1. #1
    adeeb is offline Member
    Join Date
    May 2008
    Posts
    29
    Rep Power
    0

    Default Regarding CardLayout

    Hi
    I know that in card layout we can add different cards and switch between them.
    Now i have add buttons as first, last, next and previous. And i am able to do so by using this buttons.Now what i need is to get a card directly on clicking a button say xyz. Imagine you have 15 cards and you need to get the 7th card on clicking the xyz button how to do it. Please some body help me.
    The code i used for first and last button is as follows for card layout with 4 cards.


    This is for first button

    firstb.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent arg0)
    {
    cl.first(panel);
    currcard=1;
    }
    });

    This is for last button

    firstb.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent arg0)
    {
    cl.last(panel);
    currcard=4;
    }
    });

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class CardLayoutTest implements ActionListener,
                                           ListSelectionListener {
        JPanel cardPanel;
    
        public void actionPerformed(ActionEvent e) {
            String ac = e.getActionCommand();
            CardLayout cards = (CardLayout)cardPanel.getLayout();
            if(ac.equals("FIRST"))
                cards.first(cardPanel);
            if(ac.equals("PREVIOUS"))
                cards.previous(cardPanel);
            if(ac.equals("NEXT"))
                cards.next(cardPanel);
            if(ac.equals("LAST"))
                cards.last(cardPanel);
        }
    
        public void valueChanged(ListSelectionEvent e) {
            if(!e.getValueIsAdjusting()) {
                String id = (String)((JList)e.getSource()).getSelectedValue();
                CardLayout cards = (CardLayout)cardPanel.getLayout();
                cards.show(cardPanel, id);
            }
        }
    
        private JPanel getCardContent() {
            CardLayout cards = new CardLayout();
            cardPanel = new JPanel(cards);
            String[] ids = { "red", "green", "blue", "yellow", "pink" };
            Color[] colors = {
                Color.red, Color.green, Color.blue, Color.yellow, Color.pink
            };
            for(int i = 0; i < ids.length; i++) {
                cardPanel.add(ids[i], getPanel(ids[i], colors[i]));
            }
            return cardPanel;
        }
    
        private JList getCardList() {
            String[] ids = { "red", "green", "blue", "yellow", "pink" };
            JList list = new JList(ids);
            list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            list.addListSelectionListener(this);
            return list;
        }
    
        private JPanel getPanel(String id, Color color) {
            JLabel label = new JLabel(id, JLabel.CENTER);
            label.setFont(label.getFont().deriveFont(18f));
            JPanel panel = new JPanel(new BorderLayout());
            panel.add(label);
            panel.setBackground(color);
            return panel;
        }
    
        private Box getButtonPanel() {
            Box box = Box.createHorizontalBox();
            box.add(Box.createHorizontalGlue());
            String[] ids = { "first", "previous", "next", "last" };
            for(int i = 0; i < ids.length; i++) {
                JButton button = new JButton(ids[i]);
                button.setActionCommand(ids[i].toUpperCase());
                button.addActionListener(this);
                box.add(button);
                box.add(Box.createHorizontalGlue());
            }
            return box;
        }
    
        public static void main(String[] args) {
            CardLayoutTest test = new CardLayoutTest();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test.getCardContent());
            f.add(test.getCardList(), "Before");
            f.add(test.getButtonPanel(), "Last");
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

Similar Threads

  1. Using previous with CardLayout
    By uncopywritable in forum New To Java
    Replies: 2
    Last Post: 08-05-2007, 09:43 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
  •