-1

i am design swing desktop application for restaurant in which i need menu groups forward or go back required two button Next/Prev just i make Next/Prev buttons that forward or go back this is not work correctly my code like a make pagination buttons my menu group not work correct.

following image screen shot

Image#01

Image2

Global Variables

public Connection con;
    public PreparedStatement pst;
    public ResultSet rs;
    int totalRecord;
    private int pageIndex = 0;
    int pageSize = 5;
    int pageSize1 = 6;

Menu Group Function code

public void loadgroup(int category_id) {

        // this.pageIndex1 = page;
        try {

            Connection con5 = connectionDB.conDb();
            String s2 = "SELECT id, name,btn_color,text_color FROM `menu_group` where cat_id='" + category_id + "'";
            PreparedStatement pst2 = con5.prepareStatement(s2);
            ResultSet rs2 = pst2.executeQuery();
            groupPnl.removeAll();
            revalidate();

           // Connection con4 = connectionDB.conDb();
           // String test1 = "SELECT COUNT(*)as rowCounts from menu_group";
           // PreparedStatement pst3 = con5.prepareStatement(test1);
           // ResultSet rs3 = pst3.executeQuery();
           // int dbss = rs3.getInt("rowCounts");

            if (rs2.next() == false) {

                groupPnl.removeAll();
                groupPnl.revalidate();
                groupPnl.repaint();
                groupPnl.setVisible(false);
                right.setVisible(false);
                left.setVisible(false);

            } else {

                groupPnl.revalidate();
                groupPnl.setVisible(true);
                right.setVisible(true);
                left.setVisible(true);

                do {
                    //  rs2.first();
                    String a1 = rs2.getString(2);
                    String a2 = rs2.getString(1);
                    int a3 = rs2.getInt("btn_color");
                    int a4 = rs2.getInt("text_color");

                    PosButton btn = new PosButton();

                    btn.setText(a1);
                    btn.setVerticalAlignment((int) JButton.CENTER_ALIGNMENT);
                    btn.applyComponentOrientation(getComponentOrientation());
                    btn.setFocusPainted(false);
                    btn.setFocusable(false);
                    btn.setRequestFocusEnabled(false);
                    btn.setMaximumSize(new Dimension(80, 80));
                    btn.setPreferredSize(new Dimension(40, 35));
                    btn.setMinimumSize(new Dimension(80, 80));
                    btn.setFocusPainted(true);
                    btn.setBorderPainted(true);
                    btn.setBackground(new Color(a3, true));
                    btn.setForeground(new Color(a4, true));

                    btn.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            btn.setSelected(true);
                            System.out.println(a1 + ":");
                            btn.isSelected();
                            int idss = Integer.parseInt(a2);
                            // loadbtn();
                        }
                    });

                    //String a2 = rs.getString(2);
                    btn.setHorizontalAlignment(SwingConstants.CENTER);
                    btn.setHorizontalTextPosition(AbstractButton.CENTER);
                    btn.setVerticalTextPosition(AbstractButton.BOTTOM);
                    groupPnl.add(btn);
                    groupPnl.repaint();
                    groupPnl.revalidate();

                }while (rs2.next());
            }

        } catch (SQLException e) {
            e.getStackTrace();
        }

    }

Next Button Event Code

 try {

            Connection con = connectionDB.conDb();
            String sql = "select COUNT(id) as ids from menu_cat";
            PreparedStatement pst = con.prepareStatement(sql);
            ResultSet rs = pst.executeQuery();

            while (rs.next()) {

                gid.setText(rs.getString("ids"));
            }
            double ab = Double.parseDouble(gid.getText());
            double sm = ab / pageSize1;
            int ch = (int) sm;

            if (ch >= pageIndex1) {
                loadgroup(pageIndex1++);
                System.out.println(pageIndex1++);

            } else {

                pageIndex1 = 0;
                loadgroup(pageIndex1++);
                //nxt.setEnabled(false);
                // pageIndex = 0;
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

Prev Button Event Required

pageIndex1 = 0;

loadPage(pageIndex1--);
nxt.setEnabled(true);

Menu Category Load

private void loadPage(int page) {

        this.pageIndex = page;
        try {
            // Customize page size here
            Connection con = (Connection) connectionDB.conDb();
            String sl = "SELECT id , name,translated_name ,btn_color,text_color FROM `menu_cat` LIMIT ? OFFSET ? ";
            PreparedStatement pst = (PreparedStatement) con.prepareStatement(sl);

            pst.setInt(1, pageSize);
            pst.setInt(2, pageSize * page);

            ResultSet rs = pst.executeQuery();

            cat_panel.removeAll();

            while (rs.next()) {
                totalRecord = rs.getInt(1);
                String a1 = rs.getString("name");
                int getid = rs.getInt(1);
                String a00 = rs.getString("translated_name");

                int a3 = rs.getInt("BTN_COLOR");
                int a4 = rs.getInt("text_COLOR");
                PosButton btn = new PosButton();

                btn.setText("<html><body><center><p>" + a1 + "</center><p>" + getid + "</p></body></html>");
                btn.setVerticalAlignment((int) CENTER_ALIGNMENT);
                btn.applyComponentOrientation(getComponentOrientation());
                btn.setFocusPainted(false);
                btn.setFocusable(false);
                btn.setToolTipText(a00);
                btn.setRequestFocusEnabled(false);
                btn.setMaximumSize(new Dimension(80, 80));
                btn.setPreferredSize(new Dimension(60, 60));
                btn.setMinimumSize(new Dimension(80, 80));
                btn.setBackground(new Color(a3, true));
                btn.setForeground(new Color(a4, true));

                btn.setFocusPainted(true);
                btn.setBorderPainted(true);
                btn.setSelected(true);
                //String a2 = rs.getString(2);
                btn.setHorizontalAlignment(SwingConstants.CENTER);
                btn.setHorizontalTextPosition(AbstractButton.CENTER);
                btn.setVerticalTextPosition(AbstractButton.BOTTOM);
                //  cat_panel.setLayout(new MigLayout("fill, wrap 1 0"));
                // cat_panel = new JPanel(new MigLayout("fill, wrap 1, insets 0"));

                btn.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        btn.setSelected(true);
                        btn.isSelected();
                        loadgroup(getid);
                        //loadgroupss(getid);

                        System.out.println(a1 + ":\t");

                    }
                });

                cat_panel.add(btn, "center");
                //add(cat_panel, "grow y");
                cat_panel.repaint();
                cat_panel.revalidate();
            }
        } catch (SQLException e) {
        }

    }
c0der
  • 18,467
  • 6
  • 33
  • 65
Mayo-Soft
  • 31
  • 5
  • 1
    You've posted a lot of code, but you forgot to add a question. – Stultuske Apr 16 '20 at 06:24
  • just i make **Next/Prev** buttons that forward or go back this is not work correctly my code like a make pagination buttons – Mayo-Soft Apr 16 '20 at 06:27
  • 1
    ok. and what is the problem you are having? – Stultuske Apr 16 '20 at 06:34
  • @Stultuske when i press **Next or Prev** Button values not change mean doesn't forward or goback! – Mayo-Soft Apr 16 '20 at 06:41
  • 1
    maybe there are no next or previous to show – Stultuske Apr 16 '20 at 06:46
  • 2
    1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Hard code data to replace the DB. 2) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) 3) Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Apr 16 '20 at 06:51
  • i have added an image Next/Prev Buttons – Mayo-Soft Apr 16 '20 at 06:56

1 Answers1

2

When you need to learn something new or fix something which is not working as expected it is often much easier to do it on a smaller scale, rather than on you application.
This goes in line with SO requirement of posting mre when asking or answering.
Your question is essentially how to change JPanels in their container, and the following code demonstrates just that, and nothing else:

import java.awt.*;
import javax.swing.*;

public class CardLayoutPrevNextDemo extends JPanel {

    private final CardLayout cLayout;
    private final JPanel cardsPane;
    private static int MAX_PAGES = 5;
    private int topPageIndex = 0;

    public CardLayoutPrevNextDemo(){

        cardsPane = new JPanel();
        cLayout = new CardLayout();
        cardsPane.setLayout(cLayout);
        addPages();
        changePane();

        JButton prevPage = new JButton("Prev");
        prevPage.addActionListener(e -> showPrevPane());
        JButton nextPage = new JButton("Next");
        nextPage.addActionListener(e -> showNextPane());

        JPanel buttonsPane = new JPanel(new BorderLayout());
        buttonsPane.add(prevPage,BorderLayout.WEST);
        buttonsPane.add(nextPage,BorderLayout.EAST);

        setLayout(new BorderLayout());
        add(cardsPane,BorderLayout.CENTER);
        add(buttonsPane,BorderLayout.NORTH);
    }

    private void addPages() {
        for(int i = 0; i < MAX_PAGES ; i++){
            JPanel pane = new JPanel();
            String pageName = String.valueOf(i);
            setPreferredSize(new Dimension(400,200));
            pane.add(new JLabel("Page # "+pageName));
            cardsPane.add(pageName, pane);
        }
    }

    void showNextPane() {
        topPageIndex = topPageIndex + 1 >= MAX_PAGES ?  0: topPageIndex + 1;
        changePane();
    }

    void showPrevPane() {
        topPageIndex = topPageIndex - 1 < 0  ?  MAX_PAGES -1 : topPageIndex - 1;
        changePane();
    }

    void changePane() {
        cLayout.show(cardsPane, String.valueOf(topPageIndex));
    }

    public static void main(String[] args) {

        JFrame frame =  new JFrame("Card Layout Prev  Next Demo");
        frame.setSize(400,250);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.add(new CardLayoutPrevNextDemo());
        frame.pack();
        frame.setVisible(true);
    }
}

enter image description here

c0der
  • 18,467
  • 6
  • 33
  • 65
  • thanks your cooperation can you give me more idea about ,how i can add database query or load value from database? – Mayo-Soft Apr 16 '20 at 09:22
  • @coder i have apply your code own my example and i have 48 records in my database 48 / 5 =9 how i can apply theme? your code run correctly but haff values show 0-5 pages show on per page 5 values show. – Mayo-Soft Apr 16 '20 at 12:15
  • 2
    The way to cope with a big problem is to break it ti smaller ones. Solve one issue at a time. Get you page swapping correct. Get you database data retrieval correct desperately then try to do both, This also applies to SO post: it should be around one focused question. For other questions post new threads. – c0der Apr 16 '20 at 13:31