0

I have some problems with swing painting.

My following UI (take a look at the next picture) has a JTextField on the left side, which has the possibility to show autocompletion hints. On the right side, elements, which where selected from the textfield, should be shown. These elements are contained by an JPanel which uses a FlowLayout(FlowLayout.LEFT) as layout manager.

my ui

Now when selecting some elements from the textfield, which will be added to the flowlayout, the elements won't be painted. (Shown in the next picture) New elements won't be painted

Now I have two possibilities to show these non painted elements.

First: After resizing the JFrame the elements will be shown.

Second: Selecting enough elements so that the FlowLayout has to take a line break. After a line break all elements will be shown. When selecting additional elements they won't be shown till the next line break.

Elements after a FlowLayout line break:

Elements after a FlowLayout line break

What can I do?

David Lavender
  • 8,021
  • 3
  • 35
  • 55
endian
  • 4,761
  • 7
  • 32
  • 54
  • Try calling: `revalidate()` on your FlowLayout panel after you add the element. This will force the layout manager to layout its components again. I can't suggest any more than that without seeing some code. The problem could also be in your green components paint method. – David Lavender Feb 13 '13 at 13:41
  • If `revalidate()` doesn't work, try adding `repaint()`. – alan.sambol Feb 13 '13 at 13:44
  • I've already tried following "magic" swing methods: validate(), revalidate(), repaint(), updateUI(). – endian Feb 13 '13 at 13:47
  • did you try validate() on the outer JPanel with `SwingUtilities.invokeLater()`? - btw. that's one of the reasons I do not like Swing! – michael_s Feb 13 '13 at 13:49
  • @michael_s: yes but won't work – endian Feb 13 '13 at 13:54
  • I guess the problem must be in the paint method of the green box. Is that a custom component? If so, post its paint / paintComponent method. From the screenshots, the layout manager seems to allocate space for it, but the element doesn't drawn anything. – David Lavender Feb 13 '13 at 13:58
  • @MrSpoon: The green box is an JXLabel (which is a class from the swingx library and extends JLabel). The swingx ui classes have the possibility to attach an background painter ([link](http://javadoc.geotoolkit.org/external/swingx/org/jdesktop/swingx/painter/Painter.html)). – endian Feb 13 '13 at 14:02
  • 4
    For better help sooner, post an [SSCCE](http://sscce.org/). And re. the `JXLabel`. Factor it out for the SSCCE and use instead a `JLabel`. I am guessing you will be able to get it to fail in the same way using pure J2SE. – Andrew Thompson Feb 13 '13 at 14:10
  • Was about to say try swapping JXLabel for a plain JLabel. Just as a quick sanity check. Could be a JXLabel bug. – David Lavender Feb 13 '13 at 14:15
  • @MrSpoon: I don't think it's an JXLabel bug, because I use the same class in another view an there are no problems. – endian Feb 13 '13 at 14:17
  • What are those hint elements? – Ömer Faruk Almalı Feb 13 '13 at 14:25
  • Call `pack()` on `JFrame` for it to respect new panel size (after additonal things are added) – David Kroukamp Feb 13 '13 at 14:27
  • @ÖmerFarukAlmalı: JXLabels – endian Feb 13 '13 at 14:27
  • @DavidKroukamp: but pack() changes the size of the JFrame – endian Feb 13 '13 at 14:28
  • @DavidKroukamp: Please read the question. I have no size problems. The size for the green elements will be reserved. The elements are black/not painted, thats the problem. – endian Feb 13 '13 at 14:30
  • I see... if you overrode any paint method have you made sure to invoke its `super.XXX` implementation? I.e if we dont invoke `super.paintComponent(..)` (as first call) in overridden `paintComponent` of `JComponent` visual artifacts may occur... Please do look into posting SSCCE right now we are guessing there could be may other contributing factors which only your code will show – David Kroukamp Feb 13 '13 at 14:32
  • @DavidKroukamp: I don't override any paint methods. – endian Feb 13 '13 at 14:35

1 Answers1

0

Exchanging FlowLayout throw custom WrapLayout fixed the problem. Seems it was a layout problem.

endian
  • 4,761
  • 7
  • 32
  • 54