Java: How to draw a border around an unecorated JFrame?

I currently have a 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 am 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 use the root panel of the frame to draw the border, because the frame does not support the borders themselves.

The problem is that the borders are drawn inside the component itself, as you can see in the figure below, the shaded border is drawn inside, against the borders of the frame itself:

enter image description here

Note. A (shaded) frame is drawn inside the frame with its borders, not outside the frame.

It doesn't matter which border is used, they are all drawn inside the JFrame itself.

My question is: Can I draw any frame around the frame, and not just inside the borders of the frame?


One of the ways that can be used to solve this problem is to create another transparent transparent full-screen window with a normal window on top of it. This full-screen window is used to draw the shadow, so the shadow does not need to be drawn in the frame itself. This is a solution to get a similar result, it is not what I would like. I want to draw a border outside the frame. Such solutions usually cause other problems.

+7
java swing border jframe shadow
source share
3 answers

Yes, you can draw borders around a loose JFrame. Just load the root panel of the JFrame and set its borders using the setBorder method (border border).

 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

+13
source share

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

Undecorated JFrame shadow

+1
source share

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

Some Java looks and feel can provide a custom title bar, icons, and window border. If the appearance supports this function, the getSupportsWindowDecorations () method will return true.

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

0
source share

All Articles