0

I have gone through lots of tutorials which teach about layouts in Java Swing, but they don't seem to suffice my need. I am creating a solar system GUI using Java Swing, and i want to place the planets in the GUI according to the values i fetch from my micro controller, which are usually float point values. I cannot use the Grid Bag Layout, as to position a label i have to specify grid x and grid y, which cannot be the case since I receive float point values from the micro controller. The best resource i found is to use absolute layout where i can specify the position of the planet by giving mere X and Y Co-ordinates, which will be fetched from the micro controller. The problem I am facing now is that the absolute layout does not have auto re-size feature.

What would be the best possible option to adopt the auto re-size feature in absolute layout?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • One approach would be writing your own layout manager, that takes the coordinates as constraints. And then uses those properly scaled when laying the objects on the screen. – kiheru Nov 29 '14 at 23:26

1 Answers1

3

Swing tutorials were not generally meant for situations like this -- they were meant for people who want to write more normal GUI applications, using buttons, drop-down boxes, check boxes, radio buttons, menus, and have layout that follows currently accepted practices in terms of positioning those on the screens. If any of that applies to the part of your program that is not displaying planets, I encourage you to use what they have to say about it.

But you want to place things according to calculations of your own. I recommend doing that in a panel, calculating the size and position of your objects according to the size of the panel at the point of drawing. When the panel resizes, you will need to trap the event that says it is resizing and redraw. You will need to deal with your own minimums and maximums, etc.

I don't recommend the custom layout manager suggested elsewhere for a couple of reasons. Firstly, it won't save you any work at all -- you are still going to have to write the code that determines positions of things, if you just then draw your own graphic instead of attempting to position a UI element, I think it will actually be less work. And that's the second reason -- layout managers' purpose is to position UI elements within the panel, and the pieces of your solar system don't really have any need to be UI elements, just graphics on the screen.

Good luck.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • Oh, yes. If the objects don't have any functionality, then a layout manager is overkill. I just assumed they'd have, but that may have been unwarranted. – kiheru Nov 29 '14 at 23:39
  • I've already made a similar recommendation in the original poster's [similar previous question](http://stackoverflow.com/questions/27158992/positioning-a-label-using-swing-in-java), but the original poster simply ignored my recommendations completely, no comments or anything, so you may be wasting your time answering him. – Hovercraft Full Of Eels Nov 29 '14 at 23:45
  • `I don't recommend the custom layout manager suggested elsewhere` a custom layout is always a good suggestion. `you are still going to have to write the code that determines positions of things` - Exactly! If you need to write custom code anyway, then put the code into the layout manager where you have a well defined interface to implement the layout. You don't need to worry about adding listeners to you panel to invoke the layout when the size of the panel changes. This is done automatically. Don't try to reinvent the wheel by placing the layout code randomly in your class. – camickr Nov 30 '14 at 01:34
  • @camickr: `"a custom layout is always a good suggestion"` -- not if the best solution does not involve the laying out of components. The OP is trying to do an astronomy animation, a program that likely would be best modeled using MVC with the V being a drawing JPanel that moves sprites, not components. I'm with arcy on this one. – Hovercraft Full Of Eels Nov 30 '14 at 02:40
  • 1
    @HovercraftFullOfEels `not if the best solution does not involve the laying out of components.` - misread the suggestion. Agreed, if the suggestion is to do custom painting of all the planets, then a layout manager is not required. – camickr Nov 30 '14 at 04:16