10

I currently have the DropShadowBorder class (which extends the javax.swing.border.Border class) from the SwingX library, so this is an instance of a regular Border. I would like to draw this border around my undecorated JFrame. I'm currently using the following method inside my JFrame to set the border:

DropShadowBorder b = new DropShadowBorder(Color.BLACK, 0, 10, 0.2f, 10, true, true, true, true);
this.getRootPane().setBorder(b);

Note: I'm using the root pane of the frame to draw the border on, because the frame doesn't support borders itself.

The problem is that the borders are drawn inside the component itself, as you can see in the picture bellow, the shadowed border is drawn inside, against the bounds of the frame itself:

enter image description here

Note: The (shadowed) border is drawn inside the frame against it's bounds, instead of outside the frame.

It doesn't matter what kind of border is used, all of them are drawn inside the JFrame itself.

My question is: Is it possible to draw any border around the frame, not just inside against the bounds of the frame?


One way that could be used to solve this problem, is to create a different undecorated full screen window which is transparent, the normal window is put on top of this one. This full screen window is used to draw the shadow on, so the shadow doesn't need to be drawn in the frame itself. This is a solution to get a similar result, this is not what I would like though. I want to draw a border outside the frame. These kind of solutions usually cause other problems.

Tim Visée
  • 2,988
  • 4
  • 45
  • 55
  • 1
    `this.getContentPane().setBorder(b);`, but better is there put `JPanel` with `Borders` – mKorbel Nov 23 '13 at 19:18
  • `Is it possible to draw any border around the frame itself, no around the root pane of the frame?` - No. What problem are you attempting to solve? – camickr Nov 23 '13 at 20:48
  • I would like to add a shadow to my undecorated `JFrame`. I currently have a border that creates the shadow as you can see on the picture in my question. The problem is with these borders that it's drawn inside the component the border was set on. On that image for example, the border is drawn on the inside of the bounds of the frame. Is there a way to draw this shadow border on the outside of the frame? Note: I've updated the question to make everything just a bit more clear. – Tim Visée Nov 24 '13 at 00:27
  • possible duplicate of [Undecorated JFrame shadow](http://stackoverflow.com/questions/19105242/undecorated-jframe-shadow) – tucuxi Nov 24 '13 at 00:42

3 Answers3

22

Yes you can draw the borders around the undecorated JFrame. Just simply get the root pane of the JFrame and set its borders by setBorder(Border border) method.

getRootPane().setBorder(BorderFactory.createMatteBorder(4, 4, 4, 4, Color.RED));

For example:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Borders2UndecoFrame extends JFrame{
    JLabel label = new JLabel("Welcome!", JLabel.CENTER);
    public Borders2UndecoFrame(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(new Dimension(200, 200));
        add(label, BorderLayout.CENTER);
        setUndecorated(true);
        getRootPane().setBorder(BorderFactory.createMatteBorder(4, 4, 4, 4, Color.RED));
        setVisible(true);

    }
    public static void main(String[] args) {
        new Borders2UndecoFrame();
    }

} 

enter image description here

Hanzallah Afgan
  • 716
  • 6
  • 23
  • As far as I know, this will only shrink down the root pane so it can draw the border inside the frame itself. This might be perfect for some situations, but for shadows on the other hand, it doesn't seem to be working very well. Check out the image I placed in the question, this example shows what I'm talking about. Thanks for the answer and code sample anyway! – Tim Visée Sep 09 '14 at 07:15
  • Yes really it will shrink the rootPane but increase the size of the frame by adding the size of border. Moreover your question is not about shadow but is about borders. Thanks – Hanzallah Afgan Sep 09 '14 at 10:00
  • I have to say that you're right about that one. I did notice in my question that I would like to draw the border around the frame though, this answer draws he border around the root-pane but still inside the frame. So this doesn't really solve the issue I'm afraid. – Tim Visée Sep 09 '14 at 14:09
1

JFrames and JDialogs are the only (swing) windows that need to interact with the external windowing system. To get external shadows, you need external context. See this answer on how to get it:

Undecorated JFrame shadow

Community
  • 1
  • 1
tucuxi
  • 17,561
  • 2
  • 43
  • 74
  • Is it required to use serval layers? In that example, a new content pane is created to put all the frame contents in. The shadow is drawn on the frame itself. Is it required to use this method, the problem is that this could cause window aligning and full screen problems very easily. – Tim Visée Nov 24 '13 at 00:56
0

From http://nadeausoftware.com/articles/2009/02/windows_java_tip_how_control_window_decorations

Some Java look and feels can provide their own title bar, icons, and window border. If a look and feel supports this feature, its getSupportsWindowDecorations( ) method will return true.

So you need to implement your own Java Look & Feel.

Robin Green
  • 32,079
  • 16
  • 104
  • 187
  • Nice article, very useful, but this doesn't show me how to draw a shadow, or any other border around a `JFrame`. – Tim Visée Nov 24 '13 at 00:24