code_junkie

Java rounded Swing JButton

Nov 13th, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. button.setBorderPainted(false);
  2. button.setContentAreaFilled(false);
  3. button.setOpaque(true);
  4.  
  5. button.setOpaque(false);
  6. button.setFocusPainted(false);
  7. button.setBorderPainted(false);
  8. button.setContentAreaFilled(false);
  9. setBorder(BorderFactory.createEmptyBorder(0,0,0,0)); // Especially important
  10.  
  11. JButton addBtn=new JButton("+");
  12. addBtn.setBounds(x_pos,y_pos,30,25);
  13. addBtn.setBorder(new RoundedBorder(10));//10 is the radius
  14. addBtn.setForeground(Color.BLUE);
  15.  
  16. private static class RoundedBorder implements Border {
  17.  
  18. private int radius;
  19.  
  20. RoundedBorder(int radius) {
  21. this.radius = radius;
  22. }
  23. public Insets getBorderInsets(Component c) {
  24. return new Insets(this.radius+1, this.radius+1, this.radius+2, this.radius);
  25. }
  26.  
  27.  
  28. public boolean isBorderOpaque() {
  29. return true;
  30. }
  31.  
  32.  
  33. public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
  34. g.drawRoundRect(x,y,width-1,height-1,radius,radius);
  35. }
  36. }
  37.  
  38. button.setOpaque(false);
  39.  
  40. class JImageButton extends JComponent implements MouseListener {
  41. private BufferedImage img = null;
  42.  
  43. public JImageButton(BufferedImage img) {
  44. this.img = img;
  45. setMinimumSize(new Dimension(img.getWidth(), img.getHeight()));
  46. setOpaque(false);
  47. addMouseListener(this);
  48. }
  49.  
  50. public void paintComponent(Graphics g) {
  51. g.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), null);
  52. }
  53.  
  54. @Override
  55. public void mouseClicked(MouseEvent e) {
  56. }
  57.  
  58. @Override
  59. public void mouseEntered(MouseEvent e) {
  60. }
  61.  
  62. @Override
  63. public void mouseExited(MouseEvent e) {
  64. }
  65.  
  66. @Override
  67. public void mousePressed(MouseEvent e) {
  68. }
  69.  
  70. @Override
  71. public void mouseReleased(MouseEvent e) {
  72. }
  73. }
Add Comment
Please, Sign In to add comment