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!

Recommended Answers

All 14 Replies

Hi everyone,

You can do this

JComponent.grabFocus();

Richard West

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

Hi everyone,

Well in that case i not sure how to do it

Richard West

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

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

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

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..

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.

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.

please try inside your MouseListener:

public 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.

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

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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.