Restrict size of a JComponent

Title says it all. I have an object class that I have created that extends JComponent. When I add is to a JFrame, I want the size to be limited to only a certain area. I am overriding the paintComponent method to display my graphics for my game.

Also, the JFrame is BIGGER than the component, so I cannot simply force the component to abide by the frame size.

Thanks!

ToXSiK

PS, Hey Everyone! My first post! xP

look into setPreferredSize(Dimension), setMaximumSize(Dimension) and setMinimumSize(Dimension) :wink:

setPreferredSize gives a size that should appear when a component start but still can be resized by user. If defining setMaximumSize and setMinimumSize with same dimension, I think it’ll work.

Hey guys, thanks for the replies. I figured it out finally, I had the component inside of a BoxLayout panel and BoxLayout likes to resize components like mine without asking >:{ So anyways, if anyone else has this problem, here’s the fix.

Add it to a JPanel with no formatting and add your component to that (the panel will not resize it).

Also in your component constructor you must put

this.setPreferredSize(new Dimension(x, y));

Like you guys said above for it to only display that much.
Thanks for the responses guys, only java forum I got replies to so far :slight_smile: We probably be back here soon XP

Note that in general, the minimum, preferred, and maximum sizes of components/containers are only hints. LayoutManagers are free to ignore them, or pick and choose which ones they honor. So it’ll depend on the LayoutManager of the parent Container. I think this is part of what makes folks not like Swing very much, it seems illogical when they set such properties and then see them seemingly ignored in their UI.

yeah, so I did it on reversed way. Put everything on a JPanel containing Layout rule first, the set the size of that JPanel.

That’s why I’m here :slight_smile: “we”?

Probably a typo and has to be: “Wee probably back here soon” :wink:

hehe I meant to type “will”, but I like the guesses! =)

And I have to agree with you, swing has so much potential but its complexity turns many people away (It turned me away at first, I was just using netbeans little “GUI for making a GUI”, but it is not very customizable and felt clunky to me).

Anyways, another question, what is your favorite layout manager? I started with GridBagLayout, but with BoxLayout you can specify component sizes and the will usually abide to them (Except for my custom component) >.<

Curious to hear :yawn:

You can use as many JPanels and Layout Managers as possible. I use BorderLayout as my topmost level layout, the Box to make vertical or horizontal boxes, GridLayout to lay things out in a grid, and GridBagLayout for more control over the placing of components.

Of course, there are numerous more layout managers that I’ve yet to experiment with, such as CardLayout, SpringLayout, and the exciting “new” OverlayLayout :slight_smile:

My favorite layout is “null”.

Bahahahahahaha, yes that is useful too :stuck_out_tongue:

Well, for everyday Swing programming you’re not really supposed to be setting the min/preferred/max sizes of components to get your UI looking good. Sometimes setting the preferred size can be helpful (or custom components, for example, should compute their preferred size themselves, depending on their contents), but in general, you should rely on your layout managers to be sizing and positioning your components properly. It’s not so much having a “favorite” as it is knowing which ones to use to get your expected results in different situations, which mostly comes with experience.

That said, you’d be amazed just how far you can get with just BorderLayout, BoxLayout and GridLayout, along with intelligent use of empty borders for spacing. SpringLayout gets the job done for “columns” of components, but is unnecessarily confusing, and doesn’t handle RTL locales properly, unlike the other built-in LayoutManagers. There are also folks who use GridBagLayout exclusively.

“null” layout or absolute positioning is tempting because of the simplicity, but you lose all of the fun that LayoutManagers bring to the table, such as auto-repositioning/resizing of your GUI when the user resizes it. null layout also risks your GUI looking different from one machine to the next even based on factors such as a user’s OS, or even their system font settings. Things that align properly on your system might not align properly on another system when using null layout.

Which is why I have no 1 favorite layout; each does its own job and does it well.

I’m one of them for desktop programming. For a PocketPC app I wrote back in the day I had to write my own LayoutManager. I can’t remember now the precise details of why none of the default ones in PP was suitable.

GridBagLayout is really good, but when you give it control of your panels, it does what it pleases to them. BoxLayout was nice because I can set the buttons/components to align through setLayout(BorderLayout.X_AXIS) and everything was sized appropriately and looked nice. Also, I feel like adding rigid spaces in Panels are heavily under-used. I used them a lot this time and with that you can really make your GUI look nice =)

I will post a picture of it when I get back to the house of it, so you guys can see what you helped me accomplish ;D

Now, onto the jumping and falling physics… Dare I make a thread in java2d already? 8)

gridlayout and absolutelayout 8)

AbsoluteLayout? I don’t see that in the Java API ??? Don’t you mean “null” layout? 8)

Null layout is also called absolute, cause you need specify the point and the size of component 8)

Yeah I didn’t know about that alternative name ::slight_smile: