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:
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.
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();
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With