Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8980271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:02:41+00:00 2026-06-15T20:02:41+00:00

I am trying to get a button with round edges. My button has a

  • 0

I am trying to get a button with round edges. My button has a background color of yellow. I am not able to get the round edge for my button. Here is the code i am trying

 class RoundedBorder implements Border {
        int radius;
        RoundedBorder(int radius) {
            this.radius = radius;
        }
        public Insets getBorderInsets(Component c) {
            return new Insets(this.radius+1, this.radius+1, this.radius+2, this.radius);
        }
        public boolean isBorderOpaque() {
            return true;
        }
        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
            g.drawRoundRect(x,y,width-1,height-1,radius,radius);
        }
    }

jButton1.setText(aContinue);
        jButton1.setBackground(new java.awt.Color(255, 255, 0));
        jButton1.setBorder(new RoundedBorder(20));

I am not able to get round edges using this piece of code.. Here is how my button looks .

enter image description here

I want to have round edges with no overflowing background color.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:02:43+00:00Added an answer on June 15, 2026 at 8:02 pm

    Found this great example by oracle which supplies a class which creates RoundButton.

    enter image description here

    Here is an example using an edited RoundButton class to create RoundedButton class:

    enter image description here

    import java.awt.AWTEvent;
    import java.awt.AWTEventMulticaster;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    
    public class Test {
    
        public Test() {
            initComponents();
        }
    
        private void initComponents() {
            final JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            final JTextField tf = new JTextField("");
    
            RoundedButton rb = new RoundedButton("Go");
            rb.setBackground(Color.yellow);
            rb.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    JOptionPane.showMessageDialog(frame, "You said: " + tf.getText());
                }
            });
    
            frame.add(tf, BorderLayout.NORTH);
            frame.add(rb);
    
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new Test();
                }
            });
        }
    }
    
    class RoundedButton extends Component {
    
        ActionListener actionListener;     // Post action events to listeners
        String label;                      // The Button's text
        protected boolean pressed = false; // true if the button is detented.
    
        /**
         * Constructs a RoundedButton with no label.
         */
        public RoundedButton() {
            this("");
        }
    
        /**
         * Constructs a RoundedButton with the specified label.
         *
         * @param label the label of the button
         */
        public RoundedButton(String label) {
            this.label = label;
            enableEvents(AWTEvent.MOUSE_EVENT_MASK);
        }
    
        /**
         * gets the label
         *
         * @see setLabel
         */
        public String getLabel() {
            return label;
        }
    
        /**
         * sets the label
         *
         * @see getLabel
         */
        public void setLabel(String label) {
            this.label = label;
            invalidate();
            repaint();
        }
    
        /**
         * paints the RoundedButton
         */
        @Override
        public void paint(Graphics g) {
    
            // paint the interior of the button
            if (pressed) {
                g.setColor(getBackground().darker().darker());
            } else {
                g.setColor(getBackground());
            }
            g.fillRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 20, 20);
    
            // draw the perimeter of the button
            g.setColor(getBackground().darker().darker().darker());
            g.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 20, 20);
    
            // draw the label centered in the button
            Font f = getFont();
            if (f != null) {
                FontMetrics fm = getFontMetrics(getFont());
                g.setColor(getForeground());
                g.drawString(label, getWidth() / 2 - fm.stringWidth(label) / 2, getHeight() / 2 + fm.getMaxDescent());
            }
        }
    
        /**
         * The preferred size of the button.
         */
        @Override
        public Dimension getPreferredSize() {
            Font f = getFont();
            if (f != null) {
                FontMetrics fm = getFontMetrics(getFont());
                int max = Math.max(fm.stringWidth(label) + 40, fm.getHeight() + 40);
                return new Dimension(max, max);
            } else {
                return new Dimension(100, 100);
            }
        }
    
        /**
         * The minimum size of the button.
         */
        @Override
        public Dimension getMinimumSize() {
            return new Dimension(100, 100);
        }
    
        /**
         * Adds the specified action listener to receive action events from this
         * button.
         *
         * @param listener the action listener
         */
        public void addActionListener(ActionListener listener) {
            actionListener = AWTEventMulticaster.add(actionListener, listener);
            enableEvents(AWTEvent.MOUSE_EVENT_MASK);
        }
    
        /**
         * Removes the specified action listener so it no longer receives action
         * events from this button.
         *
         * @param listener the action listener
         */
        public void removeActionListener(ActionListener listener) {
            actionListener = AWTEventMulticaster.remove(actionListener, listener);
        }
    
        /**
         * Determine if click was inside round button.
         */
        @Override
        public boolean contains(int x, int y) {
            int mx = getSize().width / 2;
            int my = getSize().height / 2;
            return (((mx - x) * (mx - x) + (my - y) * (my - y)) <= mx * mx);
        }
    
        /**
         * Paints the button and distribute an action event to all listeners.
         */
        @Override
        public void processMouseEvent(MouseEvent e) {
            Graphics g;
            switch (e.getID()) {
                case MouseEvent.MOUSE_PRESSED:
                    // render myself inverted....
                    pressed = true;
    
                    // Repaint might flicker a bit. To avoid this, you can use
                    // double buffering (see the Gauge example).
                    repaint();
                    break;
                case MouseEvent.MOUSE_RELEASED:
                    if (actionListener != null) {
                        actionListener.actionPerformed(new ActionEvent(
                                this, ActionEvent.ACTION_PERFORMED, label));
                    }
                    // render myself normal again
                    if (pressed == true) {
                        pressed = false;
    
                        // Repaint might flicker a bit. To avoid this, you can use
                        // double buffering (see the Gauge example).
                        repaint();
                    }
                    break;
                case MouseEvent.MOUSE_ENTERED:
    
                    break;
                case MouseEvent.MOUSE_EXITED:
                    if (pressed == true) {
                        // Cancel! Don't send action event.
                        pressed = false;
    
                        // Repaint might flicker a bit. To avoid this, you can use
                        // double buffering (see the Gauge example).
                        repaint();
    
                        // Note: for a more complete button implementation,
                        // you wouldn't want to cancel at this point, but
                        // rather detect when the mouse re-entered, and
                        // re-highlight the button. There are a few state
                        // issues that that you need to handle, which we leave
                        // this an an excercise for the reader (I always
                        // wanted to say that!)
                    }
                    break;
            }
            super.processMouseEvent(e);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get a button to stop flashing after the fancybox has
I'm trying to get a button to work , I'm not allowed to use
I'm trying to get a button which looks exactly the same whether it is
I have a C# winforms application and I am trying to get a button
I'am trying to get the Login button from Facebook to work on my website
I am trying to get my first button to update a display number in
I am trying to get my like button on my website www.nacts.com.au to add
im using the WPFToolkit's DataGrid and im trying to get an edit button working,
Hey there, I am trying to get my signout button to work. This is
Trying to get the id of the radio button that is checked in android,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.