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

  • Home
  • SEARCH
  • 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 7056895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:55:17+00:00 2026-05-28T03:55:17+00:00

I have created a text field component with an ‘x’ button inside that will

  • 0

I have created a text field component with an ‘x’ button inside that will clear the field’s text. It works great when using Window’s system look and feel (for Windows), however I am having problems trying to get the right look when using Nimbus – namely, I can’t change the margins on the button using button.setMargin(new Inset(0, 0, 0, 0) as I can with the system look and feel.

I did look for help setting the margins when using Nimbus and it appears to be a common problem. I was able to find a partial solution that does allow me to change the margins, however it changes the margins for all buttons and I want only the single button to be different than the default.

I set the ‘x’ button size explicitly, calculated from the bounds of the ‘x’ string so that it is just big enough to display the ‘x’.

The result when using Window’s system look and feel, which is the general appearance that I’m looking for:

System look and feel

The result when using the default Nimbus look and feel; ‘x’ overflows the space Nimbus allows for it, resulting in an ellipsis.

Nimbus look and feel

The result when using Nimbus and overriding the Button.contentMargins: Now the ‘x’ button looks correct, but the ‘test’ button looks awful.

Nimbus look and feel, plus margin overrides

Source code:

import java.awt.Graphics;
import java.awt.Insets;
import java.awt.geom.Rectangle2D;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LookAndFeel;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;

public class TextFieldTest {        
    public static void main(String[] args) {
        boolean useNimbusLookAndFeel    = false;
        boolean overrideNimbusDefaults  = false;
        String lafName = UIManager.getSystemLookAndFeelClassName();

        if ( useNimbusLookAndFeel ) {
            for ( LookAndFeelInfo info : UIManager.getInstalledLookAndFeels() ) {
                if ( "Nimbus".equals(info.getName()) ) {
                    lafName = info.getClassName();      
                    break;
                }
            }
        }

        try {
            UIManager.setLookAndFeel(lafName);              
        }
        catch (ClassNotFoundException e) {}
        catch (InstantiationException e) {}
        catch (IllegalAccessException e) {}
        catch (UnsupportedLookAndFeelException e) {}

        //Code to set override default button insets for Nimbus:
        if ( useNimbusLookAndFeel && overrideNimbusDefaults ) {
            LookAndFeel laf = UIManager.getLookAndFeel();               
            if ( laf != null ) {
                UIDefaults def = laf.getDefaults();
                def.put("Button.contentMargins", new Insets(0,0,0,0));
            }
        }       

        //Create frame:
        JPanel panel = new JPanel();        
        panel.add(new JButton("test"));
        panel.add(new DeletableTextField());

        JFrame frame = new JFrame();
        frame.getContentPane().add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }   

    static class DeletableTextField extends JTextField {
        JButton         mButton     = null;

        public DeletableTextField() {
            super(30);
            setup();
        }

        private void setup() {
            mButton = new JButton("x");
            mButton.setMargin(new Insets(0, 0, 0, 0));
            add(mButton);
        }

        @Override
        public void paint(Graphics g) {
            g.setFont(mButton.getFont());
            Rectangle2D rect    = g.getFontMetrics().getStringBounds(mButton.getText(), g);
            int stringWidth     = (int)rect.getWidth();

            int topOffset       = 3;
            int rightOffset     = 3;
            int bottomOffset    = 3;
            int width           = (int)Math.ceil(stringWidth * 2.5);
            int height          = getHeight() - (bottomOffset + topOffset);         
            int x_coord         = getWidth() - (rightOffset + width);

            mButton.setMargin(new Insets(0, 0, 0, 0));
            mButton.setBounds(x_coord, topOffset, width, height);

            super.paint(g);
        }
    }
}

I am aware that I can accomplish this in other ways, like painting my own ‘x’ on top, but I thought it would be nice to take advantage of the current look-and-feel’s button painting abilities for a unified look.

So I would like to ask if there is any way to change a single JButton’s margins when using the Nimbus look and feel without changing the margins for all the buttons. Alternatively, if there’s already a swing component out there that looks similar (text field with ‘x’ button), I may be able to use that instead.

  • 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-28T03:55:18+00:00Added an answer on May 28, 2026 at 3:55 am

    Nimbus supports per-instance overrides of LAF properties, in your context something like:

      mButton = new JButton("x");
      UIDefaults def = new UIDefaults();
      def.put("Button.contentMargins", new Insets(0,0,0,0));
      mButton.putClientProperty("Nimbus.Overrides", def);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a dynamic text field and button. I want to apply UI
I have created an form validation using ajax/php. Each text box is validated using
I have created a non-form c# program that uses the NotifyIcon class. The text
I have created a from which have a text field name 'empId' whose value
I have created a settings bundle with a single preference value, a text field
i have made user-register.tpl.php file. And i have set many text field in that.
I have created JavaScript code for validation of a simple text field. The issue
I have created some tables with text fields inside, like in the contacts for
I have created a web.py URL,and it provides only 2 text boxes(No submit button).
I have encountered strange problem. I have created simple Flash text field control and

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.