JLabel remove space between text and border - java

I am creating a JLabel with only a text (customized from another part of the program). I cannot post images here, but the label comes with padding (meaning there is a gap between the text and the border.)
I don't want the padding to be so thick. Is there a way to compress the padding so it won't be so thick? These are the things I have tried (one by one, not altogether)
setPreferredSize(width, height);
setBorder(new EmptyBorder(new Insets(0,0,0,0)));
setBorder(new EmptyBorder(new Insets(-5,-5,-5,-5)));
but none of them works. at most they shrink my JLabel but the padding stays the same. The padding ends up covering my text instead. I understand setMargin may work but JLabel doesn't seem to have setMargin so I'm lost. Any help would be appreciated, thank you!

I figured it out. The 'border' that I saw was actually the panel instead of just the label. The label's background itself was transparent (I did not setOpaque(true) for the label). Turns out my label has no padding at all, which is what I wanted. I understand why there is a padding on the panel too (done by FlowLayout) so all is well. Thank you mKorbel for the speedy response! I'll close the question.

Related

Java NetBeans GUI design Vertical text align

I'm making a Pascal triangle and I want vertical text align.
I output everything in Text Panel, I tried many options here but none seems to work. There are some align options in this editor but there are too many, I don't know which one will do what I want, can't find anything about that anywhere.
When it gets to double/triple digits it's not aligned vertically anymore. I'm making it from left to right, not in triangle shape.
To vertically align text on adjacent lines, a fixed width Font (such as Courier) can be used. If using Swing, you can can set the Font of a JTextComponent using the appropriate method:
JTextArea textArea = new JTextArea();
textArea.setFont(new Font("Courier", Font.PLAIN, 12));
If rendering with html, you can use the <pre> tag.
The fixed width Font will let the nth character on one line be aligned with the nth character on all other lines.

Adding margins in a JTextArea?

So I have a JTextArea which shows the text right from the top left corner. I want some margin on all 4 sides, so there's some space between text and boundary of the area.
I have researched a lot and could not find any solution. How can I do it?
Also, I was thinking maybe put up a label on all 4 sides to create dummy margin. How can I create a JLabel with certain width and height?
May be I dont understand your question correctly. However you can use setMargin()
// set the margin for all four sides
tt.setMargin( new Insets(10,10,10,10) ); // tt is JTextArea instance
Some Important Links
1. setMargin API
2. Class Insets

Limiting a JScrollPane's scroll range

I have a JTextArea wrapped in a JScrollPane, which I use to log my application's output. I'm using the default, plain font with a size of 9 for the text area, and the scroll pane's height is 48 px. This results in an even distribution of lines in the scroll pane view, but there's a problem: if you scroll all the way up or all the way down, this happens:
As you can see, the top line got cut off, which is why I'm wondering if there's a way to limit the scroll pane's scroll range so it, for example, can't reach the top or bottom 6 pixels. Alternative solutions are also welcome.
You could change the margin (top/bottom) of your JTextArea by setting a custom Border using the method setBorder inherited from JComponent. The documentation for JComponent suggests the following:
Although technically you can set the border on any object that
inherits from JComponent, the look and feel implementation of many
standard Swing components doesn't work well with user-set borders. In
general, when you want to set a border on a standard Swing component
other than JPanel or JLabel, we recommend that you put the component
in a JPanel and set the border on the JPanel.
That would yield the same result as limiting the scroll range, while being more straight forward.
EDIT:
OP reported that the following solution worked for him:
textAreaLog.setBorder(BorderFactory.createEmptyBorder(0, 6, 0, 6));
Place the JTextArea in a JPanel with empty borders where top and bottom insets are 6 pixels?

Borders around ImageIcons appear to have zero border, hgap and vgap are zero, still empty space between ImageIcons

There is a persistent empty space around the ImageIcons. I created new empty borders on all the objects like so:
array[i].setBorder(BorderFactory.createEmptyBorder());
set the hgap and vgap to zero on two diffrent layouts:
frame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
//frame.getContentPane().setLayout(new GridLayout(screenSize.height/15,screenSize.width/15,0,0));
and tried setting the gaps negative, on advice from another post on here. None of these seem to work, any suggestions?
Look at the all of settings of your GridLayout By default it has some margins and such. Though I might be wrong, it occurs to me this is Swing and not SWT.
Works fine for me when I use a JLabel. You don't even need to use an EmptyBorder.
Maybe the problem is that your images have a transparent border. Or maybe the problem is that you are using another component.
If you need more help than post your SSCCE that demonstrates the problem.

How to prevent MigLayout from exceeding a container's bounds

I'm trying to construct a simple status panel using MigLayout as follows:
setLayout(new MigLayout("fillx", "[][p]")); // removing constructor args makes no difference
add(createStatusLabel(), "span 2, wrap");
add(createProgressBar(), "growx, pushx");
add(createCancelButton(), "");
This works fine as long as the status message displayed by the status label is short enough to fit within the current panel's size (the cancel button remains right-justified, and the progress bar resizes to take up the remaining space). If the status message is too long, it is not cropped, and causes the area to exceed the bounds of the container, resulting in the cancel button being pushed off screen.
Any suggestions on how to prevent this from happening?
Thanks
Try setting the maximum width of the label to 100%.
You can do this by changing the layout for the label to "span 2, wrap, wmax 100%"
In my tests, I found that it still didn't look quite right, so you may want to subtract a little bit of length (something like wmax 100% - 10px) to bring it away from the edge.

Categories