0

Is there a way to "ignore" weather the text of a button fits or not so in the case it doesn't fit, it doesn't display "..."?

I'm making a game that has a grid of buttons and I'm using the chess unicode characters as the text on these buttons. With small fonts they show up but they are really small. But with larger fonts, java "thinks" they wont fit and displays them as "..." even thought they would definitively fit.

Difference

The code that generates the buttons:

        jPanel.setLayout(new GridLayout(13, 13));
    tab = new JButton[13][13];  //Tablero
    for (int j = 0; j <= 12; j++) {
        for (int i = 0; i <= 12; i++) {

            JButton jButton = new JButton();
            jButton.setText("♟");
            jButton.setBackground(getColor(i, j));
            jPanel.add(jButton);
            tab[i][j] = jButton;
            //jButton.setFont(new Font("TimesRoman", Font.PLAIN, 14));
            jButton.setForeground(Color.pink);
        }
    }
Chapi
  • 85
  • 6
  • 1
    `But with larger fonts, java thinks they wont fit and displays them as "..." even thought they would definitively fit.` - If they display as "..." then it means it won't fit. Don't hardcoded the preferred size of your components and then Swing will paint the component at its preferred size, which means the image will fit. – camickr Nov 23 '15 at 19:04
  • 1
    Don't set sizes or bounds, or use `null` layouts. Be sure to call `pack()` on your GUI after adding all components and before calling `setVisible(true)`. – Hovercraft Full Of Eels Nov 23 '15 at 19:06
  • @camickr If you mean not to hardcode it as in not specifying a font for the text, that simply makes it use the default font which won't display neither. Is that what you meant to suggest? – Chapi Nov 23 '15 at 19:07
  • 2
    I guess the margin is too large, see http://stackoverflow.com/a/5808226/5596109 on how to fix that. – Dennis Waldherr Nov 23 '15 at 19:09
  • Thanks @DennisWaldherr simply button.setBorder(null); removed the bounds and removed the "...". I can't believe I couldn't find that question before, guess I didn't picture the solution as "removing" something. Thanks anyways – Chapi Nov 23 '15 at 19:12
  • 1
    Bad solution to remove the border. You're still setting sizes somewhere, and you're not packing your GUI. I can almost guarantee this. – Hovercraft Full Of Eels Nov 23 '15 at 19:14

1 Answers1

5

If you see "..." it means that the size of the component is too small to display the text properly. This means you are overriding some property of the button:

  1. You are using a null layout and manually specifying the size
  2. You are using a layout but you have used setPreferredSize()

In these cases the button will not paint properly.

You can have the chess piece occupy more space of the button by changing the margin of the button:

JButton button = new JButton(...);
button.setMargin( new Insets(5, 5, 5, 5) );

but you still have to fix one of the above two problems so that you are using Swing layouts properly.

camickr
  • 321,443
  • 19
  • 166
  • 288