user3771102
user3771102

Reputation: 558

How do I stop the cardlayout .next() and .previous() from looping

I have here a sample of a program here. I have 3 panels and I want to stop the user in pressing the previous button if the panel is in panel_1 and also stop at the end of panel_3 which is that last panel. Is there any way to like disable the button if the user is at the start of the panel or at end of the panel?

package cardlayoutalignment;

  import java.awt.*;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import javax.swing.*;
  import javax.swing.border.BevelBorder;
  import javax.swing.border.Border;
  import javax.swing.border.EmptyBorder;

public class gridbaglayoutdemo {
JFrame Card = new JFrame();

FlowLayout flow = new FlowLayout(FlowLayout.RIGHT,2,2);
Border etch = BorderFactory.createEtchedBorder(Color.white,Color.gray);
Border margin = new EmptyBorder(10,10,10,10);

public static GridBagLayout grid = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
final static boolean shouldFill = true;

JPanel container = new JPanel();
JPanel divider = new JPanel();
JPanel bodypanel = new JPanel();
final JPanel buttonpanel = new JPanel();
JPanel panel_1 = new JPanel();
    JPanel panel_2 = new JPanel();
    JPanel panel_3 = new JPanel();
CardLayout cl = new CardLayout();

public gridbaglayoutdemo(){

    Card.setVisible(true);
    Card.setSize(605,333);
    Card.setTitle("");
    Card.setResizable(false);

    final Toolkit toolkit = Toolkit.getDefaultToolkit();
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();      
    int x=(int)((dimension.getWidth() - Card.getWidth())/2);
    int y=(int)((dimension.getHeight() - Card.getHeight())/2);

    Card.setLocation(x, y);
    Card.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


    bodypanel.setLayout(new BorderLayout());

    divider.setLayout(new BorderLayout());

    container.setLayout(cl);
    cl.show(container, "1");

    panel_1.setLayout(grid);

    JLabel label_1 = new JLabel("Enter 1:");
    label_1.setFont(new Font("Arial", Font.PLAIN, 18));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 0;
    c.gridy = 0;
    c.insets = new Insets(10,10,0,0);
    panel_1.add(label_1, c);

    JComboBox box_1 = new JComboBox();
    box_1.setPreferredSize(new Dimension(200,30));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 0;
    c.gridy = 1;
    c.insets = new Insets(10,10,0,0);
    panel_1.add(box_1,c);

    JLabel label = new JLabel("");
    label.setFont(new Font("Arial", Font.PLAIN, 18));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.weighty = 1;
    c.gridx = 0;
    c.gridy = 2;
    c.insets = new Insets(10,0,0,0);
    panel_1.add(label, c);


    panel_2.setLayout(grid);

    JLabel label_2 = new JLabel("Enter 2:");
    label_2.setFont(new Font("Arial", Font.PLAIN, 18));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 0;
    c.gridy = 0;
    c.insets = new Insets(10,10,0,0);
    panel_2.add(label_2,c);

    JTextField text_2 = new JTextField();
    text_2.setPreferredSize(new Dimension(200,30));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 0;
    c.gridy = 1;
    c.insets = new Insets(10,10,0,0);
    panel_2.add(text_2,c);

    JLabel label_22 = new JLabel("");
    label_22.setFont(new Font("Arial", Font.PLAIN, 18));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.weighty = 1;
    c.gridx = 0;
    c.gridy = 2;
    c.insets = new Insets(10,0,0,0);
    panel_2.add(label_22, c);


    panel_3.setLayout(grid);

    JLabel label_3 = new JLabel("Enter 3:");
    label_3.setFont(new Font("Arial", Font.PLAIN, 18));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 0;
    c.gridy = 0;
    c.insets = new Insets(10,10,0,0);
    panel_3.add(label_3,c);

    JTextField text_3 = new JTextField();
    text_3.setPreferredSize(new Dimension(200,30));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 0;
    c.gridy = 1;
    c.insets = new Insets(10,10,0,0);
    panel_3.add(text_3,c);

    JLabel label_33 = new JLabel("");
    label_33.setFont(new Font("Arial", Font.PLAIN, 18));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.weighty = 1;
    c.gridx = 0;
    c.gridy = 2;
    c.insets = new Insets(10,0,0,0);
    panel_3.add(label_33, c);

    buttonpanel.setLayout(new FlowLayout(SwingConstants.RIGHT));
    buttonpanel.setBorder(new EmptyBorder(0,10,0,0));

    JButton btnBack = new JButton("< Back");
    btnBack.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        cl.previous(container);
        buttonpanel.repaint();
        }   
    });
    btnBack.setFont(new Font("Arial", Font.PLAIN, 20));
    btnBack.setFocusable(false);
    btnBack.setFocusTraversalKeysEnabled(false);
    btnBack.setFocusPainted(false);
    btnBack.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    btnBack.setPreferredSize(new Dimension(110, 40));
    btnBack.setBackground(new Color(224,223,227));
    buttonpanel.add(btnBack);

    JButton btnNext = new JButton("Next >");
    btnNext.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            cl.next(container);
            buttonpanel.repaint();
        }   
    });
    btnNext.setFont(new Font("Arial", Font.PLAIN, 20));
    btnNext.setFocusable(false);
    btnNext.setFocusTraversalKeysEnabled(false);
    btnNext.setFocusPainted(false);
    btnNext.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    btnNext.setPreferredSize(new Dimension(110, 40));
    btnNext.setBackground(new Color(224,223,227));
    buttonpanel.add(btnNext);

    final JButton btnCancel = new JButton("Cancel");
    btnCancel.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Window dialog = SwingUtilities.windowForComponent( btnCancel );
            dialog.dispose(); 
        }   
    });
    btnCancel.setFont(new Font("Arial", Font.PLAIN, 20));
    btnCancel.setFocusable(false);
    btnCancel.setFocusTraversalKeysEnabled(false);
    btnCancel.setFocusPainted(false);
    btnCancel.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    btnCancel.setPreferredSize(new Dimension(110, 40));
    btnCancel.setBackground(new Color(224,223,227));
    buttonpanel.add(btnCancel);

    JPanel numberpanel = new JPanel();
    numberpanel.setPreferredSize(new Dimension(221,0));
    numberpanel.setBorder(new EmptyBorder(10,0,0,10));
    numberpanel.setBorder(BorderFactory.createEtchedBorder(Color.white,Color.gray));
    numberpanel.setLayout(flow);

    JButton button_7 = new JButton("7");
    button_7.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }   
    });
    button_7.setFont(new Font("Arial", Font.PLAIN, 30));
    button_7.setFocusable(false);
    button_7.setFocusTraversalKeysEnabled(false);
    button_7.setFocusPainted(false);
    button_7.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    button_7.setPreferredSize(new Dimension(70, 70));
    button_7.setBackground(new Color(224,223,227));
    numberpanel.add(button_7);

    JButton button_8 = new JButton("8");
    button_8.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }   
    });
    button_8.setFont(new Font("Arial", Font.PLAIN, 30));
    button_8.setFocusable(false);
    button_8.setFocusTraversalKeysEnabled(false);
    button_8.setFocusPainted(false);
    button_8.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    button_8.setPreferredSize(new Dimension(70, 70));
    button_8.setBackground(new Color(224,223,227));
    numberpanel.add(button_8);

    JButton button_9 = new JButton("9");
    button_9.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }   
    });
    button_9.setFont(new Font("Arial", Font.PLAIN, 30));
    button_9.setFocusable(false);
    button_9.setFocusTraversalKeysEnabled(false);
    button_9.setFocusPainted(false);
    button_9.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    button_9.setPreferredSize(new Dimension(70, 70));
    button_9.setBackground(new Color(224,223,227));
    numberpanel.add(button_9);



    Card.add(bodypanel);
        bodypanel.add(divider, BorderLayout.WEST);
            divider.add(container, BorderLayout.NORTH);     
                container.add(panel_1, "1");
                container.add(panel_2, "2");
                container.add(panel_3, "3");
                //container.add(panel_4, "4");
                //container.add(p5.panel_5, "5");
                //container.add(p6.panel_6, "6");
            divider.add(buttonpanel, BorderLayout.SOUTH);
        bodypanel.add(numberpanel, BorderLayout.EAST);
  }

   public static void main(String[] args){
     //Use the event dispatch thread for Swing components
     EventQueue.invokeLater(new Runnable()
     {
    @Override
     public void run()
     {
     new gridbaglayoutdemo();         
     }
     });

     }
 }

Sorry for the not so short working code. I already trimmed some of it. That's the shortest ver I can do.

Upvotes: 1

Views: 803

Answers (2)

dic19
dic19

Reputation: 17971

"Is there any way to like disable the button if the user is at the start of the panel or at end of the panel?"

According to How to Use CardLayout and CardLayout API there's no direct way to do it.

But you can implement this feature easily keeping an int variable with current card number and checking its value againts 0 (for first card) or container's component count (for last card). For instance:

public class GridBagLayoutDemo { // note code convention!

    int currentCard = 0;
    Action backAction, nextAction;
    ...

    public GridBagLayoutDemo() {
        ...
        backAction = new AbstractAction("< Back") {
            @Override
            public void actionPerformed(ActionEvent e) {
                currentCard--;
                GridBagLayoutDemo.this.evaluateActions();
            }
        };

        nextAction = new AbstractAction("Next >") {
            @Override
            public void actionPerformed(ActionEvent e) {
                currentCard++;
                GridBagLayoutDemo.this.evaluateActions();
            }
        };

        JButton btnBack = new JButton(backAction);
        ...
        JButton btnNext = new JButton(nextAction);
        ...
    }

    private void evaluateActions() {
        backAction.setEnabled(currentCard > 0);
        nextAction.setEnabled(currentCard < container.getComponentCount() - 1);
    }
    ...
}

Addendum

Looking closer at CardLayout implementation, it would be really easy to have this feature implemented by default (unless I'm missing something):

public class CardLayout implements LayoutManager2,
                                   Serializable {

    /*
     * This creates a Vector to store associated
     * pairs of components and their names.
     * @see java.util.Vector
     */
    Vector vector = new Vector();

    /*
     * Index of Component currently displayed by CardLayout.
     */
    int currentCard = 0;

    ...

    /* 
     * Hypothetical implementations
     */

    public boolean isDisplayingFirstCard() {
        return currentCard == 0;
    }

    public boolean isDisplayingLastCard() {
        return currentCard == vector.size() - 1;
    }
}

Don't know why they didn't provide such useful feature.

Upvotes: 3

camickr
camickr

Reputation: 324128

Check out Card Layout Actions for a simple extension of the CardLayout.

It provides "Next" and "Previous" actions that you can use to create buttons or menu items and the Actions will automatically be disabled when you are at the end/beginning of the cards.

Upvotes: 2

Related Questions