userv
userv

Reputation: 2617

how to draw a line between two labels in a panel where labels are added during runtime

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.

Upvotes: 1

Views: 1439

Answers (2)

Pindatjuh
Pindatjuh

Reputation: 10526

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.

Upvotes: 2

Tedil
Tedil

Reputation: 1933

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.

Upvotes: 3

Related Questions