Solved

Swing: JDialog

Posted on 2011-09-21
17
239 Views
Last Modified: 2012-05-12
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
Comment
Question by:dshrenik
  • 12
  • 5
17 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36577210
in your action method

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

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

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36577216
If you have setVisible(true)
call inside your constructor, then

just instatitaion will be eenough
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36577225
But your class should extend JDialog and usually use super(...) as the first statement in the constructor
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 47

Expert Comment

by:for_yan
ID: 36577229
Say, in your class


class MyDialog extends JDialog {

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

....


}

}
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36577242
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
 

Author Comment

by:dshrenik
ID: 36577264
Can you tell me when actionPerformed() is called?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36577270
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
 

Author Comment

by:dshrenik
ID: 36577274
I have completed the MouseListener part. But, I am not able to make dialog pop up!
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36577280
First put Systme.out.println("something") in your event code - make sure that your event is being fired
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36577289
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
 

Author Comment

by:dshrenik
ID: 36577292
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
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36577297
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36577305
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
 

Author Comment

by:dshrenik
ID: 36577315
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36577328
Why are you doing it so complex - why not to pass
just
this

Is your Jtable sitting in JFrame?
0
 

Author Comment

by:dshrenik
ID: 36577332
Sorry it works now. I just created a private method for the purpose of displaying the JDialog.
It works now!
Thanks!
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36577338
Good!
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 55
Spring Framework HTTPSession management 1 34
Way to decrease size of apk file 9 67
configure dependency in POM for new database 3 25
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question