WindowListener not working?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: WindowListener not working?

  1. #1
    Join Date
    Nov 2004
    Posts
    3

    WindowListener not working?

    Can someone please tell me what i'm doing wrong here? The program is pretty simple. It only writes input to a file. The problem is that the WindowListener is not working like I expected. Thanks for any help.



    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.*;
    import java.util.*;

    public class BillPayer extends JFrame implements ActionListener
    {
    //Declare output stream
    DataOutputStream output;

    //Construct a panel for each row
    JPanel firstRow = new JPanel();
    JPanel secondRow = new JPanel();
    JPanel thirdRow = new JPanel();
    JPanel fourthRow = new JPanel();
    JPanel fifthRow = new JPanel();
    JPanel sixthRow = new JPanel();
    JPanel seventhRow = new JPanel();
    JPanel eighthRow = new JPanel();

    //construct a panel for the fields and buttons
    JPanel fieldPanel = new JPanel();
    JPanel buttonPanel = new JPanel();

    //construct labels and text boxes
    JLabel acctNumLabel = new JLabel("Account Number: ");
    JTextField acctNum = new JTextField(15);
    JLabel pmtLabel = new JLabel("Payment Amount:");
    JTextField pmt = new JTextField(10);
    JLabel firstNameLabel = new JLabel("First Name: ");
    JTextField firstName = new JTextField(10);
    JLabel lastNameLabel = new JLabel("Last Name:");
    JTextField lastName = new JTextField(20);
    JLabel addressLabel = new JLabel("Address:");
    JTextField address = new JTextField(35);
    JLabel cityLabel = new JLabel("City: ");
    JTextField city = new JTextField(10);
    JLabel stateLabel = new JLabel("State: ");
    JTextField state = new JTextField(2);
    JLabel zipLabel = new JLabel("Zip:");
    JTextField zip = new JTextField(9);

    //construct button
    JButton submitButton = new JButton("Submit");

    public static void main(String[] args)
    {
    //set the look and feel of the interface
    try
    {
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");

    }
    catch(Exception e)
    {
    JOptionPane.showMessageDialog(null,"The UIManager could not set the Look and Feel for this application.","Error",JOptionPane.INFORMATION_MESSAGE);
    }

    BillPayer f = new BillPayer();
    f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    f.setSize(450,300);
    f.setTitle("Crandall Power and Light Customer Payments");
    f.setResizable(false);
    f.setLocation(200,200);
    f.setVisible(true);
    }

    public BillPayer()
    {
    Container c = getContentPane();
    c.setLayout((new BorderLayout()));
    fieldPanel.setLayout(new GridLayout(8,1));
    FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,5,3);
    firstRow.setLayout(rowSetup);
    secondRow.setLayout(rowSetup);
    thirdRow.setLayout(rowSetup);
    fourthRow.setLayout(rowSetup);
    fifthRow.setLayout(rowSetup);
    sixthRow.setLayout(rowSetup);
    seventhRow.setLayout(rowSetup);
    eighthRow.setLayout(rowSetup);
    buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));


    //add fields to rows
    firstRow.add(acctNumLabel);
    firstRow.add(pmtLabel);

    secondRow.add(acctNum);
    secondRow.add(pmt);

    thirdRow.add(firstNameLabel);
    thirdRow.add(lastNameLabel);
    thirdRow.add(lastNameLabel);

    fourthRow.add(firstName);
    fourthRow.add(lastName);

    fifthRow.add(addressLabel);

    sixthRow.add(address);

    seventhRow.add(cityLabel);
    seventhRow.add(stateLabel);
    seventhRow.add(zipLabel);

    eighthRow.add(city);
    eighthRow.add(state);
    eighthRow.add(zip);

    //add rows to panel
    fieldPanel.add(firstRow);
    fieldPanel.add(secondRow);
    fieldPanel.add(thirdRow);
    fieldPanel.add(fourthRow);
    fieldPanel.add(fifthRow);
    fieldPanel.add(sixthRow);
    fieldPanel.add(seventhRow);
    fieldPanel.add(eighthRow);

    //add button to panel
    buttonPanel.add(submitButton);

    //add panels to frame
    c.add(fieldPanel, BorderLayout.CENTER);
    c.add(buttonPanel, BorderLayout.SOUTH);

    //add functionality to buttons
    submitButton.addActionListener(this);

    //get the current date and open the file
    Date today = new Date();
    SimpleDateFormat myFormat = new SimpleDateFormat("MMddyyyy");
    String filename = "payments" + myFormat.format(today);

    try
    {
    output = new DataOutputStream(new FileOutputStream(filename));
    }
    catch(IOException io)
    {
    JOptionPane.showMessageDialog(null,"The program could not create a storage location. Please check the disk drive and then run the program again.","Error",JOptionPane.INFORMATION_MESSAGE);

    System.exit(1);
    }

    addWindowListener(
    new WindowAdapter()
    {
    public void windowClising(WindowEvent e)
    {
    int answer = JOptionPane.showConfirmDialog(null,"Are you sure you want to exit and submit the file?","File Submission",JOptionPane.YES_NO_OPTION);
    if (answer == JOptionPane.YES_OPTION)
    System.exit(0);
    }
    }
    );
    }

    public void actionPerformed(ActionEvent e)
    {
    String arg = e.getActionCommand();

    if (checkFields())
    {
    try
    {
    output.writeUTF(acctNum.getText());
    output.writeUTF(pmt.getText());
    output.writeUTF(firstName.getText());
    output.writeUTF(lastName.getText());
    output.writeUTF(address.getText());
    output.writeUTF(city.getText());
    output.writeUTF(state.getText());
    output.writeUTF(zip.getText());

    JOptionPane.showMessageDialog(null,"The payment information has been saved.","Submission Successful",JOptionPane.INFORMATION_MESSAGE);
    }
    catch(IOException c)
    {
    System.exit(1);
    }
    clearFields();
    }
    }

    public boolean checkFields()
    {
    if ((acctNum.getText().compareTo("")<1) ||
    (pmt.getText().compareTo("")<1) ||
    (firstName.getText().compareTo("")<1) ||
    (lastName.getText().compareTo("")<1) ||
    (address.getText().compareTo("")<1) ||
    (city.getText().compareTo("")<1) ||
    (state.getText().compareTo("")<1) ||
    (zip.getText().compareTo("")<1))
    {
    JOptionPane.showMessageDialog(null,"You must complete all fields.","Data Entry Error",JOptionPane.WARNING_MESSAGE);
    return false;
    }
    else
    {
    return true;
    }
    }

    public void clearFields()
    {
    //clear fields and reset the focus
    acctNum.setText("");
    pmt.setText("");
    firstName.setText("");
    lastName.setText("");
    address.setText("");
    city.setText("");
    state.setText("");
    zip.setText("");
    acctNum.requestFocus();
    }
    }

  2. #2
    Join Date
    Sep 2004
    Posts
    223
    ok so whats wrong with it? What doesnt it do?
    A kram a day keeps the doctor......guessing

  3. #3
    Join Date
    Nov 2004
    Posts
    3
    I expected this part of the code to prompt the user for a yes or no (option to exit when the close button is pressed). This was taken straight from a book, so I dont understand why it doesnt work. Is it in the wrong place? Here is the code:

    addWindowListener(
    new WindowAdapter()
    {
    public void windowClising(WindowEvent e)
    {
    int answer = JOptionPane.showConfirmDialog(null,"Are you sure you want to exit and submit the file?","File Submission",JOptionPane.YES_NO_OPTION);
    if (answer == JOptionPane.YES_OPTION)
    System.exit(0);

  4. #4
    Join Date
    Feb 2004
    Posts
    541
    You spelt closing wrong, so you haven't overided the "windowClosing() method. You created an entirely new method, that will never be called.

  5. #5
    Join Date
    Nov 2004
    Posts
    3
    Originally posted by mikeBarr81
    You spelt closing wrong, so you haven't overided the "windowClosing() method. You created an entirely new method, that will never be called.
    Thank you very much for the help. Thats pretty emberrasing considering the amount of time I spent on this trying to figure out what was wrong.

  6. #6
    Join Date
    Feb 2004
    Posts
    541
    Thats ok, i've made plenty of spelling mistakes before too

  7. #7
    Join Date
    Feb 2004
    Posts
    541
    Oh, remember to set the window to "DO_NOTHING_ON_CLOSE", or it will go ahead and close the window even if the user says they don't want it closing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center