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.
Related
My users found that if the last character in a JTextPane is a newline, the cursor is smaller. Debug statements showed the same cursor and the same font no matter where I clicked. I downloaded the Oracle demo for JTextPane and can see the same behaviour, so it appears to be out of the box behaviour.
Anyone know a way around this?
My users found that if the last character in a JTextPane is a newline, the cursor is smaller
The caret represents the height of the largest Font used on the line. Since there is no text a smaller caret is used. Is this really an issue to worry about?
I downloaded the Oracle demo for JTextPane and can see the same behaviour
Add the following line of code to the TextComponentDemo:
Rectangle caretCoords = textPane.modelToView(dot);
System.out.println(caretCoords); // added
You will see that the height changes. The height of the Rectangle is used by the DefaultCaret class to paint the caret.
I guess you could override the modelToView(...) method of JTextPane to return a minimum height based on the FontMetrics of the Font of the text pane. Not sure what other functionality of the text pane that might affect.
Or you could override the paint() method of the DefaultCaret to use a minimum height, again based on the FontMetrics.
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.
I have the following piece of code that I wrote. I am trying to center both the texts one after another in the panel like this:
Your name is:
1
Can anyone tell me how to go about doing this?
Thank you.
Code:
VerticalLayout layout1 = new VerticalLayout();
layout.setSizeFull();
layout1.setSizeFull();
Label label = new Label("Your name is: ");
label.setStyleName(Runo.LABEL_H1);
Label label1 = new Label(name);
label1.setStyleName(Runo.LABEL_H1);
layout1.addComponent(label);
layout1.addComponent(label1);
layout1.setComponentAlignment(label, Alignment.MIDDLE_CENTER);
layout1.setComponentAlignment(label1, Alignment.MIDDLE_CENTER);
layout.setComponentAlignment(panel, Alignment.MIDDLE_CENTER);
setContent(layout);
panel.setContent(layout1);
Output:
Labels in Vaadin have a default width of 100%, meaning the Label component fills the entire horizontal space, leaving no room for alignment. The reason for this default is that in most cases you'll want the text to wrap once it reaches the edge of the parent component.
To center the labels in your code, just set their widths to undefined by doing label.setWidth(null);
The reson for the extra vertical space is in the VerticalLayout. Since you've set an expicit height for it but haven't specified any expand ratios for the slots, it's making both of the slots equally high; half the height of the layout each. To get around that, you can do one of two things: get rid of the height in layout1 by replacing the call to layout1.setSizeFull(); with layout1.setWidth("100%");—or set an expand ratio for the second slot by doing layout1.setExpandRatio(label1, 1);. Setting the expand ratio will cause the layout to use all the extra space in the second slot, leaving the two labels closer together.
With setExpandRatio you'll of course also have to change the alignment of label1 from MIDDLE_CENTER to TOP_CENTER, so that the empty space is placed below the label and not evenly around it.
What is the best way to display text diagonally and vertically in a JApplet. I want to show the string variable.
Example of a diagonal format can be found here.
Example of a vertical format:
V
E
R
T
I
C
A
L
There are components that do this naturally.
You could try using a JTextArea. Just add a new line after every character for vertical. Then you can add multiple spaces before every line for diagonal. Actually text area support letter wrapping so you could use a monospaced font and create the textarea with a single column and it should do the vertical for you.
You could use a Text Icon for vertical. Then maybe customize the code for diagonal to add a gap for every letter.
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