0

I have a swing project and noticed that the elements inside the JFrame is statically located inside the JFrame and I do not have the option of resizing the elements as I want to.

My gui app inside the designer window looks like the following:

I want to resize the button (E.g.) by dragging the corners of the button, but I am not allowed to?

As you can see on the following picture, the dragging is not allowed per pixel, but only per section in the JFrame:

How can I disable the static placement of the elements/Enable the self-dragging of elements inside the designer window?

Birdman
  • 5,244
  • 11
  • 44
  • 65
  • 1
    *"want to resize the button (E.g.) by dragging the corners of the button, but I am not allowed to?"* The problems with that ***start with*** a) You cannot know in advance how big the component needs to be on someone else's machine (using a different Java version, screen size/resolution, PLAF etc.) b) It makes for unresizable GUIs.. – Andrew Thompson Apr 25 '12 at 08:46

2 Answers2

4

Most likely you will need to disable the LayoutManager. On Netbeans, Setting this to null would provide you full control over the location and dimension of the child elements, so I am assuming that something similar should work here (although you seem to be using Eclipse, if that is the case, please state what plugin you are using).

It is to be noticed however that usually you want to have a layout manager taking care of how your components are rendered. I would recommend you take a look here for some more information on layout managers prior to removing them completely.

npinti
  • 51,780
  • 5
  • 72
  • 96
  • Thx! I changed the argument of the "contentPane.setLayout();" method call from "new BorderLayout(0, 0)" to "null" and now I'm able to resize dynamically. – Birdman Apr 25 '12 at 08:53
  • @Alex: If this answer helped you then mark it so, it will motivate others to help you in the future :). That being said, I used to set the layout to null and used to have all kind of weird positioning issues, solved only by not allowing the user to change any of the dimensions used. As I stated in my answer, I would strongly recommend you try and see if there is a layout manager which suits your needs. – npinti Apr 25 '12 at 08:56
  • Setting the `LayoutManager` to `null` is not specific to Netbeans, it's how Swing works. – Joey Apr 25 '12 at 08:56
  • *"now I'm able to resize dynamically."* Now your GUI is ***completely*** broken. :( @npinti I have noticed some great answers from you, but that answer is not among them. – Andrew Thompson Apr 25 '12 at 08:56
  • @Joey *"..it's how Swing works."* Didn't you mean.. "..it's how Swing breaks."? – Andrew Thompson Apr 25 '12 at 08:57
  • 1
    @AndrewThompson: I did stress (enough I think, in both my answer and my comment) that leaving Layout Managers out of the equation will eventually yield problems. If there is anything else which I should point out, by all means let me know :) – npinti Apr 25 '12 at 08:58
  • @Joey: I was referring to the right click -> setLayoutManager functionality available in Netbeans, not how to do it with code behind ;) – npinti Apr 25 '12 at 08:59
  • @npinti I tried accepting your answer, but there's a time limit before you can accept an answer, so I will therefor accept you answer as I type this :-) – Birdman Apr 25 '12 at 09:00
  • @npinti I chose not to down-vote, though it is border-line. IMO we should encourage people towards '**not** shooting themselves in the foot' by '**not** handing them the ***weapon'.*** – Andrew Thompson Apr 25 '12 at 09:02
  • @AndrewThompson: I do agree with you however sometimes it is either too late or too complex to revert back to how things should be... I hope that the OP will now take in consideration using LayoutManagers and will design his forms accordingly. – npinti Apr 25 '12 at 09:06
  • *"too late"* This person is obviously new to GUI programming and it is better to establish the 'right way' early on. If they are at a point where the work is due tomorrow, they should have planned better. *"or too complex"* Arguable either way, suitable for a single use/throwaway app., but not suitable for anything that needs maintaining. I am tending more and more towards down-voting this post. See [Thread: setLayout(null) is never necessary. Ever!](https://forums.oracle.com/forums/thread.jspa?threadID=1351374) in case that might sway you differently. – Andrew Thompson Apr 25 '12 at 09:12
  • @Andrew Thompson: Down-voting this post would just be childish, since I decide what to do with my project. I now know that it's bad practice to disable the LayoutManager, but that's not what the point is in this project. – Birdman Apr 25 '12 at 14:16
  • @Alex: I understand why Andrew is stressing this issue and I do agree with him, **for any decent, real life Swing application you have to use layout managers** but there are some scenarios like delivering some course assignment which just requires some quick and easy GUI front end for which using a layout manager could result into a waste of time... I've been there... – npinti Apr 25 '12 at 14:53
3

Setting size and position of components in a GUI should be left to the JRE at run-time, using:

  • The preferred size of the component (with current content, in the current PLAF).
  • The borders/padding/insets set to the component.
  • The layout used.
  • The component spacing defined in the layout constructors.
  • The layout constraints used when adding components to the layout (especially important to e.g. BorderLayout and the much maligned GridBagLayout, as well as many 3rd party layouts).

Generally, complex effects are created by using a nested layout.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Not going to downvote the `null`-layout answer (I find the warning sufficient), but will try to get that answer towards the bottom of the page by upvoting the other answer(s) stressing that you should use a layout manager – Robin Apr 25 '12 at 09:16
  • 1
    @Robin Thanks for not turning to "the Dark Side" of component layout. ;) – Andrew Thompson Apr 25 '12 at 09:25
  • Making the switch to component layout is on my to-do list, right after putting hot nails through my eyelids. – Robin Apr 25 '12 at 09:47