Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java swing - make the smallest button

Tags:

button

swing

Hi
When I create a button in swing it adds a border around my text, thus making my button a bit bigger.
Now, I really need that screen space and what I usually do is create a text item (disabled ) which create much smaller component size (smaller space around my text) and add to it a listner. saves space. but awkward. Is there a better way to create a tiny button? (when I try to make it smaller it quickly puts a "..." inside though it got room for much more text)

10x

like image 453
Bick Avatar asked Feb 16 '11 08:02

Bick


People also ask

How do I make a JButton smaller?

Simply delete the frame. setPreferredSize() and the buttons will be small (the frame size will be determined by the preferred sizes of inner components by calling frame. pack() method).

What does pack () do in swing?

The pack() method is defined in Window class in Java and it sizes the frame so that all its contents are at or above their preferred sizes. An alternative to the pack() method is to establish a frame size explicitly by calling the setSize() or setBounds() methods.

How do you set a button margin in Java?

We can set a margin to a JButton by using the setMargin() method of JButton class and pass Insets(int top, int left, int bottom, int right) as an argument.


1 Answers

You just need to set The Insets narrower...

jButton1.setText("jButton1");
jButton1.setMargin(new java.awt.Insets(1, 2, 1, 2));

The integer arguments are: int top, int left, int bottom, int right

like image 185
Costis Aivalis Avatar answered Oct 21 '22 11:10

Costis Aivalis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!