Create and Show Wordpad File on JButton Click

Ads

 
 

Share on Google+Share on Google+

shweta
Create and Show Wordpad File on JButton Click
1 Answer(s)      7 years ago
Posted in : Java Beginners
Hello Sir I want to create wordpad or word document on JButton Click Event.which contains data which is currently on Form ie filled by User,
with following format
STUDENT ADMISSION FORM
STUDENT ID: (JTextField1Value currenly on Form)

NAME: (JTextField2Value currenly on Form)

ADDRESS: (JTextField3Value currenly on Form)

CITY : (JTextField4Value currenly on Form)

plz Help Me.
Ads
View Answers

April 22, 2010 at 3:05 PM


Hi Friend,

Try the following code:

import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.poifs.filesystem.*;
class Form extends JFrame{

JButton ADD;
JPanel panel,pan;
JLabel label1,label2,label3,label4;
final JTextField text1,text2,text3,text4;
Form() {
label1 = new JLabel();
label1.setText("Student Id:");
text1 = new JTextField(20);

label2 = new JLabel();
label2.setText("Name:");
text2 = new JTextField(20);

label3 = new JLabel();
label3.setText("Address:");
text3 = new JTextField(20);

label4 = new JLabel();
label4.setText("City:");
text4 = new JTextField(20);
ADD=new JButton("Add");

panel=new JPanel(new GridLayout(5,2));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(label3);
panel.add(text3);
panel.add(label4);
panel.add(text4);
panel.add(ADD);
add(panel,BorderLayout.CENTER);
setTitle("FORM");

ADD.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String data="STUDENT ADMISSION FORM\n\n\nSTUDENT ID: "+text1.getText()+"\nNAME: "+text2.getText()+"\nADDRESS: "+text3.getText()+"\nCITY: "+text4.getText();
writeToFile(data, "Employee.doc");
JOptionPane.showMessageDialog(null,"Inserted into File");
}
});
}
private static void writeToFile(String content, String path) {
try{
POIFSFileSystem fs = new POIFSFileSystem();
DirectoryEntry directory = fs.getRoot();
directory.createDocument("WordDocument", new ByteArrayInputStream(content.getBytes()));
FileOutputStream out = new FileOutputStream(path);

fs.writeFilesystem(out);
out.close();
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
}
}
class InsertToFile{
public static void main(String arg[]) {
try
{
Form frame=new Form();
frame.setSize(300,300);
frame.setVisible(true);
}
catch(Exception e)
{}
}
}

To run the above code, you need POI api.

Thanks

Ads









Related Tutorials/Questions & Answers:
Create and Show Wordpad File on JButton Click - Java Beginners
Create and Show Wordpad File on JButton Click  Hello Sir I want to create wordpad or word document on JButton Click Event.which contains data which...; import org.apache.poi.poifs.filesystem.*; class Form extends JFrame{ JButton
How to Open Text or Word File on JButton Click in Java - Java Beginners
How to Open Text or Word File on JButton Click in Java  How to Open Text or Word File on JButton Click in Java
Advertisements
Open TextFile on JButton Click - Java Beginners
Open TextFile on JButton Click  Hello Sir I Want to open TextFile on JButton Click. plz Help Me  Hi Friend, Do you want to simply open the text file or you want to open the file using JFileChooser,read that file
Open Text or Word Document on JButton Click in Java - Java Beginners
Open Text or Word Document on JButton Click in Java  How to open Word document or Text File on JButton Click in java  Hi Friend, Try... JFrame{ JButton ADD; JPanel panel,pan; JLabel label1,label2,label3,label4
Show Confirmation Dialog on Click the Command Link
Show Confirmation Dialog on Click the Command Link  How to show Confirmation Dialog when user Click the Command Link
jbutton
jbutton  How to use enter function in jbutton?plzz help
Jbutton
Jbutton  How to get the enter key function in multiple jbutton
how to open a file with a jbutton? - Java Beginners
how to open a file with a jbutton?  Hello Is it possible that by clicking on a button it can open java html file or doc exists on the pc? I searched and I found that it is possible to open a web page so I tried to open
click a word and search it from a file
click a word and search it from a file  i want to link a word with a file and search it form the file by clicking it. what could the code for this process
Hide/show Image by click
Hide/show Image by click In this tutorial ,we will discuss about how to display image by click using jQuery. In the below example, a image and two lines... as well. But when we click on remaining line , it will display the image again
Java Swing : JButton Example
Java Swing : JButton Example In this section we will discuss how to create...()); JButton button=new JButton("Click"); button.addActionListener(new... object of Button as - JButton button=new JButton("Click"); addActionListener
How to hide and show some field on a radio button click
How to hide and show some field on a radio button click  This is my code part:- PAYMENT DETAILS <table cellpadding="0" cellspacing="0" align="center" border="5" bordercolor="#003333" align="center" width="70%">
how to create ipa file
how to create ipa file  Hi, How to .ipa file? Thanks   Hi, Please check the thread Resources have been modified iPhone. Thanks
HTML File Upload
; HTML File Upload is used to show the list of all file, when a user click... an example from HTML File Upload. In this code, we create a html page which show you...;input type="file> is used to create a upload file with a text box
Hide/Show paragraph by button click
Hide/Show paragraph by button click In this tutorial, we will discuss about... displays another button "click to show paragraph " .when we click...;); }); $("#butt2").click(function () { $("p").show("slow"
How to create a jar file
How to create a jar file  Hello!!!! I have a project which has...???   The given code creates a jar file using java. import java.io.*; import...; protected void createJarArchive(File jarFile, File[] listFiles) { try
Open a .doc file by click on the link using struts - Struts
Open a .doc file by click on the link using struts   I want to open a doc extension file when i click a link on jsp page .I know only about which is write in web.xml but not exactly know how i do this.Plz any one can help me
Java file create new file
Java file create new file This section demonstrates you how to create a new... of these tools, you can can easily create a new empty file. Now for this task, we have... createNewFile(), you can create a file. Output: File is created
Adding a Rollover and Pressed Icon to a JButton Component in Java
about adding event i.e. the rollover and click icon to a JButton component of swing... Adding a Rollover and Pressed Icon to a JButton Component in Java... above the Button then your icon or image should be changed. When you click
Show all the entries of zip file.
Show all the entries of zip file. In this tutorial, We will  discuss how to show all the entries of a zip file.  The ZipFile   class is used...; returns enumeration of zip file entries. The getName() method
How To Create a New File
How To Create a New File In this section we will discuss about how to create... store String, characters, numbers etc. In Java to create a File for storing... which will demonstrate you about how to create a new file in Java
How to generate pdf file on click of the links using jsp..
How to generate pdf file on click of the links using jsp..  How to generate the pdf file using jsp file. Generate table in pdf by using jsp.   Here is an example that will generate a pdf file into c drive.The pdf file
Jbutton[] background & foregroundcolor change
Jbutton[] background & foregroundcolor change  how to change the color of the selected JButton in java swing. I am create JButton in an array JButton button[]=new JButton[100]; if i select button[5] then the Jbutton
Create File in Java
Create a File        Introduction Whenever the data is need to be stored, a file is used to store the data. File is a collection of stored information that are arranged
Show text by button click using jQuery
Show text by button click using jQuery In this tutorial, we will discuss about hide/show text by clicking on button. In the below example, there are two buttons : hide and show .The "hide " button is used to hide text
How to show next question from database on the click of next button..(Struts & Hibernate)
How to show next question from database on the click of next button..(Struts...?pageName=2&iid=<%=Id%>"><img src="images/edit-icon.png" title="Click Here To Update This Lists" alt="Click Here To Update This Stream"></a>
How to show next question from database on the click of next button..(Struts & Hibernate)
How to show next question from database on the click of next button..(Struts...="images/edit-icon.png" title="Click Here To Update This Lists" alt="Click Here...=deleteLists&id=<%=Id%>"><img src="images/delete-icon.png" title="Click
3D JButton in java?
3D JButton in java?  how we can create 3D button in java. thanks
CREATE AND WRITE FILE THREAD JAVA
CREATE AND WRITE FILE THREAD JAVA  Hi guys I was wondering how can I make this program in java with threads. I need to create a file and write in it (i know how to do that) but by listening in some port all the data that is being
Show output as a xml file using Velocity
Show output as a xml file using Velocity       This Example shows you how to show output as a xml file...:- Create object of VelocityContext Class. 3:- Create Template class object
Jbutton in JTable cells
Jbutton in JTable cells  I am a Java Beginner... I want a code in which I display data in Jtable from database and correspondingly a JButton for each cell and when i click on that button a new window should open