Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

Display problems with custom JComponent

Thread view: 
Chris - 16 May 2007 13:38 GMT
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.
Andrew Thompson - 16 May 2007 13:58 GMT
>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>

Signature

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

Chris - 16 May 2007 14:37 GMT
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.

>> Can anybody shed any light on what's happening.
> 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.
Lew - 16 May 2007 15:23 GMT
> Sorry, here's a better 'SSCCE' as you put it:

Did you /read/
<http://www.physci.org/codes/sscce.html>
?

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

With respect to your "SSCnon-CE":

> ---------
>
[quoted text clipped - 26 lines]
>
> --------

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?

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.