Can't vertical center align text inside a JLabel - java

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.

Related

Text Not Aligning To the Center of JLabel in 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.

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());

JLabel positioned on top of TextPane, disappears when text pane repainting

I have positioned a Jlabel on top of a text pane, which acts like a top border to the textpane. My problem is when typing on the textpane, the label get disappeared. Please give me a way to keep the jlabel visible all the time.
Thanks in advance.
---------Edit---------
Giving a code sample would be lengthy. Please refer the images below to get an idea about what happens.
1) before typing in textPane.
2) after typing.

can I rotate the button or label into vertically in java?

I want button in vertically, for that i can extend the height and shrink the width of that button. But the text of the lable of buttons are in the form of horizontal only. For that can i rotate the whole button vertically and is it possible?
Rotated Icon shows how the text and icon can be rotated on the button (or any component that uses icons.
The easiest way to do this would be to pre-rotate the labels and just have it as an image on the button.
You might also want to take a look at this solution given in sun's forum for rotating a JButton.
You should be able to do this with scene graph: https://scenegraph.dev.java.net/

How to align Labels and Texts to Left

I am using jface Dialog for displaying some labels and texts. but i am getting all those displayed in the center, which i want to have as left aligned. so how to do that.
Thanks.
In Text and Label demo you find an example how to setup the GridLayout.

Categories