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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:56:25+00:00 2026-05-25T17:56:25+00:00

My code for custom button is: public class GreyButton extends JLabel { private int

  • 0

My code for custom button is:

public class GreyButton extends JLabel {

    private int     ButtonWidth,
                    ButtonHeight;
    String          ButtonText;

    public GreyButton(String BText, int BWidth, int BHeight) {
        super(BText);
        this.ButtonHeight   =   BHeight;
        this.ButtonWidth    =   BWidth;
        this.ButtonText     =   BText;
        setGreyButton();
    }

    private void setGreyButton() {
        this.setPreferredSize(new Dimension(this.ButtonWidth, this.ButtonHeight));
        this.setBackground(Color.LIGHT_GRAY);
        this.setOpaque(false);
        this.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
        this.setForeground(Color.WHITE);
        this.setHorizontalAlignment(SwingConstants.CENTER); //This line
    }

    @Override
    public void paint(Graphics g) {
        paintComponent(g);
    }

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D  Shape                   =   (Graphics2D) g;

        AlphaComposite newComposite         =   AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f);
        Shape.setComposite(newComposite);

        Color[]         FillArray           =   {Color.WHITE, Color.GRAY};
        float[]         Distribution        =   {0.85f, 1.0f};
        GradientPaint   Fill                =   new GradientPaint(10, 8, Color.BLACK, 10, 72, Color.WHITE);         
        Paint           OldPaint            =   Shape.getPaint();

        Shape.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        Shape.setPaint(Fill);
        Shape.fillRect(0, 0, ButtonWidth, ButtonHeight);
        Shape.setPaint(OldPaint);
        Shape.setFont(new Font("Monospace", Font.BOLD, 14));
        Shape.drawString(ButtonText, 0, 0); //This line
    }

}

This is to create a JLabel with custom 2D Graphics. The problem is that I am trying to center the text in the JLabel and this be should valid for any size the used in the constructor.

Currently, I need to calculate the values and set the second and third parameters of drawString accordingly.

Question: Is there a general way of centering a text on JLabel whose size may differ with each instance?

  • 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-25T17:56:26+00:00Added an answer on May 25, 2026 at 5:56 pm

    Don’t override the paint() method when doing custom painting. You would never override the method to invoke the paintCompnent() method.

    If you are just trying to paint a custom background then I would think you would:

    1. Paint your gradient background at the start of the paintComponent() method.

    2. Then invoke super.paintComponent(g). The normal painting code will position the text based on the horizontal alignment property.

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

Sidebar

Related Questions

First, please look at this custom Button-inherited UserControl code: Public Class UserControl1 Dim _Text
I have a custom button class called ImageButton that extends JButton. In it i
I have a custom dialog inside public class NewPartnerDialog extends Dialog implements OnClickListener And
I am developing a custom messagebox class like the following- Public Class MyCustomMsgBox Private
I added a custom menu to the menu button using the following code: @Override
I'm trying to extend a class to eventually make a custom button. I read
I defined a simple event class: public class NewMoveEvent extends Event { public function
I have a custom Button and I want to set its dimensions in code
I have the following problem / bug: I made a custom button-class (called CustomBlitButton)
I've got a custom Button class, that always performs the same action when it

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.