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();
}
}
Cannot seem to get the JButton "exit" to work
Page 1 of 17 Replies - 177 Views - Last Post: 20 March 2013 - 10:53 PM
#1
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.
Replies To: Cannot seem to get the JButton "exit" to work
#2
Re: Cannot seem to get the JButton "exit" to work
Posted 20 March 2013 - 02:46 AM
Your problem is here:
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
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
#3
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);
#4
Re: Cannot seem to get the JButton "exit" to work
Posted 20 March 2013 - 08:05 PM
pbl, 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.
#5
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
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
...
#6
Re: Cannot seem to get the JButton "exit" to work
Posted 20 March 2013 - 08:40 PM
pbl, 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
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.
#7
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.
#8
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
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|