how to save output (from TextField) to a particular Folder as txt file
hi,
i m facing a small prob in GUI, need to save my output to a particular folder.
below this code showing the square of any integer in TextField named: "resultField". i've a button "save" that need to save the result from that TextField, suppose in my "c:\Repository " folder as a txt file.
In addition, the name of the txt file can be same as given input integer. Example: 77, 99, .. whatever the input integer.
how can i do this ?? pls help...
Code:
import java.awt.*;
import java.awt.event.*;
public class SaveData extends Frame implements ActionListener {
private TextField inputField, resultField;
private Button saveButton, squareButton, exitButton;
SaveData(String s) {
super(s);
setLayout(new GridLayout(3, 3, 5, 3));
add(new Label("Type in an integer: "));
inputField= new TextField(); add(inputField);
squareButton=new Button("Square"); add(squareButton); squareButton.addActionListener(this);
resultField=new TextField(); add(resultField);
saveButton=new Button("SAVE"); add(saveButton); saveButton.addActionListener(this);
exitButton=new Button("Exit"); add(exitButton); exitButton.addActionListener(this);
setSize(250,130);
setLocation(600,200);
setVisible(true); }
public void actionPerformed(ActionEvent e) {
if (e.getSource()==squareButton) {
int x=Integer.parseInt(inputField.getText());
resultField.setText(Integer.toString(x*x)); }
else if (e.getSource()==exitButton) { this.setVisible(false); this.dispose(); }
}
public static void main(String args[]){
new SaveData("test");
}
}
Re: how to save output (from TextField) to a particular Folder as txt file
// this programme helps you to enter any data to a text file in a predetermined place
//by means of a text box and a action button....
/*
* Input.java
*
* Created on August 3, 2012, 11:59 AM
*/
package me.org.source;
import java.io.BufferedWriter;
import java.io.FileWriter;
/**
*
* @author Vbabey
*/
public class Input extends javax.swing.JFrame {
public String data;
/** Creates new form Input */
public Input() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
dataText = new javax.swing.JTextField();
okButton = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
setTitle("Favourite Quote");
okButton.setMnemonic('O');
okButton.setText("OK");
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okButtonActionPerformed(evt);
}
});
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
jLabel1.setForeground(new java.awt.Color(0, 102, 102));
jLabel1.setText("Favourite Quote");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addComponent(dataText, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(okButton)
.addGap(17, 17, 17))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(43, 43, 43)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(okButton)
.addComponent(dataText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addContainerGap(46, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
data =dataText.getText();
try{
// Create file
FileWriter fstream = new FileWriter("C:\\Users\\Vbabey\\Desktop\\java\\out. txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write(data);
//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
dataText.setText("");
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Input().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField dataText;
private javax.swing.JLabel jLabel1;
private javax.swing.JButton okButton;
// End of variables declaration
}
Re: how to save output (from TextField) to a particular Folder as txt file
Quote:
Originally Posted by
Vbabey
// this programme helps you to enter any data to a text file in a predetermined place
This is a two and a half year old thread, which already has a valid answer.
Locking the thread.
Re: how to save output (from TextField) to a particular Folder as txt file
Member Vbabey has already been asked to not post to old dead threads or hijack another user's thread.
http://www.java-forums.org/new-java/...ad-hijack.html
The member is banned for 7 days to allow time to read the warnings.
db