1

Swing newbie here. I used the NetBeans built-in GUI build to make an applicaton with a menu and some JDialogs. On some (not all) of the JDialogs, the labels of the JButtons are truncated. For instance, if a JButton label should read Create, I'll see Creat.... Even when I resize the window the buttons don't resize and show the entire label.

What does SO recommend? Are there special settings in NB that I have to configure in order to force the application to show the whole label? Thanks in advance.

IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
  • such things doesn't happen normally. If you can provide some code (in which you getting this problem), may be then anyone here will be able to help you. – gprathour Dec 11 '11 at 05:00
  • Don't use a null layout. Don't use setPreferredSize(). – camickr Dec 11 '11 at 16:08

2 Answers2

3

You may be violating the button's preferred size through incorrect use of the enclosing panel's layout. The GUI designer may obscure the problem. Several related caveats are discussed here.

Also consider creating an sscce that exhibits the problem you describe.

Addendum: As concrete examples, each ButtonPanel has a default FlowLayout, which allows each button to adopt its preferred size. By comparison, the buttons in this ButtonIconTest adopt the preferred size of the specified icons.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
-2

You (or Netbeans) is probably setting the preferred width of the JLabel.

If possible, use another Layout: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html.

If that is not practical, you can also manually specify your desired button width

label.setMinimumSize(size);
label.setPreferredSize(size);

as long as you will not change the font.

Allen Z.
  • 1,560
  • 1
  • 11
  • 19
  • 2
    As suggested [here](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi/7229662), please don't recommend setting the preferred size. It's almost sure to fail on a platforms with different font metrics. – trashgod Dec 11 '11 at 05:34
  • I don't wish to start a debate over this, but *never* is a bit restrictive. Sometimes over-abstraction actually makes code harder to maintain - that is why magic numbers have their uses. This use case is basically static. The text involved, settings such as rows or columns, amount of data, look and feel, borders that might have been applied, layout manager, and size of the image (?!) are all not going to change. You might be able to argue that the font might change, but how many computers don't have (for example) Lucida? I am simply providing a tool; I defer judgment to the OP. – Allen Z. Dec 11 '11 at 06:15
  • 2
    I'd '+1' for the link to the layouts guide, but the advice to `setXXXSize()` takes the shine off the answer. :( Any chance of an edit? – Andrew Thompson Dec 11 '11 at 06:16
  • "but *never* is a bit restrictive." Umm.. 'never'? Who said that? I mean *before* you. – Andrew Thompson Dec 11 '11 at 06:22
  • @AndrewThompson "You should **_never_** use this method!!!" - From trashgod's link, if you read it. – Allen Z. Dec 11 '11 at 06:24
  • @AllenZ. If you *search* that linked document for "never use" (or "**_never_** use" for that matter), you'll find **0 hits**. Can you link to the specific question, answer or comment where that is written? -1 for inventing comments or answers that don't exist. – Andrew Thompson Dec 11 '11 at 06:31
  • @AndrewThompson Sorry, the never was both bolded *and* italicized. Yes, it's from the linked document - in fact, from the answer _you_ edited. Try re-reading that answer. – Allen Z. Dec 11 '11 at 06:42
  • 1
    Sorry, I have to demur: font metrics is just one aspect of the UI delegate's calculation that is defeated by `setPreferredSize()`. – trashgod Dec 11 '11 at 09:33
  • @trashgod I am curious - how is the UI delegate involved with the font? Also, context may be appropriate here: the first three words of the question is "Swing newbie here." This is not going to be a GUI that will be used on 10,000 computers on 100 OS's; it just needs to look decent on the zharvey's computer. Fixating on one issue - opinion, really - detracts from the learning experience, which should ideally be focused on concepts. And at the other end, if I were to make a proper, production GUI, I would not use Java. – Allen Z. Dec 11 '11 at 09:44
  • 1
    *"if I were to make a proper, production GUI, I would not use Java."* That in itself underlines why you should **refrain** from giving advice on Java GUIs. – Andrew Thompson Dec 11 '11 at 09:54
  • See also [`getPreferredSize()`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#getPreferredSize%28%29). Being a newbie makes it all the more important to get this right, and even experienced programmer's get this wrong. I know this because I am an experienced programmer who's gotten this wrong! :-) – trashgod Dec 11 '11 at 10:00
  • Andrew - I would not give advice on production Java GUI, but I believe that I am capable of commenting on CS 101 content. trashgod - I don't believe you can attach moral judgment - right and wrong - to code. Code that is "right" is code works to specifications. Here, the specs don't include "must work on eComStation". – Allen Z. Dec 11 '11 at 17:39
  • This is all I have to say - Both of you have valid arguments, and I agree that `Layout` is preferable. However, my personal coding style (which I ascribe no value judgment to) is GTD - I don't optimize my code or refactor it to be more "correct" if it works. I believe there are cases where the effort required for full abstraction, possibly requiring a custom `Layout` subclass, is disproportionate to the benefits. For me then, the complete answer should include `setPreferredSize`, as long as the design tradeoffs are discussed. – Allen Z. Dec 11 '11 at 18:01