Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Creating circle-shaped JButtons?

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Question Creating circle-shaped JButtons?

    Hi all,

    JButtons by default creates rectangular buttons. I'd like to change the shape to a circle. The following is my code:
    public void createButtons(JPanel bottomPanel) {
     
    			JButton[] buttons = new JButton[16];
    			String[] alphabets = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
    					"K", "L", "M", "N", "O", "P"}; 
     
    			for (int i = 0; i < buttons.length; i++) {
    				buttons[i] = new JButton(alphabets[i]);
    				buttons[i].setSize(40, 40);
    				buttons[i].setActionCommand(alphabets[i]);
    				buttons[i].addActionListener(this);
     
    				bottomPanel.add(buttons[i]);
    			}
     
     
    		}

    The point is i want the shape of the buttons to be circular and not the default rectangular shape. Any ideas?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Creating circle-shaped JButtons?

    Use an appropriate LayoutManager, and set the size of the JButton using setPreferredSize (and setMaximumSize/setMinimumSize) to constrain its dimensions. If you have issues, I recommend posting an SSCCE so others can reproduce the problem and provide options to fix.

Similar Threads

  1. Help with Square Shaped Spiral
    By lionel41 in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2013, 02:48 AM
  2. Replies: 1
    Last Post: January 3rd, 2012, 06:28 PM
  3. java (circle)
    By coder.freak in forum Member Introductions
    Replies: 3
    Last Post: March 26th, 2011, 12:01 PM
  4. Moving circle problem help
    By nobleisthyname in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 3rd, 2010, 11:43 AM
  5. Implement dragging a circle
    By jolly in forum AWT / Java Swing
    Replies: 0
    Last Post: August 19th, 2009, 02:48 PM

Tags for this Thread