Error: Java. Lang. Illegalargumentexception

Error in following code is: java.lang.IllegalArgumentException: adding container's parent to itself.

This is the code:

public class humev extends JFrame implements ActionListener{

    //Dichiarazione variabili e costanti

    private static final int larghezza = 1300;
    private static final int altezza = 1000;
    private static final String nome = "Human Evolution";

    private JLabel lab;
    private JButton gioca;
    private JPanel pang;

        public humev(){

            try{
                pang = new JPanel();
                gioca = new JButton("Gioca!");
                gioca.addActionListener(this);
                lab = new JLabel();

                gioca.add(gioca);
                lab.add(lab);
                pang.setLayout(null);
                }
            catch(Exception e1){
                System.err.println(e1);
                System.err.println("Impossibile caricare il frame di gioco!");
                }
        }

    public static void main(String[] args) {

        //Finestra
        try{
            humev h = new humev();

            JFrame finestra = new JFrame(nome);

            Dimension dim_finestra = new Dimension(larghezza, altezza);

            finestra.setPreferredSize(dim_finestra);
            finestra.setMaximumSize(dim_finestra);
            finestra.setResizable(false);
            finestra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            finestra.pack();
            finestra.setVisible(true);

        }
        catch(Exception e2){
            System.err.println(e2);
            System.err.println("Impossibile caricare la finestra. Frame non caricato");
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == gioca){
            lab.setText("Gioco avviato con successo!");
        }
    }
}
5

3 Answers

You cannot add a labelinto a label :-

lab.add(lab);

Aldo you cannot add a button on a button :-

gioca.add(gioca);

Try adding them to the JPanel or ContentPane instead like :-

pang.add(gioca);
pang.add(lab);
getContentPane().add(pang);

EDIT:-

For Showing the JFrame you need to do first add your JPanel to JFrame then set the visibility of JFrame to true something like :-

finestra.add(pang); // add panel to frame
finestra.setVisible(true); // show frame visibility to true

Also don't set the layout to null :-

pang.setLayout(null); 

Else you will need to set the bounds yourself. So just comment this line.

7

try to run this example.there is bunch of problems in your code.

you are adding component to itself

gioca.add(gioca); // don't do this

use layouts .don't use null

pang.setLayout(null); // don't do this .use layouts .and even if you use null then
//use bounds to absolutely position .if you use null layout and if you add using `.add()` 
//then you will not see those components .

complete code

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class humev extends JFrame implements ActionListener {

    private static final int larghezza = 1300;
    private static final int altezza = 1000;
    private static final String nome = "Human Evolution";

    private final JLabel lab;
    private final JButton gioca;
    private final JPanel pang;

    public humev() {
        super(nome);

        pang = new JPanel();
        //pang.setLayout(new FlowLayout()); // use appropriate layout .for example flowlayout.since flowlayout is default layout for jpanel you can avoid it.but don't use null
        gioca = new JButton("Gioca!");
        gioca.addActionListener(this);
        lab = new JLabel("lable");

        pang.add(gioca);
        pang.add(lab);
        add(pang); // add pang panel to frame
        Dimension dim_finestra = new Dimension(larghezza, altezza);

        setPreferredSize(dim_finestra);
        setMaximumSize(dim_finestra);
        //setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true); 
        pack();

    }

    public static void main(String[] args) {
        humev humev = new humev();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == gioca) {
            lab.setText("Gioco avviato con successo!");
        }
    }
}
0

IllegalArgumentException is an Unchecked Exception.

It rises explicitly by API Developer or Programmer to indicate that a method has invoked with illegal argument.

example:

Thread t = new Thread();
t.setPriority(15);

output:

RuntimeException: IllegalArgumentExcepion

The valid range of Thread priority is 1 to 10, if we are trying to set the priority with any other value, then we will get IllegalArgumentException.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest