Java Changing JLabel Text changes JLabel Position - java

I have a problem, I have a JLabel inside the content panel which layout is absolute X Y.
The problem is that the text of my JLabel is centered on JLabel, but when I add a few letters to the text, the position of the JLabel is pushed a bit back.
Can't I fix the JLabel position and use it always for the entire application? even if the text changes.. ?
Thanks in advance ;)

When the horizontal alignment is set to JLabel.CENTER, the text will want to expand let and right about the center position of the label.
This means that the label is doing exactly what the label was asked to do.
To,over come this, you should use JLabel.LEFT as the horizontal alignment and position the label itself as you need within its parent container

The label is in the same place. It's position is the upper left corner. With less text in it, it will be smaller.
If you want to use it with absolute layout, set its position and size.

Related

Can't resize JLabels in BoxLayout

I'm trying to set up a few JLabels to use as buttons inside a BoxLayout, stacked on top of each other. The layout is fine, but I'm finding that I can't resize the labels to the dimensions I want. I'm using the following code to size them:
JLabel fileAddBtn = new JLabel("Add File", SwingConstants.CENTER);
fileAddBtn.setBorder(BorderFactory.createLineBorder(Color.black));
fileAddBtn.setMaximumSize(new Dimension(Integer.MAX_VALUE, fileAddBtn.getMinimumSize().height));
and
JLabel fileRemBtn = new JLabel("Remove File", SwingConstants.CENTER);
fileRemBtn.setBorder(BorderFactory.createLineBorder(Color.black));
fileRemBtn.setMaximumSize(new Dimension(Integer.MAX_VALUE, fileRemBtn.getMinimumSize().height));
As of now I have two labels, with one being longer than the other. They are both taking the width of the longer label, which is good, but the labels are hugging the edges of the text right to the nearest pixel. Is there any way to make the labels a little bigger so that there is a bit of a border around the labels? I've tried using setSize() but it doesn't take. I've also added straight values into the above code, but it doesn't change them either. I tried adding an EmptyBorder() around them, which worked for sizing, but it hid my line border which surrounds them. Any thoughts?
Is there any way to make the labels a little bigger so that there is a bit of a border around the labels?
Sure. Add an EmptyBorder.
But since the code is already adding a border to the labels, to retain that line border, make a CompoundBorder consisting of the empty border and the line border, and set the compound border to the label.
See also Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? (Yes.)

Get a Fixed sized GridLayout

I need to make a fixed sized for a GridLayout with 100 buttons located in the center portion of a BorderLayout. On the east portion of the border layout is another Gridlayout that keeps shrinking the center component whenever the text is longer then the size of the current JTextAreas located in the east. The JFrame is not resizable also.
Is there a way to get a fixed size for the center component while allowing the JTextArea to still expand?
"I need to make a fixed sized for a GridLayout with 100 buttons located in the center portion of a BorderLayout".
Sorry, but that's not going to work. BorderLayout doesn't work like that. You can nest JPanel containers with different Layout managers to get your desired effect.
"Gridlayout that keeps shrinking the center component whenever the text is longer then the size of the current JTextAreas located in the east."
You should wrap your text area in a JScrollPane, and setLineWrap(true) and setWrapStyleWord(true) on you text area. The last two will set it, so that the line typed wraps when it is reaching the right edge of the text area. Also If you are setting the size to the text area, don't. Instead, use the following constructor to set its size
JTextArea jta = new JTextArea(20, 50); <--- rows, and character columns
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
JScrollPane scroll = new JScrollPane(jta);
container.add(scroll); <--- make sure you don add jta anywhere else
Without more context to your querstion, these are really the only valid suggestions I can make.

Layout labels from left to right

I would like some help on choosing or creating a layout for a component to which I will add labels at runtime. For example the component size will be 400*50 and empty when the application start up. Then some action will add a first label at position 0, the second at position first label's width, the third at first label's width + second label's width and so on. A bit like the FlowLayout except that I want my labels to start at the left of the container and not in the center.
Thanks for helping.
Just use a FlowLayout and call setAlignment(FlowLayout.LEFT); (or use one of the constructors that takes an alignment code).
You can use FlowLayout(FlowLayout.LEFT)

JLabel with Icon aligned to left

I have a JLabel with a Icon and text aligned to bottom, the icon is center to the text,
how do i have the Text and the icon aligned to left and the text below the icon.
My current code is as below
label.setIcon(new ImageIcon(fileName));
label.setText("This text is going to be varying size and can be bit long");
label.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
label.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
If you mean someting like this, then I cheated.
Basically, I created two JLabels on a JPanel. Using a GridBagLayout I aligned the two labels onto of each other and anchored to the WEST
It would pretty easy to create a custom component for the purpose if you wanted to reuse it.
your just setting the Position of your text in your code..not the Icon it self
Try to separate the icon in the label then format it using set bounds or any formlayout

Java's FlowLayout does not resize correctly

I created a JFrame initialized with a BorderLayout and a JScrollPane as its CENTER element.
The scroll pane is set with VERTICAL_SCROLLBAR_ALWAYS and HORIZONTAL_SCROLLBAR_NEVER policies. The intent of my frame is to have a controlled width, while the height should grow/shrink as data is added/removed.
Inside my scroll pane, I added a simple JPanel (lets call it the content panel) which is initialized with a FlowLayout (and LEADING policy).
In order to test this, I simply populate my content panel with 20 JLabel("Item " + n) components where n is the loop counter.
I would expect to see my labels shown on a single row if the frame is large enough and the labels wrap to other lines when I shrink the width. But instead, there is only a single line displayed with no wrapping... ever.
Does anyone know why the flow layout does not wrap when a scroll pane is involved?
If I remove the scroll pane all together and put the content panel directly in the frame, the desired wrapping effect occurs, but if the frame height is shrunk smaller than the content panel height it just disappears.
The idea is that I want my labels to be wrapped when necessary but also always be visible if it means having to scroll up/down.
Any suggestions on how to fix this?
Thanks.
Wrap Layout gives an explanation and a solution.
If you work with the designer, you have to set the prefferedSize property to null (delete what is set) then set the preferred size by clicking the triple dots [...] button next to the prefferedsize property name and put your preferred value.
I encountered the same problem and it works for me.

Categories

Resources