8

enter image description here

I am pretty new to java swing and not familiar with paint(). I want to create a button in java swing with above look. Can anyone help me to do this. Any guidance would be grateful. Thanks in advance

Nikhil
  • 2,857
  • 9
  • 34
  • 58
  • 2
    What is different in this `JButton`, except for the `Background and Foreground Colour` ? To me it's the same :-) Why you can not use `button.setBackground(Color.BLUE.darker())` and `button.setForeground(Color.LIGHT_GRAY.brighter())`, do change the font of the JButton, if you want more effects ? – nIcE cOw Jan 04 '13 at 15:10
  • Yes. I want my button to put a flatterd look like on the facebook. avoiding the gradient. can you please help? – Nikhil Jan 04 '13 at 15:17
  • Try this instead, very close to your liking : `button.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createBevelBorder( BevelBorder.RAISED, Color.BLUE.darker(), Color.BLACK), BorderFactory.createEtchedBorder(EtchedBorder.LOWERED))); button.setFont(new Font("Arial", Font.BOLD, 14));` – nIcE cOw Jan 04 '13 at 15:52

5 Answers5

16

I googled the Facebook blue RGB: 59, 89, 182/Hex Code is #3B5998 and Font family: Tahoma.

using that here is what I got with a few calls like setFocusPainted(false),setBackground(new Color(59, 89, 182)) and setFont(new Font("Tahoma", Font.BOLD, 12)):

enter image description here

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Test {

    public Test() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());
        JButton b = new JButton("Log In");//http://www.chacha.com/question/what-are-the-rgb-values-for-the-background-color-of-comments-on-facebook
        b.setBackground(new Color(59, 89, 182));
        b.setForeground(Color.WHITE);
        b.setFocusPainted(false);
        b.setFont(new Font("Tahoma", Font.BOLD, 12));//http://answers.yahoo.com/question/index?qid=20070906133202AAOvnIP
        frame.add(b);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test();
            }
        });
    }
}

unless you are looking for identical (which IMO this is about as best as it gets without using actual image)... than setting the image of the button would be the best way

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
7

If you want to completely override the look of your button, the most general solution is to create your own ButtonUI:

class MyButton extends BasicButtonUI {
    @Override
    public void paint(Graphics g, JComponent c) {
        AbstractButton b = (AbstractButton) c;
        ButtonModel model = b.getModel();
        ...
    }
}

You can then paint whatever you want, taking into account the state of your button (rollover, focused, armed, pressed, etc). Take a look at the superclass implementation for basic ideas on how to do this.

Then just set the UI of the button you want to change:

button.setUI(new MyButton());
Russell Zahniser
  • 16,188
  • 39
  • 30
2

To create a customized button like your example, I think the best way is preparing a graphics document (image etc.) and then setting it as a property of your button:

JButton button = new JButton();
button.setIcon(new ImageIcon("yourButtonImage.jpg"));
Juvanis
  • 25,802
  • 5
  • 69
  • 87
2

On Oracle javadoc, you can see jbutton javadoc.

Jbutton java method setIcon(Icon) with ImageIcon implementation will do the trick !

the swine
  • 10,713
  • 7
  • 58
  • 100
Aktarel
  • 33
  • 1
  • 7
1

To create a customized button shown in your example, I think use the following code:-

JButton button = new JButton("Log In");
button.setFont(new Font("Serif",Font.BOLD,20));
button.setBackground(new Color(0,51,204));//import java.awt.Color;
button.setForeground(Color.WHITE);
button.setFocusPainted(false);
button.setBorderPainted(false);
Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265
Manjeet Rani
  • 31
  • 1
  • 7