0

I have a JLabel for an error output. This is empty in the beginning and can be filled by some actions. But when it's empty it won't be noticed when I pack() my JFrame. So when my error label gets content, I need to repack my frame to make it visible (and that also resizes the frame what I want to avoid).

At the moment I use

lError.settext(" "); // set text to space as buffer content

but I'm not happy with this solution.

Is there another cleaner way to reach my result?

Edit: I reached what I wanted by editing the size of the panel around my label with setPreferredSize(), but I still have this space buffer.

pError.setPreferredSize(new Dimension(pError.getPreferredSize().width, new JLabel(" ").getPreferredSize().height));
Froxx
  • 957
  • 4
  • 14
  • 27
  • is the `lError` fixed size or can there be so much text that you need a scroller? – Martin Frank Oct 01 '14 at 11:02
  • @MartinFrank Scrolling won't be necessary. – Froxx Oct 01 '14 at 11:05
  • if you will right IError.setText(""); it will work as well. but it is null string rather then space. – Irfan Nasim Oct 01 '14 at 11:05
  • For fixed size label you can set preferred size. lError.setPreferredSize(new Dimension(20, 18)); – Sergiy Medvynskyy Oct 01 '14 at 11:05
  • @IrfanNasim No, it doesn't. When I set the text to null string, pack() ignores the space of the label. – Froxx Oct 01 '14 at 11:07
  • when no scrolling is necessary, see @SergiyMedvynskyy... `lError.setPreferredSize(new Dimension(w,h));` – Martin Frank Oct 01 '14 at 11:08
  • @MartinFrank That'd be good, but I dont't want to fix my width because it could cut off my text. And when I set it to match the parent's one, it would be aligned left. – Froxx Oct 01 '14 at 11:18
  • ok, i'm confused a bit ^^... you don't want variable size (would be scroller) and you don't want fixed size? so what? ... but obviously you want also to be another question, how to center a text in JPanel? see http://stackoverflow.com/questions/6810581/how-to-center-the-text-in-a-jlabel where they answer with SwingConstants.CENTER (two answers there)... – Martin Frank Oct 01 '14 at 11:25
  • @MartinFrank Oh well, I mean I don't need a scroller as a scroll bar or something like that, because my error won't be as long as my panel / frame is wide. But the width of the label should be variable. – Froxx Oct 01 '14 at 11:30
  • ok, thanks for clearing that out! i think i get you now better... – Martin Frank Oct 01 '14 at 11:32
  • i hope my pic is somehow correct? – Martin Frank Oct 01 '14 at 11:34
  • @MartinFrank Erm, no sorry ^^ I edited my question. – Froxx Oct 01 '14 at 11:45
  • well i'm glad you found your solution ^^ – Martin Frank Oct 01 '14 at 11:46
  • @MartinFrank Actually I didn't find a solution I'm glad with. I just moved my problem to a less annoying place `new JLabel(" ")` :/ – Froxx Oct 01 '14 at 11:48

1 Answers1

0

It may depend on what Layout you are using, but sometimes I've had to call not just setPreferredSize but also setMinimumSize (and setMaximumSize).

nachose
  • 101
  • 1