• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How to set Z-order of a JLable which is contained in a JPanel

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I achieved a view A which extends from JLable, and I put it in a JPanel, Then, I add a line (which is extends from JPanel) to this JPanel, but the view A will overrides this line. like below,How could I draw the line over the view A?

 
Saloon Keeper
Posts: 3291
146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A way to draw your lines over existing components is to override paint(). In this override, call super.paint(g), and then draw your line.
Normally you would draw lines in an overridden paintComponent, but as you notice, later on the components are drawn, right over your lines. By first calling super.paint(g), you make sure that your line drawing takes place after the components have been drawn.
 
Jose Ariza
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:A way to draw your lines over existing components is to override paint(). In this override, call super.paint(g), and then draw your line.
Normally you would draw lines in an overridden paintComponent, but as you notice, later on the components are drawn, right over your lines. By first calling super.paint(g), you make sure that your line drawing takes place after the components have been drawn.


Thanks for the answer.I really did so, i make sure I draw A first and then draw the line, but it did't work.
 
Piet Souris
Saloon Keeper
Posts: 3291
146
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. Here is an example:

Now, if you want to see the difference, change 'paint' to 'paintComponent' in the above code.

To respond to the title:
you can ask for the Z-order of a component, that is easy, but I have been unable to find an easy way to SET the Z-order somehow. What I did in a program in which I could drag and resize JLabels at will, was to remove a JLabel, only to add it back again. That changes the Z-order and you can make a JLabel that's at the bottom appear on top.
 
Jose Ariza
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:Okay. Here is an example:

Now, if you want to see the difference, change 'paint' to 'paintComponent' in the above code.

To respond to the title:
you can ask for the Z-order of a component, that is easy, but I have been unable to find an easy way to SET the Z-order somehow. What I did in a program in which I could drag and resize JLabels at will, was to remove a JLabel, only to add it back again. That changes the Z-order and you can make a JLabel that's at the bottom appear on top.


Thanks for the program, this program runs well and I will look through it. As you mentioned, I really do resizing a component by remove then add it again.This time I want to achieve this by repaint,and I met this bug... your program is helpful!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!