Getting pixels between border of TextArea and text inside - java

I wish to obtain and modify the number of pixels between the text contained inside the text area and the border of the text area. To provide a more descriptive visual:
where the length of the blue line is what I desired. When I received both the padding and margins of the text area in my application, I got 0px. I assume that this is the padding/margin of the TextArea relative to the outside, not the inside relative to the Ttext area.
Many thanks.

In java to set the insets of a textarea you can use setMargin().
public void setMargin(Insets m)
Sets margin space between the text
component's border and its text. The text component's default Border
object will use this value to create the proper margin. However, if a
non-default border is set on the text component, it is that Border
object's responsibility to create the appropriate margin space (else
this property will effectively be ignored). This causes a redraw of
the component. A PropertyChange event ("margin") is sent to all
listeners.
Parameters: m - the space between the border and the text
for example:
JTextArea txtArea = new JTextArea("Hello world!");
txtArea.setMargin( new Insets(15,15,15,15) );
more on insets and setMargin().
Or another approach would be to add a compound border and then setting the insets on it like this:
JTextArea txtArea = new JTextArea("Hello world!");
Border border = BorderFactory.createLineBorder(Color.RED);
txtArea.setBorder(BorderFactory.createCompoundBorder(
border, BorderFactory.createEmptyBorder(15, 15, 15, 15))
);
see this answer.

Related

JTextField customization issue

I have made a frame and put a button and a text field in it.
I am using the setMargin method to set a margin between the caret and the border of the text field and works perfectly fine until I add a border to it.
After adding the border the setMargin call method does not work.
Could you please help me understand the origin of the issue and find an alternative of having both a border and a certain margin?
public class main extends JFrame {
public static void main(String[]args){
JTextField textfield0;
JButton button0;
Border border7=BorderFactory.createDashedBorder(new Color(0xA524FF), 2, 5, 4, true);
Border border8=BorderFactory.createCompoundBorder();
Border border01=BorderFactory.createLineBorder(Color.RED);
Border border02=BorderFactory.createLineBorder(Color.YELLOW);
Border border9=BorderFactory.createCompoundBorder(border01, border02);
textfield0=new JTextField();
textfield0.setPreferredSize(new Dimension(300,30));
textfield0.setFont(new Font("Consolas",Font.BOLD,15));
textfield0.setCaretColor(Color.RED);
textfield0.setBackground(Color.CYAN);
textfield0.setForeground(Color.MAGENTA);
textfield0.setText("name");
//textfield0.setBorder(border9);
textfield0.setSelectedTextColor(Color.YELLOW);
textfield0.setMargin(new Insets(0,7,0,5));
textfield0.setCaretPosition(0);
textfield0.setSelectionColor(Color.PINK);
button0=new JButton();
button0.setText("submit");
button0.setPreferredSize(new Dimension(100,30));
button0.setFocusable(false);
button0.setBackground(textfield0.getBackground());
button0.setFont(textfield0.getFont());
button0.setBorder(textfield0.getBorder());
JFrame frame00=new JFrame();
frame00.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame00.setLayout(new FlowLayout());
frame00.add(button0);
frame00.add(textfield0);
frame00.pack();
frame00.setVisible(true);
}
}
We can find an answer for this in the JavaDoc which requires a few extra steps:
Sets margin space between the text component's border and its text.
The text component's default Border object will use this value to
create the proper margin. However, if a non-default border is set on
the text component, it is that Border object's responsibility to
create the appropriate margin space (else this property will
effectively be ignored).
Source: https://docs.oracle.com/javase/8/docs/api/javax/swing/text/JTextComponent.html#setMargin-java.awt.Insets-
And to do the above we can do one of two things, we can use a compound border as shown here: Java Swing - setting margins on TextArea with Line Border or we can take the following steps:
Overriding both AbstractBorder.getBorderInsets(Component c) and
AbstractBorder.getBorderInsets(Component c, Insets insets) to
provide the correct insets
Source: https://docs.oracle.com/javase/tutorial/uiswing/components/border.html#custom
I strongly recommend reading through the above link to get a better understanding of borders and how you can work with them.
If you read the javadoc for Border, it states: "Use EmptyBorder to create a plain border (this mechanism replaces its predecessor, setInsets)"
So you can either use a CompoundBorder with a PlainBorder and the one you are setting, or override getBorderInsets(Component c) on your existing Border

JTextPane scroll to specific row

I am trying to scroll JTextPane to specific line. For doing that, i put my text pane into the JScrollPane as JViewport. I determine the x and y coordinates of the line with :
JViewport port = (JViewport)getParent();
JScrollPane pane = (JScrollPane)port.getParent();
float calculated = (float)i/(float)lines.size() * 100;
pane.getVerticalScrollBar().setMaximum(100);
pane.getVerticalScrollBar().setValue((int)calculated);
With this code block, i can scroll the scrollbar but i can not scroll the context of text area. Only scrollbar moves, but displaying text seems the same as before. I need to display the line which is calculated.(By the way calculated variable is between 0-100)
Check out the Text Utilities class.
You can use the gotoStartOfLine(...) method to position the caret at the beginning of a line. This will cause the text area to scroll if required.
Or maybe you can use the centerLineInScrollPane(...) method.

Java how to preserve newline from String to JLabel?

JLabel newLabel = new JLabel();
String a = b;//b is String from database.
newLabel.setText(a);
I need to generate text pulled from my database which contains multiple line, but when i put them into the label, all of the text became same line.
I tried using JTextArea and it works, however, for some reason it messed with all the other component's alignment in a 1 column BoxLayout panel... which I wanted all the content to be aligned to the left. Any help would be much appreciated! thanks
however, for some reason it messed with all the other component's alignment in a 1 column BoxLayout panel
The is related to the setAlignmentX(...) method of each component.
A JLabel uses 0.0f (for left alignment). A JTextArea and JScrollPane use 0.5f (for center alignment).
Using components with different alignments will cause alignment issues when using a BoxLayout.
The solution is to change the alignment of the text area or scroll pane (whichever component you add to the BoxLayout).
So the basic code would be:
JLabel label = new JLabel(...);
JTextArea textArea = new JTextArea(...);
textArea.setAlignmentX(0.0f);
JPanel panel = new BoxLayout(...);
panel.add(label);
panel.add(textArea);
Now both components will be left aligned.

JScrollPane: Blinker(cursor) is covered by the border

I am working on my own little text editor. Files can be edited in a JScrollPane, now if a line of text is longer than the window, you can scroll to the right as it is ment to be. But when the Blinker(or whatever it's called) is at the very end of the line, it's not visible because it seems to be covered by the border.
//the JTextArea is inside the JScrollPane of course
Border scrollPaneBorder = new LineBorder(interfaceColor, 8, true);
Border textAreaBorder = new LineBorder(backgroundColor, 4, true);
Setting both borders to 0 won't change anything. Has anyone got a way to deal with this problem?
The most elegant solution in my opinion is the one in Notepad++. There it somehow puts some space between the text and the border as soon as you get close to the border. But I don't know how/if this is possible in java.
Whatever solution, thanks for the help guys.
From the documentation of setMargin:
Sets margin space between the text component's border and its text. The text component's default Border object will use this value to create the proper margin. However, if a non-default border is set on the text component, it is that Border object's responsibility to create the appropriate margin space (else this property will effectively be ignored).
It’s a bit strange that Swing’s text component will move the cursor up to a location that is covered by the painted Border if there is no margin, but it explains why the problem occurs when you set a custom Border but not with the default border as the default value for the margin property is non-zero.
You can create a custom border that reads the margin property and implements an unpainted inner region of that size or, if you don’t need support for different values for the margin property, you can just combine your border with an empty border to get a similar effect (of a hardcoded margin space):
Border textAreaBorder=BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(backgroundColor, 4, true),
BorderFactory.createEmptyBorder(2, 2, 2, 2)
);

Removing the added border from JTextField and bringing it back to normal

Please have a look at the below code
Border border = BorderFactory.createLineBorder(Color.RED, 1);
introducerFeesTxt.setBorder(border);
I used this code to create a line border for the JTextField. However now I need to remove it and replace it's normal view. Below is what I tried.
introducerFeesTxt.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
That code above again created a border which is not similar to other normal JTextFields. Below is a screenshot.
You can clearly see the differnece between the "Normal" JTextField and the JTextField with the added border. How can I reset it to be "normal" ?
You could keep the original border in a variable before change it and then use this border to set it back to its original state:
Border originalBorder;
...
JTextField textField = new JTextField(20);
originalBorder = textField.getBorder();
// here you can safely change text field's border
Of course the scope of this originalBorder variable should be wide enough to use it when needed (f.e.: class member).
Note: Please note this approach is independent of the PLAF used by your application.
You should use the original border from the L&F (Look and Feel).
Border border = BorderFactory.createLineBorder(Color.RED, 1);
introducerFeesTxt.setBorder(border);
// some operation
introducerFeesTxt.setBorder(UIManager.getBorder("TextField.border"));

Categories

Resources