javaarraysswingawt

Why are GridBagLayout.setConstraints and panel.add won't accept my indexed array of different components?


I'm creating a GUI which will have multiple different components (like JButtons, JTextFields, etc., although I'm testing it out with JButtons first) in a grid with different sizes, and I have created a method for organising it without manually setting the GridBagConstraints gridxs, gridys, gridwidhts and gridheights.

But, as the title says, the code complains about how I'm not allowed to use objects[i] as an argument in setConstraints and panel.add, because they only accept components, despite the fact that objects[i] is a component (JButton). I don't understand how this is wrong or what I'm supposed to do instead, as object arrays seems to be the only way to include both JButtons and other components like JTextFields, and contain what I'm supposed to put into the setConstraints and panel.add methods.

    // Defines some components for use in all classes and methods.
    private JFrame frame = new JFrame();
    private JPanel panel = new JPanel();
    private GridBagLayout gbl = new GridBagLayout();
    private GridBagConstraints gcon = new GridBagConstraints();

    // The GUI method, where all the work to create the GUI happens.
    public GUI() {

        // Defines 15 different buttons.
        JButton btn1 = new JButton("Btn 1");
        JButton btn2 = new JButton("Btn 2");
        JButton btn3 = new JButton("Btn 3");
        JButton btn4 = new JButton("Btn 4");
        JButton btn5 = new JButton("Btn 5");
        JButton btn6 = new JButton("Btn 6");
        JButton btn7 = new JButton("Btn 7");
        JButton btn8 = new JButton("Btn 8");
        JButton btn9 = new JButton("Btn 9");
        JButton btn10 = new JButton("Btn 10");
        JButton btn11 = new JButton("Btn 11");
        JButton btn12 = new JButton("Btn 12");
        JButton btn13 = new JButton("Btn 13");
        JButton btn14 = new JButton("Btn 14");
        JButton btn15 = new JButton("Btn 15");


        // // JTextField diceTypeField = new JTextField(5);

        // Sets the border and layout of the panel.
        panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        panel.setLayout(gbl);

        // Sets the gcon settings that apply to all components, like weight and how it'll fill the screen.
        gcon.weightx = 1;
        gcon.weighty = 1;
        gcon.fill = GridBagConstraints.BOTH;

        // Creates an array with lots of buttons and the spaces they occupy. if the same button occupies 2 indexes, it's because it's a larger button. In a later version of this program, the buttons will be switched with different components. It is then sent into a method which adds them to the panel in a formated way according to how they've been placed in the array.
        Object[] buttonArray = {btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn8, btn9, btn10, btn10, btn11, btn12, btn13, btn14, btn14, btn14, btn15, btn15, btn15, btn15, btn15, btn15};
        componentFormater(buttonArray, 3);

        // Adds the panel to the frame and changes some frame settings before setting it to be visible.
        frame.add(panel, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Dice Roller");
        // // frame.pack();
        frame.setExtendedState(Frame.MAXIMIZED_BOTH);
        frame.setVisible(true);
        
    }
    


    // A method for organising components in a grid when some components are wider and taller than others. Only works when components are wider before they are taller. It's very specific to the planned layout.
    private void componentFormater(Object[] objects, int gridW) {
        int objH;
        int objW;

        // Will go through every item in the array and add it to the panel with the gbl settings determined by how many times it occurs at once in the array.
        int occurances = 0;
        for (int i = 0; i<objects.length; i++) {
            // Only allows the process to happen when the numbers of occurances is 0, meaning when there's a new item in the array.
            if (occurances == 0) {
                // Counts the number of occurances of the object in the array..
                boolean run = true;
                int j = i;
                while (run && j<objects.length) {
                    if(objects[j] == objects[i]) {
                        occurances++;
                    }else{
                        run = false;
                    }
                    j++;
                }

                // Calculates the width and height of the component.
                if (occurances >= gridW) {
                    objW = gridW;
                }else{
                    objW = occurances;
                }
                objH = (int) Math.ceil((float) occurances/gridW);

                // Sets the gcon settings for the component.
                gcon.gridx = i%3;
                gcon.gridy = (int) Math.ceil((float) i/3)-1;
                gcon.gridwidth = objW;
                gcon.gridheight = objH;

                // Sets the constraints of the component, and adds it to the panel.
                gbl.setConstraints(objects[i], gcon);
                panel.add(objects[i]);
            }
            occurances--;
        }
    }```

Solution

  • I have to use the Components[] array or specify (component) in the methods.