How to align Labels and Texts to Left - java

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.

Related

Having Multiple Alignments In Same TextArea

I am making Calculator app.
I want digits (Number)to be written on the right side of textArea and signs(+,-,/,*) on the left side of same textArea.
How to do this?
I do not think you can have multiple alignments inside textarea, but instead you can use JTextPane here is an exemple Multiple Alignement or create your own calculator display :
1-create a container (BorderLayout for exemple)
2-add two JLabels to the container to display text dynamically
3-make the first JLabel with right Alignement setHorizontalAlignment(SwingConstants.LEFT)
4-make the second JLabel with left Alignement
setHorizontalAlignment(SwingConstants.RIGHT)
I hope this is what you are looking for, after all this is just a suggestion !

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

Swing: choice of layout and components for simple app

I'm working on a simple app that has:
2 TextFields with Labels: name and description
Add button
Panel that is filled with name/description labels and with radio buttons to select records and manipulate with them (edit / delete -> these buttons appear when radio button is selected).
I tried the following layouts composition:
2 Panels with BoxLayout (YAXIS) which contain text field and their labels
Panel with GridBagLayout to have 2 panels with text fields and a button to add data.
Panel with BoxLayout (YAXIS) to be filled with records after button press
Panel with GridBagLayout that contains radio button and 2 labels with results of name and description text fields.
Here is a screenshot of what I came to:
As you can see there's a problem - Labels have no word wrap. If I use JTextArea for word wraping, then white background appears (setBackground(null) doesn't help).
I think that GridBagLayout is not good choice here or even there are too many inner panels. In fact it seems this task is common enough. What are good practices for apps UI building like that one?
The Problem you got with your JLabel is easy to handle. Because JLabels accept HTML Tags you can use this mechanic to automaticly wrap your text:
label.SetText(String.format("<html><div WIDTH=%d>%s</div><html>", width, text));
For the general Layout I prefer the GridbagLayout. With the GridbagLayout you only need on Layout-Typ. GridBagLayout is flexible and easy to use for your case.
If the Layout will get more complex you will probably need to stack different Layouts, to get the best handling.

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/

Categories