Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Restrict size of a JComponent  (Read 2167 times)
0 Members and 1 Guest are viewing this topic.
Offline ToXSiK

Senior Newbie





« Posted 2011-05-11 23:02:59 »

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
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-05-12 01:29:39 »

look into setPreferredSize(Dimension), setMaximumSize(Dimension) and setMinimumSize(Dimension) Wink

Offline ReBirth
« Reply #2 - Posted 2011-05-12 02:24:52 »

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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ToXSiK

Senior Newbie





« Reply #3 - Posted 2011-05-12 02:29:01 »

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
1  
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 ^_^  We probably be back here soon XP
Offline BoBear2681

JGO Coder


Medals: 18



« Reply #4 - Posted 2011-05-12 02:46:49 »

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.
Offline ReBirth
« Reply #5 - Posted 2011-05-12 11:46:38 »

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

Thanks for the responses guys, only java forum I got replies to so far ^_^  We probably be back here soon XP
That's why I'm here Smiley "we"?

Offline cylab

JGO Ninja


Medals: 43



« Reply #6 - Posted 2011-05-12 11:48:52 »

Thanks for the responses guys, only java forum I got replies to so far ^_^  We probably be back here soon XP
That's why I'm here Smiley "we"?
Probably a typo and has to be: "Wee probably back here soon"  Wink

Mathias - I Know What [you] Did Last Summer!
Offline ToXSiK

Senior Newbie





« Reply #7 - Posted 2011-05-12 12:21:28 »

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
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2011-05-12 15:23:53 »

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 Smiley

Online philfrei
« Reply #9 - Posted 2011-05-12 23:15:04 »

My favorite layout is "null".

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #10 - Posted 2011-05-12 23:18:05 »

My favorite layout is "null".
Bahahahahahaha, yes that is useful too Tongue

Offline BoBear2681

JGO Coder


Medals: 18



« Reply #11 - Posted 2011-05-12 23:42:04 »

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.
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #12 - Posted 2011-05-12 23:52:33 »

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

Offline pjt33
« Reply #13 - Posted 2011-05-13 12:20:38 »

There are also folks who use GridBagLayout exclusively.
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.
Offline ToXSiK

Senior Newbie





« Reply #14 - Posted 2011-05-13 17:55:08 »

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  Grin

Now, onto the jumping and falling physics... Dare I make a thread in java2d already?   Cool
Offline ReBirth
« Reply #15 - Posted 2011-05-14 03:05:47 »

gridlayout and absolutelayout Cool

Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #16 - Posted 2011-05-14 03:40:25 »

AbsoluteLayout? I don't see that in the Java API Huh Don't you mean "null" layout? Cool

Offline ReBirth
« Reply #17 - Posted 2011-05-15 03:20:51 »

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

Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #18 - Posted 2011-05-15 04:26:11 »

Yeah I didn't know about that alternative name Roll Eyes

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (18 views)
2014-08-28 18:26:30

CopyableCougar4 (26 views)
2014-08-22 19:31:30

atombrot (39 views)
2014-08-19 09:29:53

Tekkerue (36 views)
2014-08-16 06:45:27

Tekkerue (33 views)
2014-08-16 06:22:17

Tekkerue (22 views)
2014-08-16 06:20:21

Tekkerue (33 views)
2014-08-16 06:12:11

Rayexar (70 views)
2014-08-11 02:49:23

BurntPizza (46 views)
2014-08-09 21:09:32

BurntPizza (37 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!