0

I'm having a hard time debugging my code and I want to completely change my code and so far I have this:

    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;
            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();

            JTextArea text_2;
            JTextArea text_3;


            String change = "Finish";
            final JButton btnNext;
            final JButton btnBack;
            int currentCard = 0;
            int cardflag = 0;

            AbstractAction backAction;
            AbstractAction nextAction;

        public gridbaglayoutdemo(){

                    Card.setVisible(true);
                    Card.setSize(605,333);
                    Card.setTitle("Tank Delivery");
                    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 = new JPanel(cl);
                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);

                text_2 = new JTextArea();
                text_2.setPreferredSize(new Dimension(200,30));
                c.fill = GridBagConstraints.HORIZONTAL;
                    c.weightx = 0.5;
                    c.weighty = 0;
                    c.gridx = 0;
                    c.gridy = 20;
                    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 = 30;
                    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);

                text_3 = new JTextArea();
                text_3.setPreferredSize(new Dimension(200,30));
                c.fill = GridBagConstraints.HORIZONTAL;
                    c.weightx = 0.5;
                    c.weighty = 0;
                    c.gridx = 0;
                    c.gridy = 20;
                    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 = 30;
                    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));


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

                    btnBack = new JButton("< Back");
                    btnBack.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent e) {
                                    cl.previous(container);
                                    buttonpanel.repaint();
                                    cardflag--;
                                    if (cardflag==0)
                                    {btnBack.setEnabled(false);}
                                    if(cardflag<3)
                                    {btnNext.setText("Next >");}
                            }   
                    });
                    btnBack.setEnabled(false);
                buttonpanel.add(btnBack);

                    btnNext = new JButton("Next >");
                    btnNext.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent e) {
                                    cl.next(container);
                                    buttonpanel.repaint();

                                    cardflag++;
                                    if(cardflag<3)
                                    {btnBack.setEnabled(true);}
                                    if(cardflag==2)
                                    {btnNext.setText(change);}
                                    if (cardflag==3)
                                    {cl.show(container, "3");
                                    JOptionPane.showMessageDialog(null, "DONE");
                                    Window dialog = SwingUtilities.windowForComponent( btnNext );
                                    dialog.dispose(); 
                                    cardflag=0;
                                    btnNext.setText("Next >");
                                    }

                                    validateText();


                            }   
                    });
                    btnNext.setVisible(true);
                buttonpanel.add(btnNext);

                    final JButton btnCancel = new JButton("Cancel");
                    btnCancel.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent e) {
                                cardflag=0;
                                    Window dialog = SwingUtilities.windowForComponent( btnCancel );
                                    dialog.dispose(); 
                            }   
                    });
                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) {
                            buttonaction(e);
                            }   
                    });
                    button_7.setActionCommand("7");
                numberpanel.add(button_7);

                JButton button_8 = new JButton("8");
                    button_8.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent e) {
                            buttonaction(e);
                            }   
                    });
                    button_8.setActionCommand("8");
                numberpanel.add(button_8);

                JButton button_9 = new JButton("9");
                    button_9.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent e) {
                            buttonaction(e);
                            }   
                    });
                    button_9.setActionCommand("9");
                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");

                            divider.add(buttonpanel, BorderLayout.SOUTH);
                    bodypanel.add(numberpanel, BorderLayout.EAST);
        }

            private void buttonaction(ActionEvent e){
                try{
                if(cardflag==1)
                    {text_2.append("" + e.getActionCommand());}
                if(cardflag==2)
                    {text_3.append("" + e.getActionCommand());}

                }catch(Exception e1){}
            }

            private void validateText(){
                if(cardflag==2)
                {String text = text_2.getText();
                    if (text.isEmpty()==true)
                    {JOptionPane.showMessageDialog(null, "Text 2 is empty!");
                    cl.show(container, "2");
                    btnNext.setText("Next >");
                    cardflag--;
                    }
                }
            }



    }

Is there any way that I can change the action of the next and previous button? My code is dependent on the cardflag which is a checker whether the next button is clicked or not. And so is there another way to make the next and previous button just like that but different method?

user3771102
  • 558
  • 2
  • 8
  • 27
  • CardLayout can returns min (card can be removable) and max index, then is possible to set JButton.setEnabled – mKorbel Aug 25 '14 at 08:16
  • Create some kind of model has some concept of the views (or at least the view names) which can help provide information back to the controller and view – MadProgrammer Aug 25 '14 at 08:22
  • @MadProgrammer what do you mean by that? – user3771102 Aug 25 '14 at 08:25
  • @mKorbel do you refer to `minimumLayoutSize` and `maximumLayoutSize`? – user3771102 Aug 25 '14 at 08:28
  • @user3771102 not, its about numbers of cards added to CardLayout – mKorbel Aug 25 '14 at 08:30
  • @mKorbel hmm. I see. but I don't get it.. – user3771102 Aug 25 '14 at 08:33
  • @MadProgrammer I already know what you mean. I'll try to create my program with the default controller. – user3771102 Aug 25 '14 at 08:38
  • @user3771102 as shortcut you can use get/putClientProperty, to multiply this value, then is possible to create an sceleton, you can to use PropertyChangeSupport/Listener as notifier for model v.s. view, [and quite simply to testable by...](http://stackoverflow.com/q/10880326/714968) – mKorbel Aug 25 '14 at 08:43
  • 1
    why is there buttonpanel.repaint();, really is there intensive painting based on AWTs peers (OpenGL/CL) – mKorbel Aug 25 '14 at 08:46
  • @mKorbel sorry about that.. – user3771102 Aug 25 '14 at 08:57
  • 1
    please to search in [posts by @Hovercraft Full Of Eels](http://stackoverflow.com/search?q=user%3A522444+cardlayout+propertychangelistener) hes loves PropertyChangeSupport/Listener – mKorbel Aug 25 '14 at 09:00

1 Answers1

0

Too much code to look at.

If you are just trying to enable/disable the "Next" and "Previous" button as you move from card to card then check out Card Layout Actions which provides this support for you.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I have done that before and it didn't work. The buttons is always disabled. As I have done here [link](http://stackoverflow.com/questions/24866220/how-do-i-stop-the-cardlayout-next-and-previous-from-looping?rq=1) – user3771102 Aug 26 '14 at 06:53
  • @user3771102 `I have done that before and it didn't work.` where did you do it? You have not posted any code using the RXCardLayout. You already accepted another answer in that posting so why are you asking another question if the solution worked? Create a `SSCCE` that demonstrates your problem when you use the `RXCardLaout`. All you need to do is create 3 or 4 panels of a different color without any components to simulate multiple panels. Get the `SSCCE` working first to prove it works then figure out why your real code is different. – camickr Aug 27 '14 at 03:30