954,123 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Bring component forward.

Hi! I have a bunch of Jbuttons which are scattered over each other. Is there a method which will bring the button the user selected to the front of the pack?

Thanks!

MrScruff
Junior Poster in Training
89 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

Hi everyone,

You can do this

JComponent.grabFocus();


Richard West

freesoft_2000
Practically a Master Poster
623 posts since Jun 2004
Reputation Points: 25
Solved Threads: 10
 

Ta for the info! But it didn't work :(

MrScruff
Junior Poster in Training
89 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

Hi everyone,

Well in that case i not sure how to do it

Richard West

freesoft_2000
Practically a Master Poster
623 posts since Jun 2004
Reputation Points: 25
Solved Threads: 10
 

Why are the buttons on top of each other in the first place?

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

Hi everyone,

Good question server crash. That seems like bad layout programming to me or am i missing something.

p.s - check out my new signature

Richard West

freesoft_2000
Practically a Master Poster
623 posts since Jun 2004
Reputation Points: 25
Solved Threads: 10
 

You guys are missing something ;) I am creating a scatter field, each star being a button (1700 of them to be exact). Therefor the buttons are placed depending on their x,y attributes and can overlay each other. The user can move sliders to make buttons dissapear to make the scatterfield more accurate towards their query. But when they haven't got a query it would be nice for the buttons to move forward infront of the pack if the user clicks on them. Thanks for the help though :D

MrScruff
Junior Poster in Training
89 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

I'm not sure how to do that, altought it could be possible. My only worry, would be that it depends on the order in which you added them to a container, and the buttons added last got put on top. I'll be searching the internet for a solution and get back to you if I find one.

p.s - check out my new signature

Looks good, but I think you should bold the part at the bottom if you want more traffic..

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

Thats great thanks. Yeah i was thinking that i might be able to remove and add the button when the user clicks it - might flicker. I will try it anyway.

MrScruff
Junior Poster in Training
89 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

Sorry for the double post, Im not sure how to edit the above message?

I tried the above and it kinda worked. However other buttons moved around it and it wasn't a very reliable method.

MrScruff
Junior Poster in Training
89 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

//please try inside your MouseListener:

pubilc void mousePressed(MouseEvent e){
Component comp = e.getComponent();

if (comp.getClass().getName().equals(java.awt.Button)){

comp.getParent(). setComponentZOrder(
comp, 0);
}
}

//I hope this helps.

brah
Newbie Poster
2 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

You do realise that this thread was nearly 3 years old, right?

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

I do. I was unable to find another 'how-to' for this problem, so when I found the answer I decided to post it. I hope it was useful, but I understand better solutions may be out there. Please let me know if you know of one--I would surely benefit from it.

brah
Newbie Poster
2 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 
I do. I was unable to find another 'how-to' for this problem, so when I found the answer I decided to post it. I hope it was useful, but I understand better solutions may be out there. Please let me know if you know of one--I would surely benefit from it.


Well, you can use instanceof instead of having to drill down to the classname,

if (comp instanceof java.awt.Button){


but if you only add this listener to the buttons then that check is unnecessary - you already know it's a button.

Just two minor points there though. Your solution is just fine.

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 
I do. I was unable to find another 'how-to' for this problem, so when I found the answer I decided to post it. I hope it was useful, but I understand better solutions may be out there. Please let me know if you know of one--I would surely benefit from it.


Okay, just wanted to make sure. A lot of new posters here come through and start answering years old questions (usually that have already been satisfactorily answered and they, many times, answer them wrongly) and just dig up a bunch of old threads that the "older" posters consider finished, and it many times just raises some irritation.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You