• Post Reply Bookmark Topic Watch Topic
  • New Topic

JComponent problem ?

 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What I want to do
I am trying to build a custom JComponent, inside a JLabel on the top and another a JLabel in the bottom. (just for test). This JComponent is added to JFrame and show up.

Problem I have
JComponent can not show up.

Question:
1. I know I can draw something on it. and it works. But, how can I use graphic API to draw a JLabel?
2. why JComponent can not has getContentPane(), different from JFrame ?
3. inside JComponent, Layout is allowed.

code

 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You almost had it. Comments:
1 - in BorderLayout when you use the add method without a constraint, eg, BorderLayout.EAST, the default behavior is to add the component to the center section as if you had specified the constraint BorderLayout.CENTER. If you add a component (JLabel top) to the center section and then add another component(bottom) to the center section the first component is discarded and the second component is shown. See the BorderLayout api and also the lesson Laying Out Components Within a Container in the java tutorial.

2 - In JComponent it is the preferred practice to override the paintComponent method for custom painting. The paint method is more general and calls paintComponent and other methods. See these methods in the Method Detail section of the JComponent api for specifics. Also, see the lesson Performing Custom Painting in the tutorial for basics in custom component painting.

Responses to your questions:
1 - the code below demonstrates one technique for drawing a label. The TextLayout class is another way to do this and its api has sample code.

2 - top–level containers have content panes — see the tutorial lesson Using Top–Level Containers for more information.

3 - the Using Layout Managers page in the Laying Out Components Within a Container lesson (mentioned above) has the answer to this.

In the code below I changed the class names so you can run it without name clashing problems.

[ August 27, 2004: Message edited by: Craig Wood ]
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.

Edward
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more question:

In the example above, if I want to draw the whole JComponent in some specific location, say, starting from (200,200), in the JFrame. How can I do that?

Thanks
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When we are dealing with components, eg, JComponents, JPanels, JLabels, etc., we generally add them to a container with a layout manager. In this approach we can specify some size and constraint information for the layout manager. Otherwise we can use a null layout manager and position the components absolutely. This can become very complicated but sometimes it's what we need. In the beginning, learning layout managers seems very complicated.

When we are dealing with drawing it usually occurs within a component and that component can be added to a container whose layout manager will place it for us.

So we have options. And this leads to questions. Is this the only component in your JFrame or in a certain section of it? If you want only this one custom component in the center section you can specify either the size of the JFrame (setSize) or the size of the component (setPreferredSize) and start your graphics at x and y = 200. Or you could use a layout manager that will position your component in the properly–sized JFrame. It gets a little thick from here on. We could help you with a specific task or point you back into the tutorial...
[ August 27, 2004: Message edited by: Craig Wood ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!