This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes having a small problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "having a small problem" Watch "having a small problem" New topic
Author

having a small problem

brett Watson
Greenhorn

Joined: Oct 20, 2006
Posts: 2
so here's my assignment guys
Question: Create a Java GUI program that accepts a name and add the name and current date to a file (guestlog.txt). If the process is
successful, the program prompts a window with a message (e.g., Success!). If a user clicks on the OK button of the message window,
the name in the previous window is erased, waiting for a new user. The GUI also has another button that a user can click on to close
the program.

and now here's my code:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.util.Date;
import java.util.Formatter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class TextAppendTest extends JFrame
{
JFrame frame;
JLabel label;
JTextField text;
JButton button;
Formatter fileOutput;
public TextAppendTest()
{
super("Guest Log");
label = new JLabel();
label.setText("Enter your name");
text = new JTextField(20);
button = new JButton("OK");
button.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
Date today = new Date();
try
{
fileOutput = new Formatter("guestlog.txt");
JOptionPane.showMessageDialog(null, "Success!");
}
catch(FileNotFoundException e)
{
System.err.println("Error writing to the log.");
System.exit(1);

}
finally
{
fileOutput.format("%s\n%s\n%s\n", today, ", ", text.getText());
fileOutput.close();
System.exit(0);
}
}
}
);
add(label, BorderLayout.NORTH);
add(text, BorderLayout.CENTER);
add(button, BorderLayout.SOUTH);
}
public static void main(String[] args)
{
TextAppendTest application = new TextAppendTest();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.setSize(250, 100);
application.setVisible(true);
}
}


i'm a little stuck on the end of the question where it says "If a user clicks on the OK button of the message window, the name in the previous window is erased, waiting for a new user"

any help will be greatly appreciated
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
in the actionPerformed() for the OK button, you would normally include
(after save, and displaying success message)

text.setText("");//clears the textField
text.requestFocusInWindow();//put focus back to textField
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: having a small problem
 
Similar Threads
Help understanding error messages
Calculator Problem
Error message
JTextArea scroll bar
Java Programming Help (conversions)