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