7 Replies - 424 Views - Last Post: 20 March 2013 - 10:53 PM Rate Topic: -----

#1 josephine11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-September 12

Cannot seem to get the JButton "exit" to work

Posted 20 March 2013 - 12:15 AM

I am having a sight problem, I cannot seem to see why my my exit button does not want to work... perhaps I need a pair of fresh eyes to see it...but any help would be really appreciated.

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

public class Transfer extends JFrame implements ActionListener
{
	//declare output stream
	DataOutputStream output;
	
	// construct rows
	JPanel firstRow = new JPanel();
	JPanel secondRow = new JPanel();
	JPanel thirdRow = new JPanel();
	JPanel fourthRow = new JPanel();
	
	//construct a panels
	JPanel fieldPanel = new JPanel();
	JPanel buttonPanel = new JPanel();
	
	//construct labels and text fields
	JLabel nameLabel = new JLabel("Name:                                       ");
		JTextField name = new JTextField(10);
	JLabel studentIdLabel = new JLabel("Student Id:                                ");
		JTextField studentId = new JTextField(10);
	JLabel transferCourseNumLabel = new JLabel("Transfer Course Number:    ");
		JTextField transferCourseNum = new JTextField(10);
	JLabel localCourseNumLabel = new JLabel("Local Course Number:          ");
		JTextField localCourseNum = new JTextField(10);
	
	//construct buttons
	JButton submitButton = new JButton("Submit");
	JButton exitButton = new JButton("Exit");
	
	public static void main(String[] args)
	{
		Transfer window = new Transfer();
		window.setTitle("New Transfer Student");
		window.setSize(300,200);
		window.setVisible(true);
	}
	
	public Transfer()
	{//start transfer meethod
		Container c = getContentPane();
		c.setLayout((new BorderLayout()));
		fieldPanel.setLayout(new GridLayout(4,2));
		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		
		//add fields to rows
		firstRow.add(nameLabel);
		firstRow.add(name);
		secondRow.add(studentIdLabel);
		secondRow.add(studentId);
		thirdRow.add(transferCourseNumLabel);
		thirdRow.add(transferCourseNum);
		fourthRow.add(localCourseNumLabel);
		fourthRow.add(localCourseNum);
		
		//add rows to panel
		fieldPanel.add(firstRow);
		fieldPanel.add(secondRow);	
		fieldPanel.add(thirdRow);
		fieldPanel.add(fourthRow);
		
		//add buttons to panel
		buttonPanel.add(submitButton);
		buttonPanel.add(exitButton);
		
		//add panels to frame
		c.add(fieldPanel, BorderLayout.EAST);
		c.add(buttonPanel, BorderLayout.SOUTH);
		
		//add funtinality to buttons
		submitButton.addActionListener(this);
		exitButton.addActionListener(this);
		
			try
			{
				output = new DataOutputStream(new FileOutputStream("Transfer.dat"));
			}
			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(0);
			}
		
			addWindowListener(new WindowAdapter()
				{
					public void windowClosing(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);
									}
				}
			);
		}//end transfer method
		
		public void actionPerformed(ActionEvent e)
		{//open action performed
			String arg = e.getActionCommand();
			
			if (arg == "Submit")
			
			{//open if
				try
				{
					output.writeUTF(name.getText());
					output.writeUTF(studentId.getText());
					output.writeUTF(transferCourseNum.getText());
					output.writeUTF(localCourseNum.getText());
					
					JOptionPane.showMessageDialog(null, "The student information has been saved.", "Submission Successful", JOptionPane.INFORMATION_MESSAGE);
				}
				catch(IOException c)
				{
					System.exit(0);
				}
				clearFields();
			}//close if
			else //code to execute if the user clicks exit
			{//open else
				try
				{
					output.close();
				}
				catch(IOException c)
				{
					System.exit(0);
				}
			}//close else
		}//close action performed
		
	public void clearFields()
	{
		//clear fields and reset focus
		name.setText("");
		studentId.setText("");
		transferCourseNum.setText("");
		localCourseNum.setText("");
		name.requestFocus();
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Cannot seem to get the JButton "exit" to work

#2 oha055  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 250
  • Joined: 02-February 09

Re: Cannot seem to get the JButton "exit" to work

Posted 20 March 2013 - 02:46 AM

Your problem is here:

else //code to execute if the user clicks exit
	            {//open else
	                try
	                {
	                    output.close();
	                }
	                catch(IOException c)
	                {
	                    System.exit(0);
	                }
	            }//close else



With your try/catch block you are telling the program to only exit if there is an I/O exception. Why? Just remove the try/catch and you should be fine :)
Was This Post Helpful? 2
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Cannot seem to get the JButton "exit" to work

Posted 20 March 2013 - 07:39 PM

You still need the try/catch in case the close fails

                    try
                    {
                        output.close();
                    }
                    catch(IOException c)
                    {
                       // not really anything I can do 
                    }
                    System.exit(0);


Was This Post Helpful? 1
  • +
  • -

#4 josephine11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-September 12

Re: Cannot seem to get the JButton "exit" to work

Posted 20 March 2013 - 08:05 PM

View Postpbl, on 20 March 2013 - 09:39 PM, said:

You still need the try/catch in case the close fails

                    try
                    {
                        output.close();
                    }
                    catch(IOException c)
                    {
                       // not really anything I can do 
                    }
                    System.exit(0);


Thanks for all of your replies, I have tried to eliminate this try/catch and the exit button still doesn't work. I don't know what else to try.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Cannot seem to get the JButton "exit" to work

Posted 20 March 2013 - 08:12 PM

that wont work

if (arg == "Submit")

you can;t compare String with the == operator
You have to use the String class equals() method

Better to use the ActionEvent class getSource() method

   public void actionPerformed(ActionEvent e) {
     if(e.getSource() == exitButton) {
        try {
          close
         ...


Was This Post Helpful? 2
  • +
  • -

#6 josephine11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-September 12

Re: Cannot seem to get the JButton "exit" to work

Posted 20 March 2013 - 08:40 PM

View Postpbl, on 20 March 2013 - 10:12 PM, said:

that wont work

if (arg == "Submit")

you can;t compare String with the == operator
You have to use the String class equals() method

Better to use the ActionEvent class getSource() method

   public void actionPerformed(ActionEvent e) {
     if(e.getSource() == exitButton) {
        try {
          close
         ...


The submit button actually works, so what ever I did, it does read the "Submit" and enters the try/catch, however, it is the Exit button not working.
Was This Post Helpful? 0
  • +
  • -

#7 farrell2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 838
  • View blog
  • Posts: 2,574
  • Joined: 29-July 11

Re: Cannot seem to get the JButton "exit" to work

Posted 20 March 2013 - 09:46 PM

I just followed pbl's advice and placed the System.exit(0) into the try block with close() and it closes just fine.
Was This Post Helpful? 1
  • +
  • -

#8 josephine11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-September 12

Re: Cannot seem to get the JButton "exit" to work

Posted 20 March 2013 - 10:53 PM

Thanks you all for your help and yes it worked that way. However, I did find that by adding just System.exit() after the { everything works fine.

else //code to execute if the user clicks exit
			{//open else
				try
				{
					output.close();
				}
				catch(IOException c)
				{
					System.exit(1);//this change to (1)
				}
				System.exit(0);// added this
 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1