2

I've created an applet which has one large panel to display data surrounded by several controls (buttons, textfields, etc.). The large panel contains several layers of labels which I render myself.

The controls all have tooltips associated with them, and some of these tooltips overlap the main panel. When they disappear, they leave a hole in the main panel image until the main panel is repainted.

Now mind you, this does not always happen. It only occurs when the cursor is in a certain range. If you get far enough to either the left or right (no difference noted for changes along the Y axis), the holes are painted over when the tooltip disappears.

I'm not well-versed on how tooltips and repainting are supposed to work, and if this is a sign that there's something dreadfully wrong with my program, but if I can just call repaint on the main panel whenever the tooltip disappears, I should be fine. Is there something I can override in tooltip to make this happen?

I'm using Swing

Thanks.

BCarpe
  • 860
  • 10
  • 27
  • I guess I should try creating SSCCE's before posting. In the process of trying to strip my program down to an SSCCE, I found the problem: I had set one of my labels to be opaque, when I shouldn't have. Removing the line which set it to opaque solved the problem, though it doesn't really answer the question I posted. – BCarpe Apr 18 '11 at 18:51
  • 3
    @BCarpe, that is the purpose of an SSCCE. Building it will either reveal the problem or give you something small enough that others can quickly play with it. – jzd Apr 18 '11 at 19:14
  • how do you do this: "The large panel contains several layers of labels which I render myself."? – MeBigFatGuy Apr 18 '11 at 20:23
  • Probably the same problem like what you are facing http://stackoverflow.com/questions/5615449/java-graphics-each-time-it-repaints-i-get-a-blackflash – eee Apr 19 '11 at 01:48
  • @MeBigFatGuy, The large panel is a JPanel, to which I added a JLayeredPane. Each of the layers is a class extended from JLabel with an overridden paintComponent method. The layers are added to the JLayeredPane. – BCarpe Apr 19 '11 at 13:29

1 Answers1

1

To answer your question (after you found a solution by the comments): Swing has some quite elaborate repaint management built in. When a tooltip disappears, the rectangle below it is repainted.

Now, which components have to be repainted? All those who overlap with the given rectangle, and are not themselves hidden (in the region in question) by other components - but only opaque components count here. (This is the whole reason we need the opaque property on JComponent - to optimize repainting.)

Your label declared itself being opaque, but did not really paint its whole area on a paintComponent, and such the region of the tooltip which should have been covered by the label stayed unpainted.

Declaring your label to be partly transparent caused also the concerning region of the component behind it to be repainted.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
  • Okay, well that gives me all of the information I really need. So thank you, Paŭlo. But just to be thorough in answering the question, is there a way to specifically call repaint() when a tooltip disappears? Or would trying to do such a thing just indicate poor programming practice? – BCarpe Apr 19 '11 at 13:37
  • From reading the documentation of `JToolTip`, you would have to subclass your tooltip showing JLabels and override their `createToolTip` method. Then you could either add some listener (maybe ComponentListener?) to your tooltip objects or override their `removeNotify` method to call then `repaint` on the right objects. This seems more complicated, and it will not completely solve the problem, as your repainting problems also could appear when your window is overlayed by other windows, I think. Also, the usual repainting will only repaint the defected rectangle, not everything. – Paŭlo Ebermann Apr 19 '11 at 13:47