• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

Swing: JDialog

I have a class that extends JDialog. I want to pop up an instance of it when the user clicks on particular cell of a table. I know how to determine which cell, but not sure how I can get the JDialog to pop up.

If possible, please provide some sample code.
0
dshrenik
Asked:
dshrenik
  • 12
  • 5
1 Solution
 
for_yanCommented:
in your action method

you just intstntiate your class which extends JDialog and make it visible

MyDialog md = new MyDialog(....);
md.setVisible(true);

0
 
for_yanCommented:
If you have setVisible(true)
call inside your constructor, then

just instatitaion will be eenough
0
 
for_yanCommented:
But your class should extend JDialog and usually use super(...) as the first statement in the constructor
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
for_yanCommented:
Say, in your class


class MyDialog extends JDialog {

MyDialog(JFrame parent, String title, boolean modal){
super(parent, title, modal);

....


}

}
0
 
for_yanCommented:
In you parent JFrame where you call this constructor, say in some actionPerformed method

public void actionPerformed(AxctionEvent ae){
if(ae.getSource().equals(mytable){

//check if your row and column are OK

MyDialog md = new MyDialog(this, "My new dialog", true);


}

The third parameter boolean modal - if it is true then the dialog
would be modal - it will require that you finish
work with it uintil you can return back to working with your frame




}


}
0
 
dshrenikAuthor Commented:
Can you tell me when actionPerformed() is called?
0
 
for_yanCommented:
I think with JTable it is simple to use MouseListener and then
there is a method when you chek at which row and column
mouse was clicked relative to JTable
Just a moment I should fine a sample code for that
0
 
dshrenikAuthor Commented:
I have completed the MouseListener part. But, I am not able to make dialog pop up!
0
 
for_yanCommented:
First put Systme.out.println("something") in your event code - make sure that your event is being fired
0
 
for_yanCommented:
If it is firing, then probably if you can post your code for the class which extends JDialog
and also your enevt handling code
0
 
dshrenikAuthor Commented:
Mouse listener is working fine. But actionPerformed() is not being called when I click on a table cell.
I checked by doing System.out.prinltn.
0
 
for_yanCommented:
But you don't need both

If your mouselsitener method is working you don't need actionPerformed - I jsut used it for example.

put this in
MyDialog md = new MyDialog(this, "My new dialog", true);

mouseClicked(MoiuseEvent)

methiod
0
 
for_yanCommented:
No matter in what method it executes this line it should show the dialog:

MyDialog md = new MyDialog(this, "My new dialog", true);

and if you have mose event fired and if you can in your mouseListener
check which cell of your table was clicked,
then if you see that required cell was clicke just instantiate our dialog

I just used actionPerformed() as an exmaple of any ebvent hanlder
0
 
dshrenikAuthor Commented:
My JDialog constructor takes the argument "JFrame parent"

I am trying to create an instance of the JDialog by passing the argument:
(JFrame) SwingUtilities.getAncestorOfClass(JFrame.class,this)
But, this is causing an error.
0
 
for_yanCommented:
Why are you doing it so complex - why not to pass
just
this

Is your Jtable sitting in JFrame?
0
 
dshrenikAuthor Commented:
Sorry it works now. I just created a private method for the purpose of displaying the JDialog.
It works now!
Thanks!
0
 
for_yanCommented:
Good!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 12
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now