2

I want to know is there any disadvantage to using setBounds() method in Java Swing? I have hardcoded the xy co-ordinate values to accommodate my 17-inch screen. Suppose my application is started in a 21 inch screen. Will the screen adjust automatically or will it distort? Is there any other disadvantage of using this method?

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Learner
  • 161
  • 1
  • 3
  • 16
  • 1
    Are we talking about setting the bounds of window or components? – MadProgrammer Aug 22 '12 at 06:57
  • for example adding a panel to a frame, adding multiple textboxes listboxes,buttons etc into a panel, either case, but mainly at component level. – Learner Aug 22 '12 at 07:02
  • 1
    See also [this question](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) for which many of the answers discuss the types of problems you might face. – Andrew Thompson Aug 22 '12 at 10:37

1 Answers1

7

is there any disadvantage of using setBounds

Yes, plenty. But most important of all, is the one you just mentioned.

You've just basically ignored one of the most powerful and useful API's in Java's UI toolkit, the layout managers.

These managers are responsible for making decisions on how best to represent you components on different screen sizes and resolutions without you having to think about.

If you decide to no longer use these layout managers, it becomes your responsibility to manage it and I don't know about you, I've got more then enough to do already.

The other problem is updating your screen. What happens when you want to add a new label. You need to go through ALL your code and figure out how much everything else needs to move around to accommodate it, including what to do when you don't have enough space.

And don't even get me started on FontMetrics :P

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank you for the reply. Is there anything i can do to reproduce this distortion. – Learner Aug 22 '12 at 07:29
  • 3
    @suraj You could not use `setBounds`. Try instead looking into using a good `LayoutManager` - [MiGLayout](http://www.miglayout.com) is quite popular. – Jacob Raihle Aug 22 '12 at 08:10
  • @JacobRaihle yeah MIGLayout is great – gontard Aug 22 '12 at 08:23
  • 1
    @suraj That is an open ended question, it comes to to how complex your UI's are. I tend to use combinations of layout managers, layering multiple panels within each other to achieve the results I want – MadProgrammer Aug 22 '12 at 09:32
  • Hi Jacob- I was trying the MIGLayout by downloading the miglayout jar but am getting compilation error saying Jpanel does not support miglayout. – Learner Aug 22 '12 at 10:17
  • *"..in the Swing toolkit, the layout managers."* (whispers) Most of them are in the AWT package. ;) – Andrew Thompson Aug 22 '12 at 10:38
  • @AndrewThompson don't tell em that :P – MadProgrammer Aug 22 '12 at 10:43
  • Why would we need to get you started about FontMetrics? LayoutManagers typically don't use that directly, they work with the component preferred size (and baseline) which you can also use if you don't use layout managers (or when you write your own layout). – Walter Laan Aug 22 '12 at 11:27
  • @suraj Odd - if it's not already working post a new question with the code you're trying to run. MiGLayout _does_ work with JPanels. – Jacob Raihle Aug 22 '12 at 14:43
  • @WalterAan, your right most layout managers don't look at FontMatrics directly, but if you wanted to do custom layouts, you'll probably find your self walking down his path eventually (given you're likly to avoid the pref/min/max methods) – MadProgrammer Aug 22 '12 at 19:53