Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JLabel issues  RSS feed

 
Wayne Sheets
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the deal. I have two JLabels. One of them is a large image.

When I click on the smaller label, it is 'glued' to the mouse. Meaning it moves to where ever the mouse pointer takes it.
When the mouse is clicked again, the label is 'unglued' and stays where it was dropped.

The idea is that this small label needs to be dropped onto the large label.
The problem is that the small label is behind the large label. I cant seem to bring it to the front.

I'm not using a layout manager (setLayout(null)).

Any ideas?

Thanks.
 
Roberto Perillo
Bartender
Posts: 2273
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings, Wayne. Welcome to JavaRanch!

Please CarefullyChooseOneForum when you post a question. Posting your question to the right forum helps keep it well organized, and everybody can benefit from the discussion. Since this has more to do with Java Swing, let's slide it over there
 
Wayne Sheets
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roberto Perillo wrote:Greetings, Wayne. Welcome to JavaRanch!

Please CarefullyChooseOneForum when you post a question. Posting your question to the right forum helps keep it well organized, and everybody can benefit from the discussion. Since this has more to do with Java Swing, let's slide it over there


Understood. Thanks.
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add your larger JLabel first, then the smaller. The last added component is drawn over the previously added component.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The last added component is drawn over the previously added component.

Sorry, Rob, it's the other way round. The first added Component is painted on top of any added later. That can be seen in the SSCCE that follows. However, even this behavior, though not in the least bit likely to ever change, is undocumented.

The recommended way to achieve desired stacking, or Z-Order is via the method setComponentZOrder(...).To see the desired stacking order, uncomment lines 56 and 57.
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:
The last added component is drawn over the previously added component.

Sorry, Rob, it's the other way round. The first added Component is painted on top of any added later. That can be seen in the SSCCE that follows. However, even this behavior, though not in the least bit likely to ever change, is undocumented.

Huh, I could have sworn I've seen it work the other way around. But I just checked the source of JComponent and it does indeed go through the children backwards when painting.
 
Wayne Sheets
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answers guys. It helps a lot!

[EDIT] A forum that links grammatically incorrect words! Awesome!
 
Campbell Ritchie
Marshal
Posts: 55677
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wayne Sheets wrote: . . . a lot! . . . A forum that links grammatically incorrect words! Awesome!
Not after you correct the error. That is, by the way, the only grammatical error we flag like that.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!