This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv 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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "having a small problem" Watch "having a small problem" New topic

having a small problem

brett Watson

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.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");
new ActionListener()
public void actionPerformed(ActionEvent event)
Date today = new Date();
fileOutput = new Formatter("guestlog.txt");
JOptionPane.showMessageDialog(null, "Success!");
catch(FileNotFoundException e)
System.err.println("Error writing to the log.");

fileOutput.format("%s\n%s\n%s\n", today, ", ", text.getText());
add(label, BorderLayout.NORTH);
add(text, BorderLayout.CENTER);
add(button, BorderLayout.SOUTH);
public static void main(String[] args)
TextAppendTest application = new TextAppendTest();
application.setSize(250, 100);

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:
subject: having a small problem
It's not a secret anymore!