#Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
1 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @long dragon! Please use
/closeor theClose Postbutton 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.
Why not use JFX?
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)
I see. Could you format your code properly?
it would be easier to format the code using this:
``
pasteCodeHere
``
Exactly
why did i write that in camelCase
always have been
Voluntarily
that's a bit unfair though
Why’s that?
Unfair for me?
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
That’s a bad example. You don’t really care about void methods in your code, do you?
depends
You could also write every void as int and return 0
Is it just me or does your code not have highlighting?
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(){
...
}
Did you add the 'java' in the first line after your three backticks?
Still no color
Must be a mobile problem
Thanks, now I’m blind
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
Touché
Back to your problem
Did it work?
Okay, let’s do it in an abstract way
You need a panel, that has your content
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>
Not exactly
class/type
what you have is JFrame frame = new frame JFrame()
what you need is JFrame frame = new JFrame()
Yup
Declaration was correct, initializing was fcked up
Well it fixes the compilation problem of those two lines
Let me switch to my computer
We got this 
Damn
Looks pretty nice
Nope
Also afaik you shouldn't extending the JFrame class
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
}
}
it's not a problem if you want a specific window class
splitting into classes is a good habit as long as it's managed right
i usually split my panels and sub-panels
You could do an approach similarly to this when also adding components in a specific style:
https://paste.ofcode.org/mKAYqwSmYcq5iUaMahzstB
first make the window
Right
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
connect 4
and two, it also contains some custom extended classes for two components you also don't have
I already have one written in JavaFX, but it has some bugs
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
Yes
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
Trust me, it is way easier than Connect 4
Wanna see the win conditions?
I GOT AN OLD ONE
Let me pull it up
also, here's a little structure to understand as well
two JPanels, one JFrame
and the sub-panel has 4 local JPanels
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;
}```
yeah
It's a private repo tho
For a good reason
It reminds me of the old days...
When we only had JDK 1.8
🤮
ur face is 🤮
I need to use it
Spring requires it, if you want to use the latest version
i don't know what spring boot is or why i ever need it
I never said Spring Boot
then what else Spring stands for
Spring is the framework, Spring Boot a part of it
oh
"level up your java code"
more like "perhaps this is why c#"
Don't
i don't even like c#
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
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
{
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
wdym
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
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
that looks like Java 8 code
it's got lambdas
lambda -> ActionListener interface
Nope
it is for JDK 7
i also wonder what the use case for var is in Java
ActionListenerException actionListenerException = new ActionListenerException("");
var actionListenerException = new ActionListenerException("");
You can choose
i choose
ActionListenerException ale = new ActionListenerException("");```
there
same length as your var
Ew
it's much clearer to me this way
than by using var
otherwise i just need to look at constructor calls
You are just afraid to learn something now
var came with JDK 10 and got an upgrade in JDK 11 LTS
i have lombok and can use var if i want to
But it would be more code then
You have to import it first
did you....do it to the main panel?
And maybe also annotate
like that?
var doesn't need annotation
and people crying about imports need a better hobby
You said it's the same length
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
that's call implements
not the same
my bad
extension can only be done once
implementation is limitless
and interfaces cannot be extended
only other classes or abstract classes can be

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
Don't question it
if something works on the first try, get suspicious lol
I love Python
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
You could write a Python script that codes everything in Java
But then again you need to know the Java syntax
also the fact that i don't have curly braces or access modifiers in python beats it
they use C
think NASA uses C for some of their space stuff
anything else might be custom assembly or something
Wait a goddamn minute
C++ isn't any newer
that's actually an article?
not quite any faster
Why do bikes exist if cars are faster, easier to use and live longer?
i shouldn't be surprised they use VHDL
Fortran...?
really?
what use case does that have

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