Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
    Bookmark Topic Watch Topic
  • New Topic

JDialog not closing on 1st click

 
suvelee sarpotdar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hello,

I am pretty new to Java and also to JavaRanch forum. I tried searching through the forum for my problem but couldn't find any solution. I have a parent frame with a button "+" along with some other buttons. Upon clicking this button, a JDialog is opened which takes some input from the user. Dialog has a cancel button along with the usual "X" button on the top. JDialog implements actionlistener which closes( dispose()) the JDialog on clicking cancel or "X" button. I also have a private inner class in the parent frame class which implements all the events for the parent frame.

My problem is that the JDialog closes on clicking the "X" or cancel button twice, i.e. the JDialog is not closed on the 1st click. Following is the code for both parent and the dialog:

//Parent Frame code
//------------------------------------------------------------------------
package ui;

import javax.swing.*;



import java.awt.*;
import java.awt.event.*;


public class CalendarGUI extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private static GraphicsConfiguration defaultGC = null;
private static String defaultTitle = "Untitled";
private static CalendarGUI cg;

public CalendarImage calImg;
public MonthView cal;
public CalendarToDo calToDo;

public NewEventForm newEvent;
private AllEventHandler ae;
/**
* @throws HeadlessException
*/
public CalendarGUI() throws HeadlessException {
this(defaultTitle, defaultGC);
}

/**
* @param gc Graphics configuration
*/
public CalendarGUI(GraphicsConfiguration gc) {
this(defaultTitle, gc);
}

/**
* @param title Window title
* @throws HeadlessException
*/
public CalendarGUI(String title) throws HeadlessException {
this(title, defaultGC);
}

/**
* @param title Window title
* @param gc Graphics configuration
*/
public CalendarGUI(String title, GraphicsConfiguration gc) {
super(title, gc);
initComponents();

}

public void initComponents(){
this.setLayout(new GridLayout(2,2));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ae = new AllEventHandler();
calImg = new CalendarImage();
cal = new MonthView();
cal.addActionListener(ae);
calToDo = new CalendarToDo();
this.getContentPane().add(calImg);
this.getContentPane().add(cal);
this.getContentPane().add(new JPanel());
this.getContentPane().add(calToDo);
this.setResizable(false);
this.pack();
this.setVisible(true);
}

public static void main(String[] agrs){
cg = new CalendarGUI("Calendar");

}

private class AllEventHandler implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object o = e.getSource();
if(o == cal.jbAddToDo){
newEvent = new NewEventForm(cg);
newEvent.requestFocus();
newEvent.setVisible(true);

}

}

}
}
//------------------------------------------------------------------------
//JDialog Code
package ui;
import business.Event;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class NewEventForm extends JDialog implements ActionListener{
private JLabel jlAddEvent;
private JLabel jlTitle;
private JLabel jlLocation;

private static GraphicsConfiguration defaultGC = null;
private static String defaultTitle = "Untitled";



public JTextField jtTitle;
public JTextField jtLocation;

public JButton jbCancel,jbDone;

/**
* @throws HeadlessException
*/
public NewEventForm(JFrame parent) throws HeadlessException {
super(parent,"New Event",true);
initComponents();
}

public void initComponents(){
if(getLayout() == null){
setLayout(new GridLayout(8,3,3,3));
}
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
Window owner = this.getOwner();
//this.setResizable(false);
Dimension d1 = this.getSize();
Dimension d2 = owner.getSize();
int x = Math.max((d2.width-d1.width)/2, 0);
int y = Math.max((d2.height-d1.height)/2, 0);
this.setBounds(x,y,d1.width,d1.height);
//this.setSize(200,200);
this.setModal(true);

JPanel form = new JPanel(new GridLayout(8,3,3,3));
jbCancel = new JButton("Cancel");
jbCancel.addActionListener(this);
form.add(jbCancel);
jlAddEvent = new JLabel("Add Event");
jlAddEvent.setHorizontalAlignment(JLabel.CENTER);
form.add(jlAddEvent);
jbDone = new JButton("Done");
jbDone.addActionListener(this);
form.add(jbDone);

jlTitle = new JLabel ("Title: ");
form.add(jlTitle);
jtTitle = new JTextField();
form.add(jtTitle);


jlLocation = new JLabel ("Location: ");
form.add(jlLocation);
jtLocation = new JTextField();
form.add(jtLocation);

this.getContentPane().add(form);
this.pack();
this.setVisible(true);


}


public void actionPerformed(ActionEvent ae){
Object o = (Object)ae.getSource();
if(o == jbCancel){
this.dispose();
} else if (o == jbDone){

Event e = new Event();

}
}

}


Is this happening because I have two actionlisteners? Should the inner class of parent frame handle the events of JDialog too?

Thanks a lot,
Suvelee
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please Use Code Tags.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Report post to moderator
the problem is here

newEvent = new NewEventForm(cg);
newEvent.requestFocus();
newEvent.setVisible(true);

NewEventForm is a modal dialog, so the requestFocus and setVisible lines are not executed until the dialog closes,
then, when closed, setVisible(true) is executed.

remove those requestFocus and setVisible lines and it should work OK.


[edit]
forgot to add that NewEventForm contains a setVisible(true) in it's constructor
 
suvelee sarpotdar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hello Michael,

Thanks a lot. It works perfectly when i remove those lines. Also i have one more question, is it good to handle the events related to the dialog within that dialog class itself or should the events be handled by inner class in the parent frame?

Suvelee
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Report post to moderator
> is it good to handle the events related to the dialog within that dialog class itself
> or should the events be handled by inner class in the parent frame?

depends, if a single action, I prefer an anonymous inner class added to the source
e.g. a 'Cancel' button, which closes the dialog

something more complex, or with numerous sources, could be a separate class,
contained with the dialog's class

something generic could be in its own class/package, and reusable
 
James Basller
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Report post to moderator
Michael Dunn wrote:the problem is here

newEvent = new NewEventForm(cg);
newEvent.requestFocus();
newEvent.setVisible(true);

NewEventForm is a modal dialog, so the requestFocus and setVisible lines are not executed until the dialog closes,
then, when closed, setVisible(true) is executed.

remove those requestFocus and setVisible lines and it should work OK.


[edit]
forgot to add that NewEventForm contains a setVisible(true) in it's constructor


great!!!

it works for me a lot!!!

Thank you so much!!! :)

Thanks
James
 
    Bookmark Topic Watch Topic
  • New Topic