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 / December 2005

Tip: Looking for answers? Try searching our database.

Using self made Component

Thread view: 
Neneko - 21 Dec 2005 19:43 GMT
Hello!

I got a bit of an problem using my self-made component. Which is that
it won't paint's itself..
I noticed the object is being made, but for some reason the components
paint methode isn't executed.
Here is the code of the component:

public class Peg extends Component
{
    int x,y;

    public Peg(int x, int y)
    {
        super();
        this.x = x;
        this.y = y;
    }
    public void paint(Graphics g)
    {
        g.setColor(Color.YELLOW);
        g.fillOval(x,y,100,100);
    }
    public Dimension getMinimumSize()
    {
        return new Dimension(100,100);
    }
    public Dimension getPreferredSize()
    {
        return getMinimumSize();
    }
    public Dimension getMaximumSize()
    {
        return new Dimension(500,500);
    }
}
Its for a school assingment and we need to use Awt and extending it as
a Component.
I really hope someone has an idear what im doing wrong here, been at
for some days now.
Thanks in advance!
Carl - 21 Dec 2005 20:13 GMT
> Hello!
>
[quoted text clipped - 37 lines]
> for some days now.
> Thanks in advance!

How are you attempting to use it?
This codes seems to work fine for me:

//Start code
import java.awt.BorderLayout;
import javax.swing.JFrame;

public class Test {
   
   
    public void testPegComponent(){
        JFrame f = new JFrame("TEST");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        Peg p = new Peg(4, 4);
        f.add(p, BorderLayout.CENTER);
        f.pack();
        f.setVisible(true);
    }
   
   
    public static void main(String[] args){
        Test t = new Test();
        t.testPegComponent();
    }

}
//End Code
Neneko - 21 Dec 2005 20:27 GMT
Thank you for your reaction!
Im using it this way:

//Code
Public class MyFrame extends Frame implements WindowListener
{
    public MyFrame()
    {
        super("MasterMind");
        setSize(400,600);
        setResizable(false);
        setLayout(new FlowLayout());
        addWindowListener(this);
        Peg test = new Peg(100,100);
        add(test);
               show();
       }
Code//
I tried your code aswell, but i got an error: "Exception in thread
"main" java.lang.Error: Do not use javax.swing.JFrame.add() use
javax.swing.JFrame.getContentPane().add() instead"

Could the problem be the version of the SDK that im using ?
I got 1.4.2.
Carl - 21 Dec 2005 20:56 GMT
> Thank you for your reaction!
> Im using it this way:
[quoted text clipped - 20 lines]
> Could the problem be the version of the SDK that im using ?
> I got 1.4.2.

Yeah, my mistake. That is correct, you should use the
getContentPane().add() not the deprecated JFrame.add().

Your error is indeed in your implementation of the Frame component. My
suggestion to you would be to remove lines until the problem is
revealed, or alternatively to start with a new, minimalist constructor.

//CODE
public MyFrame(){
         super("MasterMind");
         setSize(400,600);
         addWindowListener(this);
         Peg test = new Peg(100,100);
         add(test);
}
//END CODE

Also notice that I don't think you should be calling the show() method
in the Frame constructor; have the object that created the Frame object
decide when to display it. i.e.

Frame f = new MyFrame();
f.show();
Carl - 21 Dec 2005 21:04 GMT
> Yeah, my mistake. That is correct, you should use the
> getContentPane().add() not the deprecated JFrame.add().

Doh!, And I should also point out that the add() method for JFrame is
not actually deprecated, and have no idea I phrased it that way.
Anyways, you should indeed use the getContentPane().add() when using
JFrame and not add() as you would for Frame.

This is not really relevant to your project, I just wanted to clear up
the mistake in my previous post.

Carl.
Stefan Ram - 21 Dec 2005 21:26 GMT
>Anyways, you should indeed use the getContentPane().add() when
>using JFrame and not add() as you would for Frame.

 Last time I looked at

http://download.java.net/jdk6/docs/api/javax/swing/JFrame.html

 , I found no hint in this direction.

 Instead, "frame.add( child )" is mentioned in a prominent
 place, right at the start and seems to be quite approved.

 Quote:

     As a conveniance add and its variants, remove and
     setLayout have been overridden to forward to the
     contentPane as necessary. This means you can write:

         frame.add( child );

     And the child will be added to the contentPane.

 So, why should I not use this?
Carl - 21 Dec 2005 22:07 GMT
>>Anyways, you should indeed use the getContentPane().add() when
>>using JFrame and not add() as you would for Frame.
[quoted text clipped - 19 lines]
>
>   So, why should I not use this?

_You_ should use this, however it may be worth mentioning that in 1.5.0,
the add() method has "been overridden to forward to the contentPane as
necessary". The OP mentioned using 1.4.2, where the documentation states
that when "using JFrame you need to add the child to the JFrame's
content pane instead:   frame.getContentPane().add(child);"

See http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFrame.html for
more details.

Maybe I should have made the version bit clearer :) .

Cheers,
Carl.
Monique Y. Mudama - 21 Dec 2005 22:13 GMT
>>Anyways, you should indeed use the getContentPane().add() when using
>>JFrame and not add() as you would for Frame.
[quoted text clipped - 19 lines]
>
>   So, why should I not use this?

Depends on which JDK you are using.

As you might glean from your quote, add() did not always add to the
contentPane.  In previous versions, you ended up using the inherited
Container.add(), which did not do what you want.

To clarify, here's what the 1.4.x javadocs say in place of your
quote:

The content pane provided by the root pane should, as a rule, contain
all the non-menu components displayed by the JFrame. This is
different from the AWT Frame case. For example, to add a child to an
AWT frame you'd write:

frame.add(child);
 

However using JFrame you need to add the child to the JFrame's
content pane instead:

frame.getContentPane().add(child);
 

The same is true for setting layout managers, removing components,
listing children, and so on. All these methods should normally be
sent to the content pane instead of the JFrame itself.

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Neneko - 21 Dec 2005 21:29 GMT
I tried using your minimalist constructor Carl, and that does work!
Only when i make another object of Peg the one added first disappears.
But that might be normal ?

The reason it didn't worked before was maybe because i had a paint
methode in MyFrame? Because once i removed that code, the oval was
displayed.

@Monique
Im adding it the same way as in Carl's code.
/CODE
public MyFrame(){
               super("MasterMind");
               setSize(400,600);
               addWindowListener(this);
               Peg test = new Peg(100,100);
               add(test);
}

//END CODE
Roedy Green - 21 Dec 2005 22:53 GMT
>Yeah, my mistake. That is correct, you should use the
>getContentPane().add() not the deprecated JFrame.add().

His assignment is pure AWT, so the prof wants a Frame not a JFrame
which does not have the contentPane complication.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Oliver Wong - 21 Dec 2005 21:28 GMT
> Thank you for your reaction!
> Im using it this way:
[quoted text clipped - 20 lines]
> Could the problem be the version of the SDK that im using ?
> I got 1.4.2.

   The error is telling you not to use the add() method, where you have
add(test); You might want to replace that with getContentPane().add(test).

   - Oliver
Mickey Segal - 21 Dec 2005 23:02 GMT
> Public class MyFrame extends Frame implements WindowListener

I haven't looked at your code in detail but here:
http://www.segal.org/java/tooltip6/index.html
is an example of a working applet with full source code that has a Frame to
which is added a custom component.  The custom component extends Canvas, not
Component, but maybe the example will help answer the question.
pit.grinja@gmx.de - 22 Dec 2005 08:29 GMT
Hi Neneko,
> Thank you for your reaction!
> Im using it this way:
[quoted text clipped - 14 lines]
>         }
> Code//
As Roedy suggested in another part of this thread, you need to validate
your Frame. A call to "pack()" should do that.
HTH, Piet
Neneko - 22 Dec 2005 19:26 GMT
Sorry for my late reaction
But thanks alot for the very usefull information!

It works now! ^_^
Monique Y. Mudama - 21 Dec 2005 20:47 GMT
> Hello!
>
[quoted text clipped - 37 lines]
> for some days now.
> Thanks in advance!

Can you show the rest of the code?  The part where you're adding it to
something?

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Roedy Green - 21 Dec 2005 22:51 GMT
>I got a bit of an problem using my self-made component. Which is that
>it won't paint's itself..
>I noticed the object is being made, but for some reason the components
>paint methode isn't executed.
>Here is the code of the component:

I don't see anything obviously wrong. Perhaps the problem is elsewhere
in the code you did not provide.  Your component must be added to a
container and the whole thing validated and made visible.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



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



©2009 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.