I have a JDialog with buttons, and I want to close the dialog when I press them. I search in Google, but the code I could find is not working for me. I'm trying this inside the method which displays the dialog:

but the compiler raises an error because it can not find setVisible or dispose methods. Thanks in advance

11-13-2009, 11:39 PM

Fubarable

setVisible and dispose need to be called on the JDialog and right now you're calling it on whatever class happens to hold this method (we currently have no idea what class this is). One way to get your JDialog is to get the source object from the ActionCommand object via WindowsUtilities.getWindowAncestor, and then calling dispose on this.

You are right, that was the problem. I solved it by creating a new class called CustomDialog, and inside that class setVisible method can be used with no problem. I have two last questions: why do you write the comment "//!! why Object???" and how can I set the position of the Dialog in order to put it in the center of the parent frame ? Thank you very much for the quick answer.

11-15-2009, 12:33 AM

aaroncarpet

if you look in the api JDialog inherits alot of methods ......so think about the math first

if you know the width and height of the parent window....

if you know the widthand height of the dialog....

if you subtract the width of the dialog from the width of parent window and divide by 2 ...

Just that. Why use a non-specific Object for the parameter type, forcing you to cast the parameter as an ActionEvent rather than just use ActionEvent from the get-go. If you want two similar methods, one using a String the other an ActionEvent, then create two overloaded methods.

Quote:

and how can I set the position of the Dialog in order to put it in the center of the parent frame ?

Much simpler than the post above is to just set the dialog's location to be relative to the JFrame via the setLocationRelativeTo(...). For example:

but it didn't work. I've read in the Java reference that sometimes the method setLocation doesn't work because it depends on the operating system. Could be that the problem?

With the second method, I simply add the line setLocationRelativeTo(frame) and it works, but not in the way I expected, because it sets in the center of the frame the top left corner of the dialog, but not the whole dialog. Thanks again for both answers.

11-16-2009, 06:28 PM

Fubarable

Quote:

Originally Posted by sky

I've tried both methods. For the first one, I wrote this code in the class that extends JDialog:

but it didn't work. I've read in the Java reference that sometimes the method setLocation doesn't work because it depends on the operating system. Could be that the problem?

With the second method, I simply add the line setLocationRelativeTo(frame) and it works, but not in the way I expected, because it sets in the center of the frame the top left corner of the dialog, but not the whole dialog. Thanks again for both answers.

Without seeing compilable code, it's very hard to guess where your error is,... but it's certainly not in the code you've posted. I can take a wild guess, such that you're calling setLocationRelativeTo before calling pack, but again, who knows?

11-16-2009, 07:01 PM

sky

You were right again. I was calling setLocationRelativeTo() before calling pack(). Summing up: