Java JPanal Layout Question?

JC0133

Senior member
Nov 2, 2010
201
1
76
I am trying to figure out how to do 2 things.

I want to have 2 layouts.

Layout 1:
JLabel1 JTextField1 JTextField2
JLabel2 JTextField3
JLabel3 JTextField4 JTextField5
JLabel4 JTextField6
JLabel5 Jbutton


Layout 2:

JLabel1 JTextField1 JTextField2 JLabel2 JTextField3 JTextField4
JLabel3 JTextField5 JLabel4 JTextField6
JButton



There has to be a way to force the Widgets to be at a fixed position and offset from each other, no matter how big the JFrame and JPanel is?
 

Belyn

Junior Member
Jul 6, 2020
2
0
6
Use the layout managers for the jpanel (you can either set up two separate layouts, and instantiate as appropriate, or use either a cardlayout or a tabbedpane to allow the user to switch, depending on what you're trying to accomplish - both have decent docs in the below link )


Also, you can create mini jpanels as containers, each with their own layout manager, to be able to lock it down even more. If you need empty space in one of the layout managers that doesn't provide it automatically (or in the way you want), add spacer components to force it where you want. (for example: https://stackoverflow.com/questions/935293/lightest-weight-spacer-component-in-a-gridbaglayout )
 
Sep 29, 2004
18,656
67
91
You need to create your panel correctly. Not sure what your layouts are but if you always want to force them into a corner you should place your layouts in a JPanel that uses BorderLayout. Putting the JPanel into a parent Borderlayout to the east should shove everything to the right.

Swing is very powerful. Understand hte layouts and nest them to get the behavior you want.
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
Keep in mind that Java uses relative placement within layout managers to define where objects are rendered in a window. What you're talking about sounds a lot like absolute placement where you'd specify an object's top-left coordinates, width, and height. However, your layouts do heavily resemble what you'd use GridLayout or GridBagLayout for. You may want to check provided links and consider one of those layout managers.

Although, I'm not sure if you're married to using Swing, but personally, I'm much more of a fan of JavaFX. Prior to switching to FX, I really loathed creating UIs in Java as I felt like it was a never-ending battle with the layout manager. (It was usually just some stupid, small detail that never seemed to work... like a button not centering properly.) It would eventually get to a point where I'd give up on commonly used layout managers, and I'd end up using GroupLayout. Switching to FX also gave me access to a decent WYSIWYG UI editor, Scene Builder. It isn't perfect as I normally have to tweak the resulting FXML, but it's still quite helpful.

EDIT:

Oh, and it's also possible to insert spacers into your layout using Box.createRigidArea(..).
 
Sep 29, 2004
18,656
67
91
Aikouka,

I've done alot of GUIs in Swing. It takes time but eventually you don't really have t o think about it. Swing to me is how I can make something really fast that works. I do have to revisit things like painting cells i ntables and whatnot but it's not that hard.

JavaFX actually seems to take more time and is still kinda buggy to be frank. Labels with ICons can get wonky in JavaFX for example. JavaFX can be pretty though thanks to CSS.
 

cytg111

Lifer
Mar 17, 2008
23,993
13,520
136
Netbeans, the UI editor, mess around with it till you get something that fits your need in broad strokes... fine tune from there.
Ive done a lot of Swing by hand.
Never again.
 

purbeast0

No Lifer
Sep 13, 2001
53,024
5,905
126
Netbeans, the UI editor, mess around with it till you get something that fits your need in broad strokes... fine tune from there.
Ive done a lot of Swing by hand.
Never again.
I've done a lot of Swing period.

Never again!

Super old technology with so many better options out there now.
 
Reactions: cytg111

cytg111

Lifer
Mar 17, 2008
23,993
13,520
136
I've done a lot of Swing period.

Never again!

Super old technology with so many better options out there now.
Everything UI on the desktop is crammed into a browser nowadays... and in the pursuit of native performance the browser is getting stuff like native file system api ... its coming full circle and a billion billion dollars was wasted going round and round. I feel old.

 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |