Java Swing - setting margins on TextArea with Line Border
Asked Answered
T

1

6

As the title says, I am simply trying to set the margins (provide some padding) on a TextArea with a LineBorder set. Without setting the Border, .setMargins works fine. Here is the specific chunk of code.

aboutArea = new JTextArea("program info etc.....");

Border border = BorderFactory.createLineBorder(Color.BLACK);

aboutArea.setSize(400, 200);
aboutArea.setBorder(border);
aboutArea.setEditable(false);
aboutArea.setFont(new Font("Verdana", Font.BOLD, 12));

add(aboutArea);

I have tried each of these:

aboutArea.setMargins(10,10,10,10);
.getBorders(aboutArea).set(10,10,10,10);
UIManager.put("aboutArea.margin", new Insets(10, 10, 10, 10));

but nothing affects the margins after I apply the border, the padding is always 0. Any ideas how to set the padding on the textArea with the border?

Treblinka answered 23/4, 2012 at 4:20 Comment(1)
The key for the JTextArea margin is "TextArea.margin".Garrard
G
27

What if you try adding a CompoundBorder , won't this do, this will give you almost same thing

JTextArea tarea = new JTextArea("program info etc.");
Border border = BorderFactory.createLineBorder(Color.BLACK);
tarea.setBorder(BorderFactory.createCompoundBorder(border, 
            BorderFactory.createEmptyBorder(10, 10, 10, 10)));

CHECK THE MIDDLE JTextArea as OUTPUT

Gabrielagabriele answered 23/4, 2012 at 5:26 Comment(5)
@Treblinka everything depends if JTextArea is or isn't placed inside JScrollPane +1Schoof
+1, but (tilts head) as to the screenshot, why not put the GUI in front of a plain white BG? E.G. Ctrl-t While I am in FF and a get a white space of over half screen size. For further tips, see How do I create screenshots?Lovell
@Schoof You reminded me that I usually add the component to a JPanel then set the border to the panel. It works flawlessly for scroll-panes, buttons, labels..Lovell
I try to place my open window on any white space of stackoverflow page, sometimes I forget to remove my console from beneath my top window, seems like that's what is wrong with my image ?Gabrielagabriele
@nIcE cOw hehehe sometimes is better to play "Fail to React"Schoof

© 2022 - 2024 — McMap. All rights reserved.