windows event listener java

105 views Asked by At

I'm having a heck of a time trying to get this to work. I'm supposed to write to a csv file when the windows closes. The commented out code in the main method works. However, I'm trying to implement it in the GUI class, as the directions state, "the handler should be an object of an inner class that extends the WindowAdapter class." This previously asked question is similar

WindowAdapter In Inner Class

but I can't figure out how to make it work in my situation. My GUI class extends JFrame, so I tried to put:

this.addWindowsListener

But it results in that method right after actionPerformed, and I get no file. Any help would be greatly appreciated. Thank you.

package prj3amezquitar;

import java.awt.*;
import java.awt.event.*;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.*;
import java.io.*;

public class PRJ3AmezquitaR {



public static class GUI extends JFrame implements ActionListener{

    private JLabel nthLabel, resultLabel, efficiencyLabel;

    private JButton compute;

    private JRadioButton iterativeRadio, recursiveRadio;

    private JTextField enterValue, result, efficiency;

    private String method;

    private int userValue;

    private int[] valuePair = new int[2];

    private PrintWriter writer;

    private StringBuilder strings;

    public GUI(){

        //set the layout for the AtmMachine
        setLayout(new GridBagLayout ());

        //Creates an instance of the layout
        GridBagConstraints panel = new GridBagConstraints();

        // Establishes the pixels suronding each object within the layout
        panel.insets = new Insets (5, 5, 5, 5);

        //create button group for radio buttons


        iterativeRadio = new JRadioButton("Iterative");
        panel.fill = GridBagConstraints.HORIZONTAL;
        panel.gridx = 1;
        panel.gridy = 0;
        panel.gridwidth  = 1;
        iterativeRadio.setSelected(true);
        add(iterativeRadio, panel);

        recursiveRadio = new JRadioButton("Recursive");
        panel.fill = GridBagConstraints.HORIZONTAL;
        panel.gridx = 1;
        panel.gridy = 1;
        panel.gridwidth  = 1;
        add(recursiveRadio, panel);

        nthLabel = new JLabel("Enter n:");
        panel.fill = GridBagConstraints.HORIZONTAL;
        panel.gridx = 0;
        panel.gridy = 2;
        panel.gridwidth  = 1;
        add(nthLabel, panel);

        enterValue = new JTextField("Value");
        panel.fill = GridBagConstraints.HORIZONTAL;
        panel.gridx = 1;
        panel.gridy = 2;
        panel.gridwidth  = 1;
        add(enterValue, panel);

        compute = new JButton("Compute");
        panel.fill = GridBagConstraints.HORIZONTAL;
        panel.gridx = 1;
        panel.gridy = 3;
        panel.gridwidth = 1;
        add(compute, panel);

        resultLabel = new JLabel("Result");
        panel.fill = GridBagConstraints.HORIZONTAL;
        panel.gridx = 0;
        panel.gridy = 4;
        panel.gridwidth  = 1;
        add(resultLabel, panel);

        result = new JTextField("result");
        panel.fill = GridBagConstraints.HORIZONTAL;
        panel.gridx = 1;
        panel.gridy = 4;
        panel.gridwidth  = 1;
        result.setEditable(false);
        add(result, panel);

        efficiencyLabel = new JLabel("efficiency");
        panel.fill = GridBagConstraints.HORIZONTAL;
        panel.gridx = 0;
        panel.gridy = 5;
        panel.gridwidth  = 1;
        add(efficiencyLabel, panel);

        efficiency = new JTextField("efficiency");
        panel.fill = GridBagConstraints.HORIZONTAL;
        panel.gridx = 1;
        panel.gridy = 5;
        panel.gridwidth  = 1;
        efficiency.setEditable(false);
        add(efficiency, panel);

        ButtonGroup radios = new ButtonGroup();

        radios.add(iterativeRadio);

        radios.add(recursiveRadio);

        compute.addActionListener(this);


    }//end GUI constructor


    //action listener class
    public void actionPerformed(ActionEvent e){

        //try block for catching format errors
        try{

            userValue = Integer.parseInt(enterValue.getText());

            //if for radio button selection
            if(iterativeRadio.isSelected() == true){

                valuePair = Sequence.computeIterative(userValue - 2);

                //set reults box to the value of the index requested
                result.setText(Integer.toString(valuePair[0]));

                //set the efficiency field to the returned value of the counter
                efficiency.setText(Integer.toString(valuePair[1]));

            }//end if block

            //else block for recursive method
            else if (recursiveRadio.isSelected()){

                valuePair = Sequence.computeRecursive(userValue - 2);

                //set reults box to the value of the index requested
                result.setText(Integer.toString(valuePair[0]));

                //set the efficiency field to the returned value of the counter
                efficiency.setText(Integer.toString(valuePair[1]));

            }//end else block

        }//end try block

        //catch block for numberformat exeptions
        catch(NumberFormatException n){

            //pop up error for user
            JOptionPane.showMessageDialog( null, "Value entered was not an number.");

        }//end catch block



    }//end actionPerformed


    //attemt to add listener for closing window
    @Override
    public void addWindowListener(WindowListener wl) {
        super.addWindowListener(new WindowAdapter(){

            public void windowCloses(WindowEvent b){
                                    try{
                writer = new PrintWriter(new File("test.csv"));

                strings = new StringBuilder();

                strings.append("index");

                strings.append(",");

                strings.append("Iterative Efficiency");

                strings.append(",");

                strings.append("Recursive Efficiency");


                strings.append(",");

                strings.append("\n");

                writer.write(strings.toString());

                writer.close();

                }
                catch(IOException a){

                }
            }

        }); //To change body of generated methods, choose Tools | Templates.
    }



}//end GUI


//utility class for computational methods
public static final class Sequence{


    //variable declarations for values. previousValue is set to 1, as formula
    //does not truly begin until the 3rd elment of the array.
    private static int counter = 0;

    private static int addedValue = 0;

    private static int previousValue = 0;

    private static int currentValue = 1;

    private static int newCurrent = 0;

    //array to hold both value and efficiency
    private static int[] resultPair = new int[2];

    //List<int> words = new ArrayList<int>();

    //blank constructor
    private Sequence(){
    }

    //method using iterative process
    public static int[] computeIterative(int value){

         //if for a value of anything less than 0
        if (value < 0){

            resultPair[0] = 0;
            resultPair[1] = 1;

            return resultPair;
        }

        //else if for a value of 0
        else if (value == 0){

            resultPair[0]=1;
            resultPair[1]=2;
            return resultPair;
        }//end else if


        //iterative while loop to find requested value
        while (counter < value){

            newCurrent = previousValue + currentValue + currentValue;

            previousValue = currentValue;

            currentValue = newCurrent;

            counter++;

        }//end while loop

        resultPair[0] = currentValue;
        resultPair[1] = counter;

        //reset variables
        counter = 0;
        previousValue = 0;

        currentValue = 1;

        newCurrent = 0;

        //return the found value
        return resultPair;

    }//end iterative method

    public static int[] computeRecursive(int value){

        //if for a value of anything less than 0
        if (value < 0){

            resultPair[0] = 0;
            resultPair[1] = 1;

            return resultPair;
        }

        //else if for a value of 0
        else if (value == 0){

            resultPair[0]=1;
            resultPair[1]=2;
            return resultPair;
        }//end else if

        //if block for the first two elements, as the formulat does not
        //apply to these elements

        newCurrent = previousValue + currentValue + currentValue;

        previousValue = currentValue;

        currentValue = newCurrent;

        counter++;

        //condition for exiting recursion
        if(counter == value){

            resultPair[0] = currentValue;
            resultPair[1] = counter;
            counter = 0;
            previousValue = 0;

            currentValue = 1;

            newCurrent = 0;
            return resultPair;

        }//end exit condition



        return computeRecursive(value);

    }//end recursive method



}//end sequence class

public static void main(String[] args) {

    GUI Project3  = new GUI();
    /*        
    Project3.addWindowListener(new WindowAdapter(){

        private PrintWriter writer;

        private StringBuilder strings;

        @Override
            public void windowClosing(WindowEvent f){

                try{
                writer = new PrintWriter(new File("test.csv"));

                strings = new StringBuilder();

                strings.append("index");

                strings.append(",");

                strings.append("Iterative Efficiency");

                strings.append(",");

                strings.append("Recursive Efficiency");


                strings.append(",");

                strings.append("\n");

                writer.write(strings.toString());

                writer.close();

                }
                catch(IOException a){

                }

        }//end windows closing

    });

    */    

    Project3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Project3.setLocationRelativeTo(null);
    Project3.setVisible(true);
    Project3.setSize(450, 350);
    Project3.setTitle("Change the Balance");



}//end main method

}//end PRJ3AmezquitaR
0

There are 0 answers