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 251987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:38:26+00:00 2026-05-11T21:38:26+00:00

This is a continuation of the question Java rounded Swing JButton . I have

  • 0

This is a continuation of the question “Java rounded Swing JButton”. I have searched for an extension of javax.swing.JButton which will inherit all runtime behavior and just override drawing of the corners.

Using the code given by noah.w on sun forums page the result looks like this:

alt text

I’d like to be able to have the same gradient in the background, on mouse over change etc. Does anybody know how to do that?

Code which creates the Java Swing window from the picture is:

public class XrButton extends JButton implements MouseListener {

    private static final long serialVersionUID = 9032198251140247116L;

    String text;
    boolean mouseIn = false;

    public XrButton(String s) {
        super(s);
        text = s;
        setBorderPainted(false);
        addMouseListener(this);
        setContentAreaFilled(false);
    }

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        if (getModel().isPressed()) {
            g.setColor(g.getColor());
            g2.fillRect(3, 3, getWidth() - 6, getHeight() - 6);
        }
        super.paintComponent(g);

        if (mouseIn)
            g2.setColor(Color.red);
        else
            g2.setColor(new Color(128, 0, 128));

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setStroke(new BasicStroke(1.2f));
        g2.draw(new RoundRectangle2D.Double(1, 1, (getWidth() - 3),
                (getHeight() - 3), 12, 8));
        g2.setStroke(new BasicStroke(1.5f));
        g2.drawLine(4, getHeight() - 3, getWidth() - 4, getHeight() - 3);

        g2.dispose();
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.getContentPane().setLayout(new FlowLayout());

        XrButton xrButton = new XrButton("XrButton");
        JButton jButton = new JButton("JButton");

        frame.getContentPane().add(xrButton);

        frame.getContentPane().add(jButton);

        frame.setSize(150, 150);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
        mouseIn = true;
    }

    public void mouseExited(MouseEvent e) {
        mouseIn = false;
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }
  • 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-05-11T21:38:26+00:00Added an answer on May 11, 2026 at 9:38 pm

    I think you’ve got 2 choices here:

    1.Implement drawing yourself in a paint method of your component

    2.Create new ButtonUI for your look and feel. In this case i would suggest to use Synch LAF

    In both cases drawing different states is your resposibility

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 147k
  • Answers 147k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer $('input[type=checkbox]').change(function(){ setMyTextArea(getMyMessage(this.checked)); }); function setMyTextArea(text){ $('#MyTextArea').val(text); } function getMyMessage(checked){ return… May 12, 2026 at 9:09 am
  • Editorial Team
    Editorial Team added an answer I assume the error is on this line from your… May 12, 2026 at 9:09 am
  • Editorial Team
    Editorial Team added an answer A little more in detail: <IfModule mod_rewrite.c> The contents of… May 12, 2026 at 9:09 am

Related Questions

Will the CAN interface card likely be installed as a COM port? How do
I work on financial applications in Java and getting concurrency right is pain. Erlang
This is a continuation of my question about reading the superblock . Let's say
This is a continuation of this question . I'm in the process of testing

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.