Hey, I am creating a calculator program, with some small buttons, I want one of the buttons to have "Ans" on them, but whenever I make the JButton smaller then 50, 50, it will show three dots. "...", how can I remove these dots and show the normal text given?
-
It has been a long time since I used jButton, but maybe you want to reduce the fontsize of the text entered in jButton or a simple hack just paste a jLabel over the jButton and set the jButton text to "" – chaitanya Apr 27 '11 at 17:39
5 Answers
Probably this because margin of your button is too big.
Try this:
myButton.setMargin(new Insets(0, 0, 0, 0));
You can also turn off border:
button.setBorder(null);

- 26,145
- 14
- 53
- 66
Don't set the preferred size of the button. Use the preferred size of the button and let the layout manager layout the components. The preferred size ensures all the text will be displayed properly on different Look and Feels.

- 321,443
- 19
- 166
- 288
-
Yeah, but I want it all nicely spaced where I want it and how I want it, I disabled Layout Managers, and used button.setBounds(); – Stan Apr 27 '11 at 17:44
-
-
No, I just wanted the "..." to dissapear, but, my question has already answered by smas. – Stan Apr 27 '11 at 17:49
-
1@Stan, Using setBounds is NOT the way to design a GUI. Only people just learning Java do this. Using a layout manager is the proper way to do this. Your code will NOT be portable to other platforms. – camickr Apr 27 '11 at 21:11
This code attempts to explain why layouts and preferred sizes are so important. The important part lies in the input/output.
TestGuiSize.java
import java.awt.*;
import javax.swing.*;
class TestGuiSize {
public static void addButtonToPanel(JPanel panel, String label) {
JButton button = new JButton(label);
button.setMargin(new Insets(1,1,1,1));
panel.add(button);
}
public static void main(String[] args) {
JPanel p = new JPanel(new GridLayout(4,3,3,3));
addButtonToPanel(p, "7");
addButtonToPanel(p, "8");
addButtonToPanel(p, "9");
addButtonToPanel(p, "/");
addButtonToPanel(p, "4");
addButtonToPanel(p, "5");
addButtonToPanel(p, "6");
addButtonToPanel(p, "*");
addButtonToPanel(p, "1");
addButtonToPanel(p, "2");
addButtonToPanel(p, "3");
addButtonToPanel(p, "-");
addButtonToPanel(p, "0");
p.add(new JLabel(""));
addButtonToPanel(p, "Del");
addButtonToPanel(p, "+");
Dimension d = p.getPreferredSize();
System.out.println(
"Preferred Size: " +
d.getWidth() +
"x" +
d.getHeight());
JOptionPane.showMessageDialog(null, p);
}
}
Input/output
prompt> java TestGuiSize
Preferred Size: 113.0x105.0
prompt>java -Dswing.plaf.metal.controlFont=Dialog-22 TestGuiSize
Preferred Size: 169.0x157.0
prompt>java -Dswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel TestGuiSize
Preferred Size: 93.0x93.0
prompt>java -Dswing.defaultlaf=com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel TestGuiSize
Preferred Size: 205.0x129.0
prompt>
Run-time parameters are just the tip of the iceberg of the differences between runs that might sink an application's GUI code. Layouts are designed to handle such differences.

- 168,117
- 40
- 217
- 433
Use setMargin(Insets m)
to adjust the space between the JButton border and the label. The default is (2, 14, 2, 14)
. To maximize the space available for the label (and to remove the dots completely) you can use something like
myButton.setFont(new Font("Tahoma", Font.BOLD, 11));
myButton.setMargin(new Insets(0, -1, 0, -20));
myButton.setHorizontalAlignment(SwingConstants.LEFT);

- 563
- 6
- 9
You could change the size of the font on the button. See these links:
Increasing font size (Can be easily changed to decrease font size)

- 7,972
- 21
- 79
- 122