Display problems with custom JComponent

C

Chris

I am having issues displaying two custom JComponents at the same time.
I have created a class which extends JWindow, and runs the following
code:

getContentPane().add(new CustomComponent(20,20));
getContentPane().add(new CustomComponent(120,20));
setSize(640,480);
setVisible(true);

The class CustomComponent extends JComponent and has the following
method:

protected void paintComponent(Graphics g)
{
g.fillOval(x,y,50,50);
}

The problem is that only the second CustomComponent added is
displayed. When the following code is run, there is no problem:

getContentPane().add(new CustomComponent(20,20));
setSize(640,480);
setVisible(true);
getContentPane().add(new CustomComponent(120,20));
setSize(640,480);
setVisible(true);

Note that BOTH setSize and setVisible must be run inbetween each
CustomComponent addition for them all to be displayed.

Can anybody shed any light on what's happening. I don't get this
problem when adding JLabel, so I guess it must be a problem with my
CustomComponent.

Thanks for your help.
 
A

Andrew Thompson

Chris said:
I am having issues displaying two custom JComponents at the same time.

What layout are you using?
I have created a class which extends JWindow, and runs the following
code: ...
Can anybody shed any light on what's happening.

Is that a question?

Perhaps it is the preferred size, or maybe the
layout, or maybe.. It could be one of any number
of things, and it is easier to determine which, if
you post an SSCCE.
<http://www.physci.org/codes/sscce.html>

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200705/1
 
C

Chris

Sorry, here's a better 'SSCCE' as you put it:

---------

public class CustomComponent extends JComponent
{
protected int x, y;

CustomComponent(int x_in, int y_in)
{
x = x_in;
y = y_in;
}

protected void paintComponent(Graphics g)
{
g.fillOval(x,y,50,50);
}
}

public class Test extends JWindow
{
public static void main(String[] args)
{
getContentPane().add(new CustomComponent(20,20));
getContentPane().add(new CustomComponent(120,20));
setSize(640,480);
setVisible(true);
}
}

--------

I'm not using a layout, is one required?
I don't know what you mean by the preferred size. I have not come
across this.
Is that a question?

Yes, if you add JLabels to the content pane instead of the
CustomComponent class, the above code works fine. I am confused about
why it does not like more than one of my classes.
 
L

Lew

Chris said:
Sorry, here's a better 'SSCCE' as you put it:

Did you /read/
# Self Contained - Ensure everything is included, ready to go.
# Correct - Copy, paste, (compile,) see is the aim.

With respect to your "SSCnon-CE":
---------

public class CustomComponent extends JComponent
{
protected int x, y;

CustomComponent(int x_in, int y_in)
{
x = x_in;
y = y_in;
}

protected void paintComponent(Graphics g)
{
g.fillOval(x,y,50,50);
}
}

public class Test extends JWindow
{
public static void main(String[] args)
{
getContentPane().add(new CustomComponent(20,20));
getContentPane().add(new CustomComponent(120,20));
setSize(640,480);
setVisible(true);
}
}

--------

Did you notice the compiler errors? (I called the test class
"CustomComponentTester" to avoid name conflicts.)

Actual compiler errors:

Compiling 1 source file to projects/testit/build/classes
projects/testit/src/testit/CustomComponentTester.java:9: non-static method
getContentPane() cannot be referenced from a static context
getContentPane().add(new CustomComponent(20,20));
projects/testit/src/testit/CustomComponentTester.java:10: non-static method
getContentPane() cannot be referenced from a static context
getContentPane().add(new CustomComponent(120,20));
projects/testit/src/testit/CustomComponentTester.java:11: non-static method
setSize(int,int) cannot be referenced from a static context
setSize(640,480);
projects/testit/src/testit/CustomComponentTester.java:12: non-static method
setVisible(boolean) cannot be referenced from a static context
setVisible(true);

You saw these same errors when preparing your "SSCCE", right?

Here's the clue to interpreting the "non-static method ... referenced from a
static context" message: From what object are you trying to getContentPane()
and the others?

Was this how your original code was? Did it, too, call non-static methods
from a static context?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top