views:

1971

answers:

4

hi, im trying to make a JButton round at the (x,y) corrds of (150,210). i want the button to be an oval of the size (40,40). and the buttons background color red. it doesnt need text so i should get a button to what ever size i want right.

i am using a panel and i set the setLayout to null

setLayout(null)

i checked google and it brought me here please help me.

+4  A: 

JDC Tech Tips: August 26, 1999: Creating Round Swing Buttons

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class RoundButton extends JButton {
  public RoundButton(String label) {
    super(label);

// These statements enlarge the button so that it 
// becomes a circle rather than an oval.
    Dimension size = getPreferredSize();
    size.width = size.height = Math.max(size.width, 
      size.height);
    setPreferredSize(size);

// This call causes the JButton not to paint 
   // the background.
// This allows us to paint a round background.
    setContentAreaFilled(false);
  }

// Paint the round background and label.
  protected void paintComponent(Graphics g) {
    if (getModel().isArmed()) {
// You might want to make the highlight color 
   // a property of the RoundButton class.
      g.setColor(Color.lightGray);
    } else {
      g.setColor(getBackground());
    }
    g.fillOval(0, 0, getSize().width-1, 
      getSize().height-1);

// This call will paint the label and the 
   // focus rectangle.
    super.paintComponent(g);
  }

// Paint the border of the button using a simple stroke.
  protected void paintBorder(Graphics g) {
    g.setColor(getForeground());
    g.drawOval(0, 0, getSize().width-1, 
      getSize().height-1);
  }

// Hit detection.
  Shape shape;
  public boolean contains(int x, int y) {
// If the button has changed size, 
   // make a new shape object.
    if (shape == null || 
      !shape.getBounds().equals(getBounds())) {
      shape = new Ellipse2D.Float(0, 0, 
        getWidth(), getHeight());
    }
    return shape.contains(x, y);
  }

// Test routine.
  public static void main(String[] args) {
// Create a button with the label "Jackpot".
    JButton button = new RoundButton("Jackpot");
    button.setBackground(Color.green);

// Create a frame in which to show the button.
    JFrame frame = new JFrame();
    frame.getContentPane().setBackground(Color.yellow);
    frame.getContentPane().add(button);
    frame.getContentPane().setLayout(new FlowLayout());
    frame.setSize(150, 150);
    frame.setVisible(true);
  }
}
Adam Davis
+2  A: 

There's a good explanation of how you can do this here: http://forums.sun.com/thread.jspa?threadID=284897&tstart=28696 that can be extended to buttons of any shape.

Andy Mikula
A: 

Easy it is called JRounded button

A: 

if make a round button follow this way, i think that it's over complex. Can you create an OvalButton by another way?

if you are not concerned about the actual clickable bounds of the oval (keeping it to the containing rectangle), then using a png image and setContentAreaFilled(false); works
brian_d