Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating JButton with customized look

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

like image 285
Nikhil Avatar asked Jan 04 '13 14:01

Nikhil


People also ask

How do I change the style of a JButton?

First, set your look and feel to something cool, like 'metal'. Then do something like this: my_jbutton. setBackground(new Color(240,240,241);

How do I fit an image into a JButton?

Now, to use this method in your example code: JFrame frame2 = new JFrame("Tauler Joc"); JPanel panell = new JPanel(); ImageIcon icon = new ImageIcon("king. jpg"); JButton jb= new JButton(); jb. setBounds(200,200,700,700); panell.


5 Answers

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

like image 110
David Kroukamp Avatar answered Oct 05 '22 18:10

David Kroukamp


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());
like image 29
Russell Zahniser Avatar answered Oct 05 '22 19:10

Russell Zahniser


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"));
like image 31
Juvanis Avatar answered Oct 05 '22 18:10

Juvanis


On Oracle javadoc, you can see jbutton javadoc.

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

like image 22
Aktarel Avatar answered Oct 05 '22 17:10

Aktarel


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);
like image 44
Manjeet Rani Avatar answered Oct 05 '22 19:10

Manjeet Rani