Setting a 1 or 2-character text on a JButton 32x32

Go To StackoverFlow.com

3

I want to set the text on a JButton that is size 32x32 but it only shows "...". yeah I know you could see the text if you make the button bigger, but how do you make the text be shown on a 32x32 jbutton? The text is only 1 or 2 digits(characters), it is actually a counter. Thanks

2012-04-04 04:22
by Zik


3

The insets are probably crowding out the text...

try

button.setMargin(new Insets(1, 1, 1, 1));  

edit: Also, use a smaller font.

edit2: you can also control the insets for all buttons:

UIManager.put("Button.margin", new Insets(1, 1, 1, 1));
2012-04-04 04:26
by marathon
it works! thanks - Zik 2012-04-04 06:05


1

I don't think you can, this is managed directly by the look'n'feel' that is used by Java. You could try by changing it to another one to see if there is one with different insets. You could try changing them by setting smaller insects programatically.

A more complex way would be to subclass the JButton class and provide a custom drawing implementation but I think you will lose all the other cool effect.

2012-04-04 04:26
by Jack
oh ok but thanks anywa - Zik 2012-04-04 04:32
yes, this is possible. See my answer - marathon 2012-04-05 03:22


0

As per my idea , its quite simple to making GUI application  easier.I am writing some code below it may help you .

public static void main(String[] args) {
        // TODO Auto-generated method stub
            JFrame frm=new JFrame("Manoj Button Test");
            frm.setVisible(true);
            frm.setSize(500,500);
            Container cnt=frm.getContentPane();
                        //You can add any text to the JButton
            JButton btn=new JButton("Hello Button");
            cnt.add(btn);           

            //2nd type of example 
            JButton btn2=new JButton();
            int number_btntext=4;
            btn2.setText(String.valueOf(number_btntext));
            cnt.add(btn2);


    }


In the above code I have set text to GUI JButton.
2012-04-04 04:42
by xxxxxxxx d
Ads