Adding margins in a JTextArea? - java

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

Related

How do I refer to the distance I’ve scrolled in a JScrollPane?

I’m writing code for drawing in a JPanel as if it’s a canvas, where I can create shapes by clicking inside the panel. When I click, it places the dots where I want them to go, however if I scroll and then click, it places the dots where the mouse would be if I hadn’t scrolled. I assume this is because the mouse coordinates don’t change, so I’m guessing I need to add the number of pixels I’ve scrolled (horizontally and vertically). Is there a command I can use to refer to these values?
It's been a while since I've worked with Swing, but I believe you want to call:
JScrollPane sp = ...
Point viewPosition = sp.getViewport().getViewPosition();
The viewPosition.x and .y will return the offset in pixels from the top left corner.

How to vertically align two JSlider objects independent of tick labels

I am using several horizontally positioned JSlider objects in my Java application and would like to align their left and right track ends vertically. That means whenever two knobs are positioned on the left end of their track the upper knob should be exactly above the lower one without any displacement in horizontal direction.
The problem I currently have is that long labels at ticks on the left or right end of a track decrease the length of the track and do not allow a nice alignment. As an example, see the following image:
JSlider alignment problem example
Does anyone know how to vertically align the tracks of two horizontally positioned JSlider objects independent of their tick labels?
A solution for my problem might be to left-align/right-align labels at the end of the track (i.e. the label on the far left tick is left-aligned to its tick), but I did not find out how I could do that. Maybe there is also a better solution I did not think about.
Thanks, Sandreal
I do not think that the JSlider labels are accessible in such a way that you can edit their location, so instead you should create separate labels and position then yourself relative to the JSlider, then you can be sure that they will not effect the JSlider (depending on your layout manager).

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.

Vaadin incorrect alignment

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.

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?

Categories