• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Need help with Absolute Layout GUI

 
Pete Lindor
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am very new to the whole graphical side of Java, this needs no function ability at all. It does not need to be function able where it calculates anything it just needs to be graphically ascetic to the example picture, not exact.  I want to get a good idea of how to do this before I start doing a hard mid term assignment so I picked out a sample problem out of my book and would just like a walk through.  I know it would need a main method, and 2 classes. So right now I have GUI04Driver for the main method, GUI04FlowLayoutRadioButtons, and GUILayout as my 3 classes.  Here is a picture of what it is suppose to look like (attached) , any kind of help would be greatly appreciated!

The Sample Output uses 6 labels, 4 radioButtons, 4 textFields, 5 Panels and 1 button
a. Labels
i. TitleLabel
ii. radiolabel
iii. widthLabel
iv. heightLabel
v. radiusLabel
vi. lengthLabel
b. Radio Buttons
(note: buttons must be mutually exclusive…they must be part of a group)
i. rectangleRadioButton
ii. boxRadioButton
iii. circleRadioButton
iv. cylinderRadioButton
c. Text Fields
i. widthTextField
ii. lengthTextField
iii. heightTextField
iv. radiusTextField
d. Panels
i. radioButtonPanel
ii. widthPanel
iii. lengthPanel
iv. heightPanel
v. radiusPanel
sample.png
[Thumbnail for sample.png]
Sample output
 
Tim Holloway
Saloon Keeper
Posts: 21133
134
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Pete!

Absolute layout refers to the case where you demand that each control and display element be located at a specific X, Y pixel position. Or sometimes physical units (inches, mm, points).

It's actually discouraged in most cases, because it can end up unusable on some platforms. Not all display devices have the same dimensions or pixel scales. Or aspect ratios.

While monitor screen sizes are less likely to vary these days that they used to be, portable devices do vary quite a bit. And even on the same display. systems like Swing that can use multiple look-and-feel options can end up looking messy as the sizes, borders, and margins of different controls may vary from one look-and-feel to the next.

So the preferable approach is to use relative layout. With relative layout managers, the GUI has the freedom to move stuff around to fit the display it's working with. Instead of absolute positions and sizes, you give hints and it does its best to use them optimally. Although sometimes that works better than others. There is a certain art to it.
 
Campbell Ritchie
Marshal
Posts: 65814
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Moving discussion to our GUIs forum.

What to do about absolute layout? Don't. Swing® is designed to wok with layout manages, not absolute positioning. The reason for not using absolute positioning is that absolute positioning can't cope well with resizing displays, but the layout managers cope well. Thee ae unfortunately too many different types of layout for me to tell you about them all; they are listed in the Java™ Tutorials. It is much easier to start with layouts for something with fewer components to display.
Maybe I would find out about Cay Hostmann's GBC class and go straight to GridBagLayout, missing out the multiple panels. I would make sure to specify insets and anchors for every Component. Just maybe; there are many other ways to set out a GUI.
 
Piet Souris
Bartender
Posts: 3519
150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Pete,

welcome to the Ranch and enjoy the stay!

Do not use Absolute Layouts if possible. Certainly for the JPanel you showed us, using Absolute things means calcuating quite some coordinates. The problem comes when you want to change that layout later, meaning many recalculations if you are unlucky. If you are doing this, I suggest that, in your code, you add some buttons  to your panel, see how it looks and then ask, in your code, what the bounds of your components are. That'll give you some feeling about the coordinates.

A possible way to avoid all this is to use the GUI builder of your IDE. That takes some getting used to, including the way you must write your code that goes with it. But it is certainly a good way.

Another way is to use existing Layout Managers to accomplish a good looking panel. My first thought was to use a BorderLayout for your panel, with the title being a JLabel that goes to the North of the panel (BorderLayout.PAGE_START).

The JRadioButtons would be in a JPanel with vertical BoxLayout that you add to the left of the panel (BorderLayout.LINE_START).

The four textfields and captions would be in a JPanel also, with for instance a GridLayout with two columns and a decent in between space. I would place that panel in the BorderLayout.CENTER.

The big button in the bottom would be a normal JButton placed at the BorderLayout.PAGE_END.

Certainly not Beginners stuff, but if you want to go that way, try as much as you can.
 
Tim Holloway
Saloon Keeper
Posts: 21133
134
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
 
Pete Lindor
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my 3 classes atm.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Tim Holloway
Saloon Keeper
Posts: 21133
134
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use Code tags to make your examples easier to read. We have code tag support for Java code, XML, SQL and most other fixed-format text.

I've added them to your example.
 
Pete Lindor
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Piet Souris
Bartender
Posts: 3519
150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you notice, using this AbsoluteLayout or using no LayoutManager at all makes for nasty code that is hard to follow. You can avoid much of this, at the cost of getting to grips with some other LayoutManagers, but they do take away much of the hard work.

Let's look at the method 'createContentPane' in your Week5GUILayout class. To see the radioButtonPanel, you not only need to set its location, but you must also specify its size, in some way. That is because you use a null layout, so you take responsibility for sizing and placing.

So, before adding the rbp to your panel, you must do for instance:

However, the use of a dedicated panel for these radiobuttons is a good idea. Here is a part of what I suggested that takes care of the title and the radiobuttons.
 
Tim Holloway
Saloon Keeper
Posts: 21133
134
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's how you might get your sample display using primarily nested grid layouts, illustrated YAML-style:


You could also make the 2x2 value entry grid be a 2x4 and skip the sub-layouts, using gravity and weighting to ensure that the elements appear the way you want them. But the way I showed is less likely to get malformed if the screen size isn't co-operative.
 
salvin francis
Bartender
Posts: 2371
102
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like all things in code, this can be done in multiple ways Here's my solution using Tim's yaml style hierarchy:

 
Piet Souris
Bartender
Posts: 3519
150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never heard of yaml, but I understand there are programs that turn these things into real java code.
Can you do that and show the result here?

Thanks!
 
Tim Holloway
Saloon Keeper
Posts: 21133
134
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
YAML stands for Yet Another Markup Language.

Its virtue is its simplicity. As you can see from my example, it's basically just an outline form. It handles key/value pairs, collected key/values (dictionaries) and arrays. It's even simpler to parse than JSON, since JSON lines are actually JavaScript expressions, whereas YAML is strictly data. Output is likewise simple, since it's just basic text printing.

The Jackson library can process both JSON and YAML. It makes it easy for ReST services to request the same data in either format, just by indicating the acceptable MIME type.
 
please buy this thing and then I get a fat cut of the action:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!