#Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container

1 messages · Page 1 of 1 (latest)

inland capeBOT
#

This post has been reserved for your question.

Hey @long dragon! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

scarlet finch
#

Why not use JFX?

long dragon
#

okay well, here's one problem

#

why are you directly trying to add components to a JFrame

#

make a JPanel

#

add compnents to the JPanel, then add the JPanel to the JFrame by add(panel)

scarlet finch
#

I see. Could you format your code properly?

long dragon
#

it would be easier to format the code using this:
``

pasteCodeHere

``

long dragon
#

why did i write that in camelCase

scarlet finch
#

It’s slowly craving towards your dna

#

Coding is hard

long dragon
#

i'm terrible at it

scarlet finch
#

True

#

Happens

#

I can teach you

long dragon
#

always have been

long dragon
#

i'm broke

scarlet finch
#

Voluntarily

long dragon
#

that's a bit unfair though

scarlet finch
#

Why’s that?

long dragon
#

free

#

that's why

scarlet finch
#

Unfair for me?

long dragon
#

teaching costs money, sanity, time™️ and stuff

#

unfair for me

scarlet finch
#

Why would it be unfair for you, when I’m the one teaching for free

#

Don’t get it

long dragon
#

because it's free

#

you wouldn't exactly want to waste time without getting something in return, right?

#

if you're a void method, what's there to gain?

#

there has to be a return

scarlet finch
#

That’s a bad example. You don’t really care about void methods in your code, do you?

long dragon
#

depends

scarlet finch
#

You could also write every void as int and return 0

long dragon
#

i can't exactly write JSONObjects as an int

#

well, yeah

scarlet finch
#

Is it just me or does your code not have highlighting?

long dragon
#

that's how it would be done at the very basic level

#

nope

#

here too

#

they forgot to append

#

here, let me reformat it for you

#
public class Test{

Test2 test = new Test2();
JFrame frame = new JFrame();

Test(){
...
frame.setLayout(new BorderLayout());
frame.add(test, BorderLayout.CENTER);
...
}

//main
...
}

//public class Test2{
public class Test2 extends JPanel {

//JPanel test2 = new JPanel();

Test2(){
...
}
scarlet finch
#

Did you add the 'java' in the first line after your three backticks?

long dragon
#

odd

#

i see colour

scarlet finch
#

Must be a mobile problem

long dragon
scarlet finch
#

Thanks, now I’m blind

long dragon
#

oh yeah

#

mobile is shit

#

there's no colour on mobile

#

no Color class 💀

#

yeah?

#

don't be a dark dickrider

#

light is unironically good

#

you need light to see with your eyes

#

you can't escape it

scarlet finch
#

Back to your problem

#

Did it work?

#

Okay, let’s do it in an abstract way

#

You need a panel, that has your content

long dragon
#

well they first need a frame

#
public class LauncherFrame extends JFrame {

  private static final long serialVersionUID = 1L;
  public static LauncherFrame instance;
  private final Dimension minimumDim;

  {
    this.minimumDim = new Dimension(640, 480);
  }

  public LauncherFrame() {
    super("TwentyTen Launcher");

    this.setIconImage(ResourceUtils.getIconImage("icon/favicon.png",
        LauncherFrame.class));

    LauncherFrame.instance = this;
    this.setMinimumSize(this.minimumDim);

    this.getContentPane().setBackground(Color.BLACK);

    this.add(new LauncherPanel());
    this.pack();

    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    this.setResizable(true);
    this.setVisible(true);
  }

like this

#

no

#

why?

#

let me show you why

#
JFrame frame = new JFrame();```
#

this is why

#

it needs to be declared like so

#

new calls are constructors

#

forgot the semicolon

#

<class> <variable> = new <constructor>

scarlet finch
#

Not exactly

scarlet finch
long dragon
#

ah

#

OOOOH YEAH, variable types

#

int and such

scarlet finch
#

Primitives

#

Right

long dragon
#

but in their context

#

it's class

#

no

scarlet finch
#

Yes

#

The declaration part is

#

JFrame frame

long dragon
#

what you have is JFrame frame = new frame JFrame()

#

what you need is JFrame frame = new JFrame()

scarlet finch
#

Yup

long dragon
#

same goes for JPanel

#

it goes for everything you create an instance of

scarlet finch
#

Declaration was correct, initializing was fcked up

long dragon
#

first half was correct

#

init was off, yeah

#

depends

#

what's the rest of your code

scarlet finch
#

Well it fixes the compilation problem of those two lines

#

Let me switch to my computer

#

We got this pika_cool

#

Looks pretty nice

#

Nope

#

Also afaik you shouldn't extending the JFrame class

long dragon
#

it would be easier to do it like this:

public class FrameTest extends JFrame {

  public FrameTest() {
    super("FrameTitle");

    this.createPanel();
  }

  private void createPanel() {
    JPanel panel = new JPanel();
    this.add(panel);

    // your content here
  }
}
long dragon
#

splitting into classes is a good habit as long as it's managed right

#

i usually split my panels and sub-panels

scarlet finch
#

I split my money

#

Anyways

long dragon
scarlet finch
#

Alright, let's do this

#

You need a window with a button

long dragon
#

first make the window

scarlet finch
#

Right

long dragon
#

a new...window...like a JFrame or a JDialog?

#

or a file explorer

#

yeah, don't do that because for one, i made my own slightly edited JPanel class that you don't have

scarlet finch
#

connect 4

long dragon
#

and two, it also contains some custom extended classes for two components you also don't have

scarlet finch
#

I already have one written in JavaFX, but it has some bugs

long dragon
#

god I hate JFX

#

why do you want to open a completely new JFrame? there's no reason for multiple JFrames in your instance

#

a single one with a JDialog as an additional will do the trick

#

i wouldn't be so sure of that, but JDialog is way better as a 'secondary window' than a completely new JFrame

scarlet finch
#

Yes

long dragon
#

here's how you could structure your swing app:

JFrame // main window
  JPanel // main panel for all the sub-panels
    JPanel // subpanel 1
    JPanel // subpanel 2
    JPanel // subpanel 3
JDialog // secondary window that one could call anywhere
#

remind you that it does not include any local JPanels created within methods

#

these are simply class files

scarlet finch
#

School man

#

My teacher forced me

long dragon
#

oh hey!

#

Look at that

scarlet finch
#

Trust me, it is way easier than Connect 4

long dragon
#

maybe you want me to create you a super simple example

#

OH WAIT

scarlet finch
#

Wanna see the win conditions?

long dragon
#

I GOT AN OLD ONE

scarlet finch
#

Let me pull it up

long dragon
#

also, here's a little structure to understand as well

#

two JPanels, one JFrame

#

and the sub-panel has 4 local JPanels

scarlet finch
#

Don't.

#

Ein Deutscher, aha.

#

Let's continue in English tho

long dragon
#

it's a bit messy, but here's how the locals are

#

all that within a subpanel

scarlet finch
#
private boolean hasWon(int pRow, int pCol) {
        // verticalCheck
        if (pRow <= 2)
            for (var i = 0; i < pRow; i++)
                if (gameField[pRow][pCol] == activePlayer && gameField[pRow + 1][pCol] == activePlayer &&
                        gameField[pRow + 2][pCol] == activePlayer && gameField[pRow + 3][pCol] == activePlayer)
                    return true;

        // horizontalCheck
        var inRowHor = 0;
        for (var i = 0; i < 7; i++)
            if (gameField[pRow][i] == activePlayer) {
                inRowHor++;
                if (inRowHor == 4) {
                    return true;
                }
            } else
                inRowHor = 0;

        [...]

        return false;
    }```
long dragon
#

yeah

scarlet finch
#

The other 2 checks are bugged

#

But yeah

long dragon
#

booleans used for questioning things

#

"is this, has that, are those" ?

scarlet finch
#

It's a private repo tho

#

For a good reason

#

It reminds me of the old days...

#

When we only had JDK 1.8

long dragon
#

JDK 1.8...? that's still recent

#

2015 i think

scarlet finch
#

It is still used

#

BUT

#

I'm using JDK 17 LTS

long dragon
#

🤮

scarlet finch
#

ur face is 🤮

#

I need to use it

#

Spring requires it, if you want to use the latest version

long dragon
#

i don't know what spring boot is or why i ever need it

scarlet finch
#

I never said Spring Boot

long dragon
#

then what else Spring stands for

scarlet finch
#

Spring is the framework, Spring Boot a part of it

long dragon
#

oh

scarlet finch
long dragon
#

"level up your java code"

scarlet finch
long dragon
#

more like "perhaps this is why c#"

scarlet finch
#

Don't

long dragon
scarlet finch
#

Good

#

Neither do I

#

That's why I'm here lol

long dragon
#

i love Java

#

it makes me feel at home

#

it's like a milc

#

"mother i'd like to compile"

#

why are you creating a new frame within an extended jframe class?

#

don't copy code you don't understand

#

well, then one thing to actually eliminate is the dual jframe

#

man should be adding components to the jpanel, and the jpanel to the jframe

#

here, i'll take the same code

#

and make it easier

scarlet finch
#

Error

#

Classic

long dragon
#
public class BeispielListener extends JFrame implements ActionListener {

  private static final long serialVersionUID = 1L;
  JButton button1;
  JButton button2;
  JButton button3;
  JLabel label;

  {
    this.label = new JLabel("\u00A0");
    this.button1 = new JButton("Rules");
    this.button2 = new JButton("Start game");
    this.button3 = new JButton("Leave game");
  }

  public BeispielListener() {
    super("4 Gewinnt");

    this.setSize(700, 500);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    this.createPanel();

    this.button1.setFocusable(false);
    this.button2.setFocusable(false);
    this.button3.setFocusable(false);
    this.button1.addActionListener(this);
    this.button2.addActionListener(this);
    this.button3.addActionListener(this);
  }

  public static void main(String[] args) {
    BeispielListener bl = new BeispielListener();
    bl.setVisible(true);
  }

  private void createPanel() {
    JPanel panel = new JPanel();
    panel.add(this.label);
    panel.add(this.button1);
    panel.add(this.button2);
    panel.add(this.button3);
    this.add(panel);
  }

  @Override
  public void actionPerformed(ActionEvent ae) {
    // Die Quelle wird mit getSource() abgefragt und mit den
    // Buttons abgeglichen. Wenn die Quelle des ActionEvents einer
    // der Buttons ist, wird der Text des JLabels entsprechend geändert
    Object source = ae.getSource();
    if (source == this.button1) {
      this.label.setText("Button 1 was clicked");
    }
    if (source == this.button2) {
      this.label.setText("Button 2 was clicked");
    }
    if (source == this.button3) {
      this.label.setText("Button 3 was clicked");
    }
  }
}
#

there we go

scarlet finch
#
{
    this.setSize(700, 500);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    this.label = new JLabel("\u00A0");
    this.button1 = new JButton("Rules");
    this.button2 = new JButton("Start game");
    this.button3 = new JButton("Leave game");
  }```
#

How is this part supposed to work

long dragon
#

wdym

scarlet finch
long dragon
#

oh wait

#

why do i have a duplicate

#

there we go

#

fixed it

#

i called setSize and setDefaultCloseOperation twice

#

when i already had them called in the constructor

#

did you import the interface?

#

i did tell me that until i imported the interface

#

judging why IDEA decides to not quick-import my stuff

#

i had these toggled off

#

that's probably why

#

I like this guy

#

simple Java learner

#

uhhh

#

what are you doing?

#

why are you trying to create a setSize method

#

why is this here?

#

yeah, no, delete that.

#

eclipse dark mode hurts my eyes

#

yaaaaaaaaaay

#

eclipse was never known for good dark mode

#

yeah. unless you want to manually override things like I did with say.. a custom JPanel... don't do those "setters" when they are already assigned by default

#
public class CustomJPanel extends JPanel {

  private static final long serialVersionUID = 1L;

  public CustomJPanel(LayoutManager layout, boolean isDoubleBuffered) {
    super(layout, isDoubleBuffered);
  }

  @Override
  public Insets getInsets() {
    return new Insets(15, 30, 15, 30);
  }

  @Override
  public Color getBackground() {
    return Color.GRAY;
  }

  @Override
  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.BLACK);
    g2d.setStroke(new BasicStroke(2));
    g2d.drawRect(1, 1, this.getWidth() - 2, this.getHeight() - 2);

    g2d.setColor(Color.WHITE);
    g2d.setStroke(new BasicStroke(1));
    g2d.drawRect(2, 2, this.getWidth() - 5, this.getHeight() - 5);
  }
}

If you want to get that grey panel I got, this is how

#

and you just extend or create a panel with this class instead of regular "JPanel"

#

that might teach you a bit on @Override property

scarlet finch
#

This is my approach

#
package de.dummyapt.sandbox;

import javax.swing.*;

public class Sandbox {
    private final JButton button1;
    private final JButton button2;
    private final JButton button3;
    private final JLabel label;

    public Sandbox() {
        label = new JLabel("\u00A0");
        button1 = new JButton("Rules");
        button2 = new JButton("Start game");
        button3 = new JButton("Leave game");
    }

    public static void main(String[] args) {
        var sandbox = new Sandbox();
        sandbox.createPanel();
    }

    private void createPanel() {
        button1.setFocusable(false);
        button1.addActionListener(e -> label.setText("Button 1 clicked"));

        button2.setFocusable(false);
        button2.addActionListener(e -> label.setText("Button 2 clicked"));

        button3.setFocusable(false);
        button3.addActionListener(e -> label.setText("Button 3 clicked"));

        var panel = new JPanel();
        panel.add(label);
        panel.add(button1);
        panel.add(button2);
        panel.add(button3);

        var frame = new JFrame();
        frame.setVisible(true);
        frame.setTitle("Sandbox");
        frame.setSize(700, 500);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(panel);
    }
}```
#

Yup

long dragon
#

that looks like Java 8 code

scarlet finch
#

?

long dragon
#

it's got lambdas

scarlet finch
#

Yeah

long dragon
#

i use JDK 7

#

zulu LTS

scarlet finch
#

lambda -> ActionListener interface

long dragon
#

also...var?

#

isn't that Lombok's thing

scarlet finch
#

Nope

long dragon
#

it is for JDK 7

scarlet finch
long dragon
#

i also wonder what the use case for var is in Java

scarlet finch
#
ActionListenerException actionListenerException = new ActionListenerException("");
var actionListenerException = new ActionListenerException("");
#

You can choose

long dragon
#

i choose

ActionListenerException ale = new ActionListenerException("");```
#

there

#

same length as your var

scarlet finch
#

Ew

long dragon
#

it's much clearer to me this way

#

than by using var

#

otherwise i just need to look at constructor calls

scarlet finch
#

You are just afraid to learn something now

#

var came with JDK 10 and got an upgrade in JDK 11 LTS

long dragon
#

i have lombok and can use var if i want to

scarlet finch
#

You have to import it first

long dragon
#

did you....do it to the main panel?

scarlet finch
#

And maybe also annotate

long dragon
#

like that?

long dragon
#

and people crying about imports need a better hobby

scarlet finch
#

You said it's the same length

long dragon
#

i said the same length in line

#

simply because i made it an acronym

#

uh no

#

like this

#

the public class is your class

#

you extend it with CustomJPanel instead of JPanel

scarlet finch
#

Don't implement the ActionListener interface

#

You can use lambda

long dragon
#

not the same

scarlet finch
#

my bad

long dragon
#

extension can only be done once

#

implementation is limitless

#

and interfaces cannot be extended

#

only other classes or abstract classes can be

scarlet finch
long dragon
#

it would be correct if you either did

CustomJPanel panelName = new CustomJPanel(new GridLayout(), true or if you extended an existing panel class with it

#

like I showed you

#

in the code I provided

#

you navigate to the createPanel method

#

and replace the JPanel call with CustomJPanel

#
  private void createPanel() {
    CustomJPanel panel = new CustomJPanel();
    panel.add(this.label);
    panel.add(this.button1);
    panel.add(this.button2);
    panel.add(this.button3);
    this.add(panel);
  }
#

add this in CustomJPanel class if you want to use empty constructor:

  public CustomJPanel() {
    super();
  }
#

and for class definitions, you simply use this format:
<modifier> <type> class <ClassName> extends <otherClassName> implements <InterfaceName>, <InterfaceName>

#

<type>is optional, and would be abstract in some cases when needed.

#

so you could do a class like class MyClass extends MyAbstractClass implements MyInterface

#

idk how to explain why it works

scarlet finch
#

Don't question it

long dragon
#

if something works on the first try, get suspicious lol

scarlet finch
#

Oof

#

Anyways, I would recommend using the latest LTS version of JDK

long dragon
#

man uses jdk 17

#

JDK 20 is latest iirc

#

god i hate python

scarlet finch
#

I love Python

long dragon
#

i had to cope with python for a mere 4 months 5 years ago

#

it was painful

#

i thought they said it's a simple language...

#

OOP in python is a complete joke and it has weird flaws

scarlet finch
#

You could write a Python script that codes everything in Java

#

But then again you need to know the Java syntax

long dragon
#

also the fact that i don't have curly braces or access modifiers in python beats it

scarlet finch
#

Lol

#

I don't think they use high level languages like Python for rocket science

long dragon
#

they use C

scarlet finch
#

Something proprietary

long dragon
#

think NASA uses C for some of their space stuff

#

anything else might be custom assembly or something

scarlet finch
#

Wait a goddamn minute

long dragon
#

C++ isn't any newer

scarlet finch
long dragon
#

that's actually an article?

scarlet finch
long dragon
#

not quite any faster

long dragon
#

because it's the only extension to C that uses OOP principles

#

C itself doesn't

scarlet finch
#

Why do bikes exist if cars are faster, easier to use and live longer?

long dragon
#

Fortran...?

#

really?

#

what use case does that have

scarlet finch
#

Weather prediction

long dragon
#

what about Perl?

#

why is Perl here?

scarlet finch
long dragon
#

VHDL i know why

#

Node.js i have no clue

#

and MATLAB i assume is for simulations

scarlet finch
#

I would unfollow this thread, if the problem is solved now