Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java JPanel two borders, different colors?

Tags:

java

border

swing

For my layout i want to have double borders, on the bottom of my JPanel, one should be slightly darker and the other should be slightly lighter.

Currently i 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 could i add another border underneath the one that i already have? Is this possible?

like image 832
Alosyius Avatar asked Jul 25 '13 06:07

Alosyius


1 Answers

Use a CompoundBorder

ie...

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

like image 50
MadProgrammer Avatar answered Nov 18 '22 16:11

MadProgrammer