Search code examples
javaswinguser-interfacejframejtextpane

Remove white outline JFrame


I have JFrame with a JTextPane in it.

Kind of a very simple text editor.

Now the problem is there is a very thin white border around the frame.

This is ok if the color of the text pane is white but if i make the text pane dark colored then the white outline looks very bad.

Here is my code :

import javax.swing.*;
import java.awt.*;
public class Try{
    public static void main(String[] args) {
        JFrame frame = new JFrame("Try");
        frame.setLayout(new GridLayout(1, 1, 0 ,0 ));
        JTextPane tpane = new JTextPane();
        tpane.setBackground(Color.BLACK);
        tpane.setForeground(Color.WHITE);
        JScrollPane scp = new JScrollPane(tpane);
        frame.add(scp);
        frame.setSize(900, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

I tried setUndecorated but that removes the title bar not the outline.

This is the white outline i am talking about :

Screenshot

Can anyone help regarding this?


Solution

  • Here this is caused due to the default border of the scroll pane. We can get rid of it by setting an empty border to it.

    This is the updated code :

    import javax.swing.*;
    import java.awt.*;
    public class Try{
        public static void main(String[] args) {
            JFrame frame = new JFrame("Try");
            frame.setLayout(new GridLayout(1, 1, 0 ,0 ));
            JTextPane tpane = new JTextPane();
            tpane.setBackground(Color.BLACK);
            tpane.setForeground(Color.WHITE);
            JScrollPane scp = new JScrollPane(tpane);
            scp.setBorder(BorderFactory.createEmptyBorder());
            frame.add(scp);
            frame.setSize(900, 500);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }