Text Not Aligning To the Center of JLabel in Java - java

I have a JLabel on the bottom of my panel that gives text instructions to the user. Some of the text went off the screen because it was too long. To fix this, I added tags. However, now the text is no longer centered, it is now aligned to the left. Why is this case. Shouldn't this code center the text?
detailedInstructions.setText("<html><div align=center>" + test.getMicroSteps()[currMicroStep].getDescription() + "</div><html>");

Instead of using HTML code, simply call
detailedInstructions.setHorizontalAlignment(SwingConstants.CENTER);
Or use the JLabel constructor that sets the horizontal alignment.
Note that if the JLabel displays an ImageIcon, then you'll also want to set it's horizontal text position which determines the location of the label's text relative to its image.
Other potential issues is that the JLabel itself might not be centered at teh bottom of the GUI. To test this, put a border around the label to see it's actual placement, and if so, then you will need to work with the settings of the label's container's layout manager.

Related

Increase gap between icon and button java

I am doing a program in java in which you have to select a category with a button.
I'm trying to put an icon and text in the JButton, but I can't quite get the alignment right. I want a wider space to the left between the left side of the button and the icon.
I have read that you can create an invisible line border to solve this, but my JButton already has a border.
choose_Animals = new JButton ("ANIMALS");
choose_Animals.setIcon(categoriesIcon[0]);
choose_Animals.setHorizontalAlignment(SwingConstants.LEFT);
choose_Animals.setIconTextGap(20);
choose_Animals.setOpaque(false);
choose_Animals.setContentAreaFilled(false);
choose_Animals.setForeground(Color.BLACK);
choose_Animals.setBorder(border);
choose_Animals.setFocusable(false);
choose_Animals.setFont(p);
choose_Animals.setBounds(90, 220,470, 85);
choose_Animals.addActionListener(this);
this.add(choose_Animals);
Do you know if what I ask for is possible with a null layout?
Positioning the text/icon of a button has nothing to do with a layout manager. These are properties of the component itself.
And yes, you should be using a layout manager, not a null layout. The time to learn using layout managers is now, not some time in the future.
I have read that you can create an invisible line border to solve this,
You can use an EmptyBorder to give addition space to one of the 4 inset positions.
but my JButton already has a border.
You can use a CompoundBorder to combing your current Border with the EmptyBorder
Read the section from the Swing tutorial on How to use Borders for more information and examples.

Can't vertical center align text inside a JLabel

numBox is a square JLabel like the ones in the game 2048. The text where the number goes will not vertically center.
neither
numBox.setVerticalAlignment(JLabel.CENTER);
nor
numBox.setVerticalTextPosition(JLabel.CENTER);
are working.
Text shows up horizontally centered but at the top of the box that the label shows up in.
How do I get the text to show up in the middle of the JLabel?
You can give an alignment suggestion to the layout manager by using:
label.setAlignmentY(JLabel.CENTER_ALIGNMENT);
If this doesn't help then post a proper SSCCE that demonstrates the problem.

Moving JButton text over its icon by pixels

I am creating a desktop application in Java with lots of customized UI.
It also has a breadcrumb.
I've extended JButton class to customize it for my Breadcrumb buttons.
This is the screenshot of expected breadcrumb.
http://puu.sh/4MtvZ.jpg
The background of this breadcrumb is an ImageIcon.
But now, I am not able to perfectly align the JButton text over this background icon.
This is the screenshot of actual breadcrumb.
http://puu.sh/4MtDc.png
I've used following code to align the text.
setHorizontalTextPosition(CENTER);
setVerticalTextPosition(CENTER);
So, is their a way that I can move this text in pixels to its right position?
And also, I want root breadcrumb button "Schemes" to overlap some part of its preceding breadcrumb button "2000 Avenues" as shown in expected breadcrumb screenshot!
How can I achieve that?
Check out the Overlap Layout for one solution.
So, is their a way that I can move this text in pixels to its right position?
I don't understand this question. Why are you setting the alignment to center if you want it aligned to the left.
Edit:
I see. You are just using an Icon for the button outline and are then painting the text on top of the Icon. I thought your were using a custom shaped button with text). You can just use the default vertical/horizontal text position settings.
To shift the text to the left you can use:
button.setIconTextGap(10 - button.getIcon().getIconWidth());

Java set component location in GroupLayout

I'm trying to make a marquee like effect in my program, the text is determined by the content of a file which will result a dynamic lenght/width for JLabel, and the problems are:
I use drag n drop and GroupLayout since it will auto resize the component, but it doesn't allow me to use setBounds nor the setLocation method.
I tried to change it to null layout, yes I can use the setBounds or the setLocation, and now the problem is that the JLabel cannot auto resize its width to fit the text length.
My marquee text would goes from right to the left screen, and will do the run 2 times.
Any suggestion here? Thanks :)

Java Swing JLabel Text longer than bounds

I have a problem with Java Swing JLabel.
The text i want to display on the JLabel exceeds the bounds of the JLabel. I want to display it via a Marqueeeffect. I already implemented the effect but when there is a string that exceeds the bounds of the JLabel it gets cut off and the rest gets replaced with "...".
My question is, if there is any opportunity to set the textlength for a JLabel individually, not depending on the bounds, that it doesnt get cut off?
Hope somebody got an answer for me.
I dont use any LayoutManagers and i dont want the JLabel to get resized, it should only can contain text longer than the bounds of it.
I want to display it via a Marqueeeffect.
Check out the Marquee Panel.
In this LayoutTest, you can see how the label's UI delegate uses layoutCompoundLabel() to elide the text when label's size falls below the preferred size.
In this MarqueeTest, MarqueePanel has a default FlowLayout, which adopts the display label's preferred size.
The Swing JLabel was not designed to do marquee scrolling.
Here's the source code for JLabel. You can modify the text handling routines to do a marquee scroll rather than compressing the text with an ellipsis.
Oh, you'd better use a layout manager. Your marquee JLabel won't layout correctly without a layout manager.

Categories