0

enter image description here

Hello, is there a way of specifying a point on screen where a component should be drawn? Shown the picture above, the JScrollPane is drawn in the center, by default (using flow layout). I want to be able to do something like setLocation(100, 100); and it will draw at that position. Would I be required to write my own layout manager? Or not? Thanks a lot

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user1009569
  • 477
  • 6
  • 22
  • 1
    null layout manager? I don't recommend it though. – Eng.Fouad Sep 21 '13 at 10:46
  • Can you set the location of the component with null layout manager? Why would you not recommend it? – user1009569 Sep 21 '13 at 10:49
  • 2
    Null layouts are not recommended because they are limited and do not take into the variable nature of modern operating environments or how a component should react to changes around it. The answer is, generally, no – MadProgrammer Sep 21 '13 at 11:04
  • Thanks a lot. Setting the layout to null is exactly what I need really, got it working fine thanks :) – user1009569 Sep 21 '13 at 11:13
  • 1
    *"Setting the layout to null is exactly what I need really"* So.. can you handle the derision offered when you ask the next 10 questions about getting the `null` layout to work? – Andrew Thompson Sep 21 '13 at 11:16
  • "So.. can you handle the derision offered when you ask the next 10 questions about getting the null layout to work?" As I had stated, I'd already got it working, using setBounds method. There's no need to be a dick my friend... – user1009569 Sep 22 '13 at 17:12

1 Answers1

3

Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. Writing a custom layout manager as you suggest is one way to go, but you need to account for the many things listed above. But there is probably a simpler solution.

Use combinations of layout managers1, along with layout padding & borders for white space2.

1.

2.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thanks for your input but I've decided I'm not going to necessarily need a layout manager, I'm using internal frames now, thanks anyway. – user1009569 Sep 22 '13 at 16:53
  • *"I'm not going to necessarily need a layout manager, I'm using internal frames now"* ..and what exact layout are you going to be using for the components *inside* the internal frames? No matter what hoops you jump through, it will be necessary to layout the components, and layout managers are the way to do that. – Andrew Thompson Sep 22 '13 at 17:13
  • I won't be using a layout. The internal frame will only be holding one component. I'll have 3 internal frames, 1 with a JTree on, 1 will display an image and the final shall have properties on it. I'm not jumping through any hoops in order to take the easy way, this is what I was looking for. – user1009569 Sep 22 '13 at 17:18
  • So. A `BorderLayout` or `GridLayout` to get the single component to stretch to the available width and height, or a `GridBagLayout` to have it 'centered at natural size'. What magic are *you* using to layout the components, voodoo? – Andrew Thompson Sep 22 '13 at 17:26
  • What are you talking about now...? I want each component to be in separate internal frames so they can be moved, resized iconified etc – user1009569 Sep 22 '13 at 18:18
  • *"..so they can be moved, **resized** iconified etc"* And when they are resized, what is *supposed* to happen to the component inside? Should it stay the same size, exactly where you left it, even if the user makes the internal frame so thin that it does not have enough width to display it? Post an SSCCE of your '`null` layout with internal frames' and I will be able to post a number of screenshots as to how it **fails dismally** to behave well. – Andrew Thompson Sep 22 '13 at 18:29
  • The program is only going to be used by me so it's not a worry tbh. – user1009569 Sep 22 '13 at 19:07