0

Does anyone know whether its possible to change the colour of the empty space provided by the BorderFactory.createEmptyBorder() method. By default it always sets it to white - i want to set it to whatever colour the background of my JFrame is (i.e. grey).

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
maloney
  • 1,633
  • 3
  • 26
  • 49

4 Answers4

3

You want an empty border that has a color? The point of an empty border is that it takes up no space. Something with no space can't have a color. Did you want a line border instead?

see BorderFactory#createLineBorder(Color color)

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Slartibartfast
  • 1,605
  • 2
  • 16
  • 23
  • 1
    Empty border can have insets greater than 0. The purpose of empty border is to not paint anything! – Guillaume Polet Jan 15 '13 at 16:37
  • The reason i ask this is that for some reason my cursor disappears from the right side of the text pane if I have anything over than an emptyBorder. Please see my previous question here...http://stackoverflow.com/questions/14340851/right-aligned-cursor-not-showing-through-in-a-jtextpane-component#comment19935043_14340851 – maloney Jan 15 '13 at 16:38
  • 1
    I suppose my question should be why does the cursor show up when i have an empty border but not when i use any of the other border types? – maloney Jan 15 '13 at 16:40
  • @maloney: See the answer to your previous question. – Gilbert Le Blanc Jan 15 '13 at 19:36
  • 1
    -1 - as @GuillaumePolet already noted, the answer is ... wrong ;-) Wondering: how could anybody (or three bodies) have upvoted this? – kleopatra Jan 16 '13 at 11:01
2

Apply the empty border & color (or 'not' for plain) to a panel added to the content pane.

Blue Colored EmptyBorder Plain Colored EmptyBorder

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class PlainColoredEmptyBorder {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                // the GUI as seen by the user (without frame)
                JPanel gui = new JPanel(new BorderLayout());
                gui.setBorder(new EmptyBorder(20, 30, 20, 30));

                JTree tree = new JTree();
                tree.setVisibleRowCount(4);
                gui.add(new JScrollPane(tree));
                gui.setBackground(Color.BLUE);

                JFrame f = new JFrame("Demo");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Using this method on a JTextField still produces a white space around the box. Is there any way of doing this and still having the cursor show up when clicked to the right most point? – maloney Jan 16 '13 at 09:11
  • I have, please see: http://stackoverflow.com/questions/14340851/right-aligned-cursor-not-showing-through-in-a-jtextpane-component#comment19935043_14340851 – maloney Jan 16 '13 at 09:53
1

Why does the cursor show up when i have an empty border but not when i use any of the other border types?

Because an empty border is the only Border that does not paint. Any other type of Border will paint over the text pane cursor.

Putting your text pane inside of a JPanel with a background color doesn't work either because your text pane will be painted after the parent JPanel.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
0

Setting the background colour of the text field and setting it to opaque fixes this

maloney
  • 1,633
  • 3
  • 26
  • 49