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
-
2What 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 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))
:
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

- 36,155
- 13
- 81
- 138
-
-
@nik glad it helped, BTW I just noticed you never mentioned the button was facebook, but it looked exactly like it so I modeled it after that – David Kroukamp Jan 05 '13 at 08:01
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());

- 16,188
- 39
- 30
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"));

- 25,802
- 5
- 69
- 87
-
-
yes, of course. prepare a custom .jpeg file and set its path to the ImageIcon instance just like above. – Juvanis Jan 04 '13 at 15:07
-
-
-
@mKorbel : Really, I didn't knew that thingy, wish I could see one example somewhere !! – nIcE cOw Jan 04 '13 at 16:37
-
1@Gagandeep Bali [for example, sure this image hasn't proper format, ratio](http://stackoverflow.com/a/7919280/714968) – mKorbel Jan 04 '13 at 17:07
-
1@Gagandeep Bali there are three ways (I think all of them are proper ways) use Icon, paintComponent, override BasicButtonUI, (or modify / set arrays of Colors for ColorUIResource) – mKorbel Jan 04 '13 at 17:11
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);

- 35,625
- 19
- 175
- 265

- 31
- 1
- 7