Your borders work fine - but it's just that the empty frame turns blue because you draw the entire component and the border is part of that component. For instance:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class Sandbox {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
WideComponent wc = new WideComponent();
JButton button = new JButton("Test");
wc.setBorder(createBorder());
button.setBorder(createBorder());
panel.add(wc);
panel.add(button);
frame.add(panel);
frame.setVisible(true);
frame.pack();
}
private static Border createBorder() {
Border innerBorder = BorderFactory.createLineBorder(Color.red);
int eb = 50;
Border outerBorder = BorderFactory.createEmptyBorder(eb, eb, eb, eb);
Border comboBorder = BorderFactory.createCompoundBorder(outerBorder,
innerBorder);
return comboBorder;
}
}
class WideComponent extends JComponent {
final int WIDTH = 500;
final int HEIGHT = 150;
public WideComponent() {
setOpaque(true);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillRect(0, 0, WIDTH, HEIGHT);
}
public Dimension getPreferredSize() {
return new Dimension(WIDTH, HEIGHT);
}
public Dimension getMaximumSize() {
return getPreferredSize();
}
public Dimension getMinimumSize() {
return getPreferredSize();
}
}
If you need an empty frame around your WideComponent, then paste it inside the JPanel and give the border to the JPanel socket. For example, the above code can be changed with the addition of:
JPanel nestingJPanel = new JPanel();
nestingJPanel.add(new WideComponent());
nestingJPanel.setBorder(createBorder());
panel.add(wc);
panel.add(button);
panel.add(nestingJPanel);