tags:

views:

114

answers:

2

A panel contains two labels which are generated by mouse clicks on the panel. I want to connect these two labels by drawing a line between them probably by dragging the mouse pointer from one label to another.

There are two events here - one is clicking on the panel to generate the labels and the second is connecting these two labels (mouse pressed, mouse dragged and mouse released events). Both these event need to call the repaint() method but for different purposes. But there can be only one paint() method. The problem is when I connect these two labels, the line comes up but the rest of the components on the panel disappear.

+3  A: 

That is probably due to the fact that you're overriding the panels paint() method. Override paintComponent() / paintComponents() instead. No matter if you're using paint or paintComponent, do not forget to call super.paint() or super.paintComponents() respectively.

Tedil
+1  A: 

You can use a JLayeredPane instead of JPanel to draw multiple objects above eachother.

You can add your original JPanel to the JLayeredPane, and then add another one, with a higher Z-index and the opaque property set to true. Then the highest panel can be easily repainted without the other, lower, panel to show weird things.

Pindatjuh