-1

How can I create a JButton like this with inner shadow in Swing?

enter image description here

I wan to create JButton with different Color of border, like top and left border color should be black and right and bottom border color should be of white color.

But all together, I want inner shadow of dark gray color in top and left side like above image.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 2
    Is this a "joke question" or something? Because [link](http://stackoverflow.com/a/32154944/4857909). – Lukas Rotter Aug 22 '15 at 15:22
  • I've deleted my answer as it seems like you answered the question yourself... – Lukas Rotter Aug 22 '15 at 15:27
  • 1
    @LuxxMiner: Please reconsider; see also this related [example](http://stackoverflow.com/a/5755124/230513). – trashgod Aug 22 '15 at 23:14
  • @trashgod I don't quite see why. His answer is basically better than mine, as the given example exactly produces the JButton showen in the picture. My answer was intended to give him a "starting point" - At which he can customize the button further. But if you think my answer would be useful for "general interest" (When other people click on this question and want to know the answer), sure. I'll undelete it. – Lukas Rotter Aug 22 '15 at 23:24
  • @LuxxMiner: Among the many ways to decorate a Swing button, your answer includes a [complete example](http://stackoverflow.com/help/mcve) that directly addresses the issue, while his was an unhelpful link. His answers are generally improving, and I wish to encourage you both. – trashgod Aug 23 '15 at 12:50

1 Answers1

4

First I thought you can just achieve this with a simple BevelBorder, but unfortunately you can't set the border's thickness comfortably... So I had to basically make a customized Border. You can customize it more if you don't like the style of my button in the paintBorder method, but you have to know how to work with Graphics. Here is what I've got:

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;

/**
 *
 */
public class MyBorder implements Border {

    private int thickness_ = 4;
    private Color white = Color.WHITE;
    private Color gray = Color.GRAY;
    private Color black = Color.BLACK;

    public static void main(String[] args) {

        JFrame frm = new JFrame("Border Test");
        frm.setLayout(new FlowLayout());
        JButton btn = new JButton("Button");

        MyBorder border = new MyBorder();

        btn.setBorder(border);
        btn.setFocusPainted(false);
        btn.setPreferredSize(new Dimension(60,30));
        btn.setBackground(Color.LIGHT_GRAY);

        frm.add(btn);
        frm.setSize(200,200);
        frm.setVisible(true);

    }

    public void paintBorder(Component c, Graphics g, int x, int y, int width,
            int height) {
        Color oldColor = g.getColor();
        int i;

        for (i = 0; i < thickness_; i++) {
            g.setColor(white);
            g.drawRect(x + i, y + i, width - i - i - 1, height - i - i - 1); //White Rectangle
        }
        for (i = 0; i < thickness_/2; i++) {
            g.setColor(black);
            g.drawLine(x + i, y + i, (width - x) - (i * 2), y + i); //Top Outer Edge
            g.drawLine(x + i, y + i, x + i, (height - y) - (i * 2));  //Left Outer Edge
        }
        for (i = thickness_/2; i < thickness_; i++) {
            g.setColor(gray);
            g.drawLine(x + i, y + i, (width - x) - (i * 2), y + i); //Top Inner Edge
            g.drawLine(x + i, y + i, x + i, (height - y) - (i * 2)); //Left Inner Edge

        }
        g.setColor(oldColor);
    }

    public int getThickness() {
        return thickness_;
    }

    public void setThickness(int i) {
        thickness_ = i;
    }

    public boolean isBorderOpaque() {
        return true;
    }

    public Insets getBorderInsets(Component c) {
        return new Insets(thickness_, thickness_, thickness_, thickness_);
    }

}
Lukas Rotter
  • 4,158
  • 1
  • 15
  • 35