Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By DeNiS_M

Thread: How can i close a Jframe window using a jbutton applied in my program?

  1. #1
    epsi2430 is offline Member
    Join Date
    Sep 2011
    Location
    Philippines
    Posts
    6
    Rep Power
    0

    Default How can i close a Jframe window using a jbutton applied in my program?

    here is my code., pls help me.,



    import java.awt.Color;
    import java.awt.Cursor;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;


    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JButton;


    public class MyWindow {

    private static JPanel myPanel () {

    final JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(6,2));
    panel.setBackground(Color.green);

    panel.add(new JLabel("Rf:")); // Add JLabel object
    final JTextField textField1 = new JTextField ();
    panel.add(textField1); // Add JTextField object

    panel.add(new JLabel("Ri:")); // Add JLabel object
    final JTextField textField2 = new JTextField ();
    panel.add(textField2); // Add JTextField object

    panel.add(new JLabel("Vi")); // Add JLabel object
    final JTextField textField3 = new JTextField ();
    panel.add(textField3); // Add JTextField object

    panel.add(new JLabel("Vo")); // Add JLabel object
    final JTextField textField4 = new JTextField ();
    panel.add(textField4); // Add JTextField object
    textField4.setEditable(false);

    JButton button = new JButton("Compute me");
    panel.add(button); // Add JButtonl object

    JButton clear = new JButton("Clear me");
    panel.add(clear); // Add JButton2 object

    JButton cancel = new JButton("Cancel me");
    panel.add(cancel); // Add JButton3 object

    button.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
    double rf= Double.parseDouble (textField1.getText());
    double ri= Double.parseDouble (textField2.getText());
    double vi= Double.parseDouble (textField3.getText());
    textField4.setText(getVout(rf, ri, vi));
    }

    String getVout (double rf, double ri, double vi){
    return String.valueOf(vi*(1+(rf/ri)));
    }

    });

    clear.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
    textField1.setText("");
    textField2.setText("");
    textField3.setText("");
    textField4.setText("");
    panel.setBackground(Color.cyan);
    }
    });

    cancel.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {

    //** THIS NEEDS TO BE RESOLVED**

    }



    });

    return panel;

    }

    public static void main(String[] args) {

    JFrame MyWindow = new JFrame("Non Inverting");
    MyWindow.add(myPanel ()); // Add JPanel component to handle other swing objects
    int windowWidth = 600; // Window width in pixels
    int windowHeight = 200; // Window height in pixels
    MyWindow.setBounds(50, 100, // Set position
    windowWidth, windowHeight); // and size
    ;
    MyWindow.setVisible(true); // Display the window
    }


    }

    tnx..

  2. #2
    DeNiS_M is offline Member
    Join Date
    Sep 2011
    Location
    Athens Greece
    Posts
    29
    Rep Power
    0

    Default Re: How can i close a Jframe window using a jbutton applied in my program?

    MyWindow.setDefaultCloseOperation(JFrame.EXIT_ON_C LOSE);

    And next time use code tags please :)

  3. #3
    epsi2430 is offline Member
    Join Date
    Sep 2011
    Location
    Philippines
    Posts
    6
    Rep Power
    0

    Default Re: How can i close a Jframe window using a jbutton applied in my program?

    thank you for the response sir., but i had already try that code and there is still a red line under the "setDefaultCloseOperation". i had try to follow the recommendations by java but still the jframe window does not close when i click the cancel button., what should i do? tnx sir

  4. #4
    DeNiS_M is offline Member
    Join Date
    Sep 2011
    Location
    Athens Greece
    Posts
    29
    Rep Power
    0

    Default Re: How can i close a Jframe window using a jbutton applied in my program?

    Could please post what you have written
    Because i've tried it and it works fine!

  5. #5
    epsi2430 is offline Member
    Join Date
    Sep 2011
    Location
    Philippines
    Posts
    6
    Rep Power
    0

    Default Re: How can i close a Jframe window using a jbutton applied in my program?

    import java.awt.Color;
    import java.awt.Cursor;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;


    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JButton;


    public class MyWindow {

    private static JPanel myPanel () {

    final JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(6,2));
    panel.setBackground(Color.green);

    panel.add(new JLabel("Rf:")); // Add JLabel object
    final JTextField textField1 = new JTextField ();
    panel.add(textField1); // Add JTextField object

    panel.add(new JLabel("Ri:")); // Add JLabel object
    final JTextField textField2 = new JTextField ();
    panel.add(textField2); // Add JTextField object

    panel.add(new JLabel("Vi")); // Add JLabel object
    final JTextField textField3 = new JTextField ();
    panel.add(textField3); // Add JTextField object

    panel.add(new JLabel("Vo")); // Add JLabel object
    final JTextField textField4 = new JTextField ();
    panel.add(textField4); // Add JTextField object
    textField4.setEditable(false);

    JButton button = new JButton("Compute me");
    panel.add(button); // Add JButtonl object

    JButton clear = new JButton("Clear me");
    panel.add(clear); // Add JButton2 object

    JButton cancel = new JButton("Cancel me");
    panel.add(cancel); // Add JButton3 object

    button.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
    double rf= Double.parseDouble (textField1.getText());
    double ri= Double.parseDouble (textField2.getText());
    double vi= Double.parseDouble (textField3.getText());
    textField4.setText(getVout(rf, ri, vi));
    }

    String getVout (double rf, double ri, double vi){
    return String.valueOf(vi*(1+(rf/ri)));
    }

    });

    clear.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
    textField1.setText("");
    textField2.setText("");
    textField3.setText("");
    textField4.setText("");
    panel.setBackground(Color.cyan);
    }
    });

    cancel.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {

    MyWindow.setDefaultCloseOperation(JFrame.EXIT_ON_C LOSE);

    }
    });

    return panel;

    }

    public static void main(String[] args) {

    JFrame MyWindow = new JFrame("Non Inverting");
    MyWindow.add(myPanel ()); // Add JPanel component to handle other swing objects
    int windowWidth = 600; // Window width in pixels
    int windowHeight = 200; // Window height in pixels
    MyWindow.setBounds(50, 100, // Set position
    windowWidth, windowHeight); // and size
    MyWindow.setDefaultCloseOperation(JFrame.EXIT_ON_C LOSE);
    MyWindow.setVisible(true); // Display the window
    }


    }

    thank you sir

  6. #6
    DeNiS_M is offline Member
    Join Date
    Sep 2011
    Location
    Athens Greece
    Posts
    29
    Rep Power
    0

    Default Re: How can i close a Jframe window using a jbutton applied in my program?

    You have written JFrame.EXIT_ON_C LOSE it should be JFrame.EXIT_ON_CLOSE
    Last edited by DeNiS_M; 09-17-2011 at 11:27 AM.

  7. #7
    epsi2430 is offline Member
    Join Date
    Sep 2011
    Location
    Philippines
    Posts
    6
    Rep Power
    0

    Default Re: How can i close a Jframe window using a jbutton applied in my program?

    where should i encode the "MyWindow.setDefaultCloseOperation(JFrame.EXIT_ON_ CLOSE);" ?

    if i encode it in the main method, there is nothing wrong with the program but still the "Cancel me" button, when i click it., it does not close the Jframe window.,

    and if i encode it in the

    cancel.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {

    MyWindow.setDefaultCloseOperation(JFrame.EXIT_ON_C LOSE); //*<-- Here

    }
    });

    return panel;

    }


    there comes my another problem., what should i do? tnx again sir

  8. #8
    epsi2430 is offline Member
    Join Date
    Sep 2011
    Location
    Philippines
    Posts
    6
    Rep Power
    0

    Default Re: How can i close a Jframe window using a jbutton applied in my program?

    i correctly spelled thE CLOSE in my program

  9. #9
    DeNiS_M is offline Member
    Join Date
    Sep 2011
    Location
    Athens Greece
    Posts
    29
    Rep Power
    0

    Default Re: How can i close a Jframe window using a jbutton applied in my program?

    OK let me explain the setDefaultCloseOperation(JFrame.EXIT_ON_ CLOSE) sets the behaviour of the window componet so it would know what to do when the user initiates close operation.
    The "default" close operation a user may issue is clicking the X button which you did and as you wrote the window closed.

    Now you want the cancel button to perform the same operation(That is close the window) so in your actionListener implementation
    you have nothing else to do but add the System.exit() method.

    There is no use calling setDefaultCloseOperation() because the property of the class has already been set!

    Hope i helped
    Last edited by DeNiS_M; 09-17-2011 at 11:53 AM.
    epsi2430 likes this.

  10. #10
    epsi2430 is offline Member
    Join Date
    Sep 2011
    Location
    Philippines
    Posts
    6
    Rep Power
    0

    Default Re: How can i close a Jframe window using a jbutton applied in my program?

    sir thank you very much!!! for the additional information you had given me, i do not know that there is a method called System.exit()., i appreciate the time and effort you did for me sir., thank you very much., now my program is running from the way i wish it to run., hope you will help me sir in my future problems., :) God Bless., Salamat :)

  11. #11
    DeNiS_M is offline Member
    Join Date
    Sep 2011
    Location
    Athens Greece
    Posts
    29
    Rep Power
    0

    Default Re: How can i close a Jframe window using a jbutton applied in my program?

    Thank you for your kind words.
    fyi System.exit() terminates your application.
    It's like the exit() function in C (if you happen to know).

Similar Threads

  1. Can't close JFrame
    By dunafrothint in forum AWT / Swing
    Replies: 5
    Last Post: 12-16-2009, 05:00 PM
  2. Calling a JFrame window from a command line program.
    By new_2_java in forum New To Java
    Replies: 7
    Last Post: 11-09-2008, 03:40 AM
  3. Close Active Window (?)
    By dcnorman07 in forum New To Java
    Replies: 2
    Last Post: 10-03-2008, 05:55 PM
  4. How to close an open JFrame window from a jsp page?
    By kasisaiganesh in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 05-27-2008, 06:29 PM
  5. How to close a JFrame
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 05:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •