javaswinguser-interfaceawt

How to put padding around JtextArea


I was wondering how does one put spacing and set the size of a JTextArea component in Java.

eg

//I've tried
        questInfo = new JTextArea(20,30);
        questInfo.setMargin(new Insets(10,10,10,10));

When I preview the gui, It just shows JTextArea filling in the pane from top to bottom and left to right, no spacing in between the pane


Solution

  • Apparently, this happens due to UI overriding the margin. Please refer to this question. You might resolve it using:

    questInfo.setBorder(BorderFactory.createCompoundBorder(
            questInfo.getBorder(), 
            BorderFactory.createEmptyBorder(5, 5, 5, 5)));