Start New Discussion within our Software Development Community

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...

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");
  }
}

Look into the File class, it will allow you to create a new File by a given name, I think. And also look into the PrintWriter class which will allow you to output to a text file.

This article has been dead for over six months. Start a new discussion instead.