0

Is there a value that can be put into setBounds to set the width to the minimum needed?

JLabel title = new JLabel("Title Text");
title.setBounds(50, 20, ?, 13);

Rather than using guess & check to find the minimum width, is there a value I can use?

jzbakos
  • 285
  • 1
  • 3
  • 12
  • With swing, the layout decides what size a component will have so calling setBounds is ignored. You can however influence the sizes swing will use with calls to `setPreferredSize(Dimension d)` – MeBigFatGuy Oct 22 '16 at 23:53

1 Answers1

1

If you already know the text that will be in the JLabel (i.e., if that text won't change after being assigned once), there is no need to set the minimum width; the JLabel should do so automatically.

Shannon
  • 100
  • 8
  • Okay, but what value would I use? – jzbakos Oct 22 '16 at 23:36
  • You wouldn't use any; you wouldn't use `setBounds()` at all. `JLabel`s resize automatically to fit whatever you put in them. – Shannon Oct 22 '16 at 23:38
  • What do you recommend I use to place the label in a specific location? For some reason, setLocation does not work. – jzbakos Oct 22 '16 at 23:40
  • I would suggest using a layout manager of some kind... `MiGLayout` is awesome for a grid-based layout (it's very flexible.) If you don't want to use a layout at all, check out this link: [link](https://docs.oracle.com/javase/tutorial/uiswing/layout/none.html). – Shannon Oct 22 '16 at 23:44