• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding X Y location of button

 
joe vasher
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working with a JDialog box, that I want to appear in a certian pos on the calling JFrame window. I think I have figured out how to do this by setting bounds. But I'm having trouble finding the location of a button that I wish to make this dialog pop up below.

Its a JButton that I need to find location of.

I have tried button.getLocation() which only returns 0 x 0 y..


So thats How badly I'm confused. <SMILE

How do I get the x y location of a button?

I played with pasing the button to setLocationRelativeto(); and It would center my window over the button. Wish I could control that position. would make life simple..

Thanks






 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tested it now and it works!
Good luck,
cc11rocks aka John Price

EDIT(Link to Java JButton API):
http://download.oracle.com/javase/6/docs/api/javax/swing/JButton.html
 
Rob Spoor
Sheriff
Posts: 20819
68
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getLocation() returns the location relative to its parent container. To get a component's coordinates on the screen use getLocationOnScreen(), but beware:
Throws:
IllegalComponentStateException - if the component is not showing on the screen
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:but beware:
Throws:
IllegalComponentStateException - if the component is not showing on the screen

So just add a Exception "cacher".
try {
blahblahblahcode getLocationOnScreen()
}
catch (IllegalComponentStateException ex) {
ex.printStackTrace();
}
John Price aka cc11rocks
 
Darryl Burke
Bartender
Posts: 5153
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than program by exception, it makes more sense to test for isShowing() and do nothing if that returns false.

I rather suspect, though, that the OP wants to show the dialog on clicking the said button -- and that a javax.swing.Popup may be a better solution.
 
joe vasher
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
john price wrote:
Tested it now and it works!
Good luck,
cc11rocks aka John Price

EDIT(Link to Java JButton API):
http://download.oracle.com/javase/6/docs/api/javax/swing/JButton.html


Ok, thanks, I went back in and tested this and it helped me figure out how I messed up. Guess you need to put the getLocation() after pack() or it will return 0..

thanks.
 
joe vasher
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:getLocation() returns the location relative to its parent container. To get a component's coordinates on the screen use getLocationOnScreen(), but beware:
Throws:
IllegalComponentStateException - if the component is not showing on the screen


thanks. This works well. The beware helped me, because I put it in the event handler so it gets updated when I go into my event handler. thought that is what you ment by beware. However Now I relize it was ment to displaying the window when main window didn't actually happen. However, I think this (If I understand what is going on correctly (which I never do)) is not a problem. because THe main window must display or I won't have a button to click on to call the popup window.?.?

thanks.
 
joe vasher
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Rather than program by exception, it makes more sense to test for isShowing() and do nothing if that returns false.

I rather suspect, though, that the OP wants to show the dialog on clicking the said button -- and that a javax.swing.Popup may be a better solution.


Just a quick question on the above point. if My button that I call the JDialog window from is press to open the window do I still need to check if the main window isShowing, or do you mean to check to see if the JDialog is showing (it is modal?)

I'm looking into the javax.swing.Popup window. It has some options I like over the JDialog, at least what I have found so far about it. I think the biggest of these would be the fact that I can click back in main window and the window goes away. But I will play with this first before I try and convert all my code to work with it. (actually, if I understand some of the code examples I found on the net, I might be able to just call my Dialog window as is and use the on focus events to to call the dialog window.


Thanks.
 
Rob Spoor
Sheriff
Posts: 20819
68
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you call the method from the button's own event handler, then under normal circumstances the button is visible. After all, how can you press a non-visible button? There may be one exception, when the button's doClick method is called, but unless you do that I wouldn't worry about it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!