button size/insets

Posted on 2005-04-20
Last Modified: 2012-06-27
I have a button with only one character as text. I want this button as small as possible. How can I change its insets ?

- button.getInsets().set(0, 0, 0, 0) doesn't seem to work
- if I do button.setBorder(null) - I get the smallest possible size which is what I want, but I lose, of course, the button's nice look

Question by:hapciu
    LVL 86

    Expert Comment

    Use a smaller font ?
    LVL 92

    Expert Comment

    you could use a narrower border but that doesn't save you much.

    Author Comment

    exactly. how do i change the border's size/insets without really knowing the border's type ? well button.getInsets() or button.getBorder().getBorderInsets() and change those you say... but it don't work.

    Just try this:
    JButton b = new JButton("I");
    // add the button to your favourite container and see how excessively wide it is :)
    b.getInsets().left = 0;
    b.getInsets().right = 0;

    ... nothin

     And I'd like this to work on all L&Fs :)
    LVL 92

    Expert Comment

    you don't need to know the border size, you only need to set it to whatever border you want.
    LVL 35

    Accepted Solution

    Insets margins = new Insets( 0, 0, 0, 0 ) ;
    JButton b = new JButton("I");
    b.setMargin( margins ) ;


    Author Comment


    I found out myself just now - one trick is to extend Jbutton and overwrite getInsets() to return what you want - in my case new Insets(2, 2, 2, 2)

    TimYates' solution is shorter though - I should've searched the Javadoc more careful (now where's that "I'm ashamed" emoticon ?)
    LVL 35

    Expert Comment


    hehe, good luck!!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    count8 challlenge 13 65
    mapBully challenge 6 51
    Starting to learn JAVA, 7 34
    Unexpected HttpURLConnection connection behavior 2 17
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now