Create a Java swing / awt Layout with JLabels of different heights - java

I hava a JLabel (lets call it main) and during Runtime I am adding multiple JLabels to it. Sometimes these JLabels contain an Image, sometimes a JTextArea. The Problem is, because of the different Content they have different sizes. As long as I only add Text the layout is like I want it, just the size of the text. But if I add the first JLabel with an Image as content every JLabel I already added increases its height to the one from the JLabel containing the image.
My question is, which Layout should I use for the main JLabel and how should I add the other JLabeles which I add during runtime?
I first tried the GridLayout and now have the BorderLayout but not like I want it to.

Related

How would you create a layout like this?

I would like to create the main menu for my program and I have difficulties with Swing.
How would you code an alignment like this?
The middle elements would be the options like exit or settings and this window should be able to be resized and its contents should get bigger proportionally.
Single column GridLayout with vertical padding declared in the constructor. A grid layout will stretch components to fit the available space.
Add a large EmptyBorder to the JPanel that contains the 3 buttons, and that's the job done.

What kind of layout should I choose to put n JLable of a specific size?

I need to put n(10 for instance, the actual number depends on some circumstances) JLabel in a JPanel in a row. Those JLabels should have size 50x50. What layout should I select? As said in this answer we should avoid using setXxxSize(...).
Here is what I'm trying to get:

Using an image instead of JDialog/frame to hold swing components?

I currently have a JDialog (class that implements JDialog and is constructed like a jframe), and has 3 swing buttons placed on it. Currently I have it set, undecorated = true, to hide the outer frame. Is there any way to use my image to replace the default square frame?
This is what I aim for :
The blue square with shadow is the pre made image.
Regards
The blue square with shadow is the pre made image.
Well, the best way would be to set the background of the panel and then add a ShadowBorder to the panel. This will provide you with far more flexibility in the future as you can create many panels with different colors and reuse the same ShadowBorder instead of having to create an Image every time. I don't have an example of a ShadowBorder, but you might find one if you search the web.
Is there any way to use my image to replace the default square frame?
But if you really want to use your premade Image, then you can just:
create a JLabel and add your Image to the label as an Icon
add the label to the dialog
set the layout manager of the label
add your components to the label.

Cosmetics with GridLayout

I'm posting this question because I'm new to programming at the present time and I have a pet peeve that when I create the app I don't want the objects to go across the entire window.
I use GridLayout the most often and I was wondering if there was a way to make components such as a JTextField or JTextArea NOT span the entire window, leave a little space on both ends?
You can add a component to a JPanel, which uses a FlowLayout by default and all components are displayed at their preferred sizes. Then add the panel to the layout using the GridLayout. The panel will increase in size but the components on the panel will stay at their preferred size.

Maintain JComboBox Size

I want to make an account screen for a project, but I'm still new to GUI's. This is my first time working with a JComboBox and I'm having a bit of trouble. I want to basically place the JComboBox inside a box, which will be part of my background image. I tried using BorderLayout, but that just made a giant combobox that took up my entire screen. I have my code here and a drawing which illustrates my goal below:
See this answer for 2 layouts that can easily center the panel containing the combo box.
Use borders and layout padding within that panel for the white space required.

Categories