I have created an application using Swing that takes data that an user enters into textfields and once they press the submit button, the entered data is written to an external text file (program.txt), which I have placed in the same directory as the application.
Application1.jpg
Application2.jpg
I can then open my application and click 'Display Data' under my Functions menu selection and view the results in that window through a TextArea with a scrollpane, pulling the data from the text file (program.txt) that the data submitted was written to.
Application3.jpg
I am having trouble and have tried various approaches with my code and any help here would be appreciated by someone more knowledgeable.
Here is my code:
Java Code:
import java.awt.*; //declare packages needed
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.JMenuBar;
public class FinalProgram
extends JFrame
String answer =
(e.
getActionCommand());
if (answer == "Get Data") {
JLabel label8 =
new JLabel("Click Button to Submit Form -->");
JButton button1 =
new JButton("Submit Data"); [COLOR=
"Red"]
//here is button1[/COLOR]
textfield1.addActionListener(this);
textfield2.addActionListener(this);
textfield3.addActionListener(this);
textfield4.addActionListener(this);
textfield5.addActionListener(this);
textfield6.addActionListener(this);
textfield7.addActionListener(this);
button1.addActionListener(this);
//textfield1.setSize(10, 10);
iFrame.add(label1);
iFrame.add(textfield1);
iFrame.add(label2);
iFrame.add(textfield2);
iFrame.add(label3);
iFrame.add(textfield3);
iFrame.add(label4);
iFrame.add(textfield4);
iFrame.add(label5);
iFrame.add(textfield5);
iFrame.add(label6);
iFrame.add(textfield6);
iFrame.add(label7);
iFrame.add(textfield7);
iFrame.add(label8);
iFrame.add(button1);
try { [COLOR="Red"]//problem starts here[/COLOR]
String directoryName =
"c:\\javacode\\Notepad"; [COLOR=
"Red"]
//I've created my variable to hold my directory where my .txt file is to be pulled from[/COLOR] String fileName =
"program.txt"; [COLOR=
"Red"]
//variable that holds my text file name[/COLOR] File output =
new File(directoryName,fileName
); [COLOR=
"Red"]
//File object that points to the directory and file[/COLOR] output.createNewFile(); [COLOR="Red"]//if file is not created, create it[/COLOR]
if (! output.isFile()) { [COLOR="Red"]//if it can't be created, output an error message[/COLOR]
System.
out.
println("File creation of" + output.
getPath() + "failed");
return;
}
//put an if statement here
if (e.getSource() == button1) { [COLOR="Red"]//I feel I'm doing something wrong here, I'm trying to get the when the button (button1 created above) is pushed, to then execute the code below.[/COLOR]
[COLOR="Red"]//This part here is all test code below, that when the button (button1) is pressed, this code is executed and this text is written to a data file, once I confirm a button press I can move on to taking the data input into each of the separate textFields from the form and writing it, one step at a time, I'm beginner.[/COLOR]
String[] text = {"This is the text that will be written. \r", //we pass an array, which holds the text to append to the file
"The text will be written to the file \r",
"in append mode. If the file does not \r",
"exist it will be created.\r"};
for(int i = 0; i < text.length; i++) {
out.write(text[i] + "\n");
}
out.close();
}
}
System.
out.
println("Error writing to file " + d
);
}
[COLOR="Red"]//all other code outside of these red words works fine as you can see in the pictures, I'll accept any suggestions as I'll be working on this still.[/COLOR]
iFrame.setSize(200, 500);
iFrame.setVisible(true);
add(iFrame);
}
if (answer == "Display Data") {
scroller.getViewport().add(text);
iFrame.getContentPane().add(scroller);
iFrame.setSize(200, 150);
iFrame.setVisible(true);
add(iFrame);
}
}
public FinalProgram() {
super ("Menu Example");
item.addActionListener (this);
file.
add(item =
new JMenuItem("Display Data"));
item.addActionListener (this);
jmb.add(file);
addWindowListener(new ExitListener());
setJMenuBar (jmb);
}
public static void main(String[] args) {
FinalProgram window = new FinalProgram();
window.setTitle("Final Program");
window.setSize(600, 600);
window.setVisible(true);
}
}