File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes A Graphic and a JLabel on one JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "A Graphic and a JLabel on one JFrame" Watch "A Graphic and a JLabel on one JFrame" New topic
Author

A Graphic and a JLabel on one JFrame

Steven Joost
Greenhorn

Joined: Sep 29, 2012
Posts: 28
Hi guys,

I've got an problem(again)

Always If I place a Graphic(this time A Cubic Curve) and a JLabel on a Frame, It will only show one of both.
This is what I wrote, may you recognize the mistake



Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1763
    
    7

That's because of the default behavior of the default layout manager of a JFrame's content pane.
You should be able to figure out which layout manager is used and what the default behavior you get when invoking the add() method with respect to the size of the component you add, if you follow the link to the JFrame's API documentation


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Steven Joost
Greenhorn

Joined: Sep 29, 2012
Posts: 28
Didn't understand, can you post a code example?

I tried it this way, and it shows both, but now the Curve looks very weird(at least not, like at beginning)

Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1763
    
    7

The BorderLayout indedd is the default layout manager used by the content pane, and according to its API documentation:
border layout lays out a container, arranging and resizing its components to fit in five regions: north, south, east, west, and center. Each region may contain no more than one component ...

So your approach of adding each component to its own region seems like a reasonable fix, however the API documentation also states:
The components are laid out according to their preferred sizes and the constraints of the container's size. The NORTH and SOUTH components may be stretched horizontally; the EAST and WEST components may be stretched vertically; the CENTER component may stretch both horizontally and vertically to fill any space left over.

What that means is that the Curve (a JPanel) instance will be stretched horizontally, but the preferred height will be respected - if set. The JLabel will be likewise be stretched horizontally in the SOUTH region and the CENTER region will fill whatever size is left to fill the dimensions of the JFrame. You need to ask yourself if the BorderLayout is the most appropriate layout manager for the layout you have in mind. There's a visual guide to layout managers on the Oracle website, which may help you decide.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2288
    
  49
Actually there are more problems than just the layout manager and I'm not talking about the non descriptive variable and class names or the failure to follow Java naming conventions.
You should never put Thread.sleep() and/or repaint in any of the paintXXX methods. If you want to do animations then you need to use a background thread or Timer to change the components state and call repaint();
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1763
    
    7

Tony Docherty wrote:Actually there are more problems than just the layout manager


Many, but the layout manager is responsible for the problem where only one of the components is showing, so let's focus on one thing at a time
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Sounds like something which would fit better in our GUIs forum.
You really should start your GUIs very small. Start with a GUI which simply displays a frame. Then you can add to it. If you don’t have a book which describes the graphics, start here, though JavaFX may supersede Swing in a few years. You should find layouts in that tutorial, too.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4543
    
    5

In addition to what Tony has already pointed out, a painting method override needs to either paint the entire visible area of the component or invoke the super implementation.


luck, db
There are no new questions, but there may be new answers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A Graphic and a JLabel on one JFrame