Search code examples
javaswingjbuttonlayout-managergrid-layout

Java JFrame button organization


How do i create a jframe that organizes the text box and jbuttons, with the textbox on top and a 3 by 4 grid layout of buttons 1-10?

"TEXT BOX HERE"
[1][2][3]
[4][5][6]
[7][8][9]
[(][0][)]

This is what I have so far:

setTitle("Test");
    setSize(400, 400);

    // Create JButton and JPanel
    JButton[] buttons = new JButton[12];
    JButton buttonLpar = new JButton("(");
    JButton buttonRpar = new JButton(")");
    JPanel panel = new JPanel();
    JPanel panel2 = new JPanel(new GridLayout(3, 4));

    // adding 10 buttons
    for(int i = 0; i < 10; i++) {
        buttons[i] = new JButton(String.valueOf(i));
    }
    buttons[11].add(buttonLpar);
    buttons[12].add(buttonRpar);

     JTextField text = new JTextField("",10);
     text.setFont(new Font("Helvetica Neue", Font.BOLD, 12));

     panel.add(text, BorderLayout.NORTH);
     panel.add(panel2, BorderLayout.CENTER);
     this.getContentPane().add(panel);

     setVisible(true);
     setDefaultCloseOperation(EXIT_ON_CLOSE);

But that's where I'm stuck.

Okay I should note that I need a for loop to populate 3 by 4 gridlayout. But I don't know what I need in the loop.


Solution

  • For this kind of grid, assuming you create it on the go, you just need to have an if-else to see if you're on the last row of data.

    For example:

    import java.awt.GridLayout;
    
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class GridLayoutWithEmptyComponents {
        private JFrame frame;
        private JPanel pane;
        private JPanel buttonsPane;
        private JButton[] buttons;
    
        private void createAndShowGUI() {
            frame = new JFrame(getClass().getSimpleName());
            pane = new JPanel();
            pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
    
            buttonsPane = new JPanel();
            buttonsPane.setLayout(new GridLayout(4, 3));
    
            buttons = new JButton[10];
            for (int i = 0; i < buttons.length; i++) {
                buttons[i] = new JButton("" + (i + 1));
                if (i == buttons.length - 1) {
                    buttonsPane.add(new JButton("("));
                    buttonsPane.add(buttons[i]);
                    buttonsPane.add(new JButton(")"));
                } else {
                    buttonsPane.add(buttons[i]);
                }
            }
    
            pane.add(new JLabel("Text box"));
            pane.add(buttonsPane);
    
            frame.add(pane);
            frame.pack();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new GridLayoutWithEmptyComponents()::createAndShowGUI);
        }
    }
    

    Or you could have all the data in a String[] like:

    String[] buttonValues = {"1", "2", "3", ..., "8", "9", "(", "0", ")"};
    

    And then use those values as the JButtons values without the need for an if-else inside the loop.

    enter image description here