Java JPanel two borders, different colors?

For my layout, I want to have double borders, the bottom of my JPanel should be a little darker, and the other should be a bit lighter.

I currently have 1 border:

JPanel pnlTop = new JPanel(new BorderLayout()) { protected void paintComponent(Graphics grphcs) { super.paintComponent(grphcs); Graphics2D g2d = (Graphics2D) grphcs; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); GradientPaint gp = new GradientPaint(0, 0, new java.awt.Color(237, 237, 237), 0, getHeight(), new java.awt.Color(222, 222, 222)); g2d.setPaint(gp); g2d.fillRect(0, 0, getWidth(), getHeight()); } }; pnlTop.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new java.awt.Color(173, 173, 166))); pnlTop.setPreferredSize(new Dimension(2000, 90)); add(pnlTop, BorderLayout.PAGE_START); 

How can I add another border under the one I already have? Is it possible?

+7
java swing border
source share
1 answer

Use CompoundBorder

e ...

 pnlTop.setBorder(new CompoundBorder( BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLUE), BorderFactory.createMatteBorder(0, 0, 1, 0, Color.RED)); 

See How to use borders for more details.

+15
source share

All Articles