Home > Net >  Java IllegalArgumentException when assigning array of buttons to GridLayout
Java IllegalArgumentException when assigning array of buttons to GridLayout

Time:06-05

So I have started a calculator from scratch in order to practice Java knowledge. I have a slight problem, when I'm assigning buttons to a JLabel using GridLayout the console returns an exception java.lang.IllegalArgumentException. I know I am doing something foolish and I better reorganize the array and do a for loop to assign buttons, but I am too lazy to do it and want to understand what I did wrong.

Here is the code:

buttonDisplay.setBounds(30, 30, 400, 320); //buttonDisplay is a JLabel
buttonDisplay.setLayout(new GridLayout(5,4,5,5));
        
operator = new JButton[20];
operator[0] = new JButton("0");
operator[1] = new JButton("1");
operator[2] = new JButton("2");
operator[3] = new JButton("3");
operator[4] = new JButton("4");
operator[5] = new JButton("5");
operator[6] = new JButton("6");
operator[7] = new JButton("7");
operator[8] = new JButton("8");
operator[9] = new JButton("9");
operator[10] = new JButton(" ");
operator[11] = new JButton("-");
operator[12] = new JButton("*");
operator[13] = new JButton("/");
operator[14] = new JButton(".");
operator[15] = new JButton("=");
operator[16] = new JButton("AC");
operator[17] = new JButton("C");
operator[18] = new JButton("TAX ");
operator[19] = new JButton("TAX-");

buttonDisplay.add(operator[0],4,2); //exception starts on this line
buttonDisplay.add(operator[1],3,3);
buttonDisplay.add(operator[2],3,2);
buttonDisplay.add(operator[3],3,1);
buttonDisplay.add(operator[4],2,3);
buttonDisplay.add(operator[5],2,2);
buttonDisplay.add(operator[6],2,1);
buttonDisplay.add(operator[7],1,3);
buttonDisplay.add(operator[8],1,2);
buttonDisplay.add(operator[9],1,1);
buttonDisplay.add(operator[10],1,4);
buttonDisplay.add(operator[11],2,4);
buttonDisplay.add(operator[12],3,4);
buttonDisplay.add(operator[13],4,4);
buttonDisplay.add(operator[14],4,1);
buttonDisplay.add(operator[15],4,3);
buttonDisplay.add(operator[16],5,1);
buttonDisplay.add(operator[17],5,2);
buttonDisplay.add(operator[18],5,3);
buttonDisplay.add(operator[19],5,4);

CodePudding user response:

When I ran [part of] your code, the following line threw an exception:

buttonDisplay.add(operator[0],4,2);

Here is [part of] the stack trace I got:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: illegal component position
    at java.desktop/java.awt.Container.addImpl(Container.java:1115)
    at java.desktop/java.awt.Container.add(Container.java:1033)

The method you are calling is this one:
screen capture of GUI

CodePudding user response:

JLabel is not a container it is used to place a plain text or image. but i understand that you need something a container that can hold all the buttons. So, you should use JPanel.
here is the code that helps you a lot.

import java.awt.Color;
import javax.swing.JPanel;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;

public class Calculator extends javax.swing.JFrame {

    JFrame frame = new JFrame();
    JPanel buttonContainer = new JPanel();
    JButton operator[] = new JButton[20];
    public static void main(String args[]) {
        new Calculator();
    }

    public Calculator() {

        operator[0] = new JButton("0");
        operator[1] = new JButton("1");
        operator[2] = new JButton("2");
        operator[3] = new JButton("3");
        operator[4] = new JButton("4");
        operator[5] = new JButton("5");
        operator[6] = new JButton("6");
        operator[7] = new JButton("7");
        operator[8] = new JButton("8");
        operator[9] = new JButton("9");
        operator[10] = new JButton(" ");
        operator[11] = new JButton("-");
        operator[12] = new JButton("*");
        operator[13] = new JButton("/");
        operator[14] = new JButton(".");
        operator[15] = new JButton("=");
        operator[16] = new JButton("AC");
        operator[17] = new JButton("C");
        operator[18] = new JButton("TAX ");
        operator[19] = new JButton("TAX-");

        setSize(500, 500);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        buttonContainer.setLayout(new GridLayout(5, 4,5,5));
        buttonContainer.setBackground(Color.red);
        for (int i = 0; i < 20; i  ) {
            buttonContainer.add(operator[i]);
        }
        add(buttonContainer);
        setVisible(true);
    }
}
  • Related