This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

having a small problem

 
brett Watson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic