Create and Show Wordpad File on JButton Click

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









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
react show another component on click
react show another component on click  Hi, I want to show the another component view on the click on the button or link? What is code for react show another component on click? Thanks   Hi, React is powerful UI
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
ModuleNotFoundError: No module named 'wordpad'
ModuleNotFoundError: No module named 'wordpad'  Hi, My Python... 'wordpad' How to remove the ModuleNotFoundError: No module named 'wordpad... to install padas library. You can install wordpad python with following command
ModuleNotFoundError: No module named 'wordpad'
ModuleNotFoundError: No module named 'wordpad'  Hi, My Python... 'wordpad' How to remove the ModuleNotFoundError: No module named 'wordpad... to install padas library. You can install wordpad python with following command
Java Swing : JButton Example
Java Swing : JButton Example In this section we will discuss how to create... button=new JButton("Click"); button.addActionListener(new ActionListener... as - JButton button=new JButton("Click"); addActionListener() method adds
ModuleNotFoundError: No module named 'click-config-file'
ModuleNotFoundError: No module named 'click-config-file'  Hi, My... named 'click-config-file' How to remove the ModuleNotFoundError: No module named 'click-config-file' error? Thanks   Hi, In your
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
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
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
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"
HTML File Upload
; HTML File Upload is used to show the list of all file, when a user click... illustrate an example from HTML File Upload. In this code, we create a html page...;file">:The <input type="file> is used to create a upload file
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
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
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
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..."); } } } Through the method createNewFile(), you can create a file. Output
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
how to create tar file in ubuntu
how to create tar file in ubuntu  Hi, I have a directory with many sub directories and files in it. I want to create tar file of the directory so... tar file in ubuntu? Thanks   Hi, Here is command to create tar file
how to create tar file in ubuntu
how to create tar file in ubuntu  Hi, I have a directory with many sub directories and files in it. I want to create tar file of the directory so... tar file in ubuntu? Thanks   Hi, Here is command to create tar file
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
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
How to create XML file - XML
How to create XML file  Creating a XML file, need an example. Thanks!!  To create XML file you need plain text editor like note pad. After... language has many API's to create xml file from program.Here is the code example
Create JAR file - Java Beginners
Create JAR file  Respected Sir, I got the answer from your side. But do let me know, isn't there any other way of making a JAR File. Simply like... to make a JAR file and run it easily on any computer. For that purpose i want
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... a simple example which will demonstrate you about how to create a new file
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 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 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
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
Map words to line number in text file and show occurence
Map words to line number in text file and show occurence  hi i want to Map words to line number in text file and show occurrence of word in java coding
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
i want java code for this xml file...please show me..
i want java code for this xml file...please show me..   xbrli:shares xbrli:pure iso4217:INR
Create JSP file
Create a JSP file To create a JSP file for struts, Some special (Struts... for StrutsPrepareAndExecuteFilter in web.xml file. as <filter> <filter-name>struts2..., this is the name of the action, which is you will map in the struts.xml file
Java Swing Create XML file
Java Swing Create XML file In this tutorial, you will learn how to create XML file using swings. Here is a code that accepts the data from the user through... and fetch all the data from textfields and create an xml file with proper
3D JButton in java?
3D JButton in java?  how we can create 3D button in java. thanks
Create a temporary file
Create a temporary file In this section, you will learn how to create a temporary file. This will perform with the help of  createTempFile() method of File class. The syntax and description of createTempFile() method is given
i want to create an application with only a button which on click displays table from database using struts2 and hibernate on eclipse
i want to create an application with only a button which on click displays table from database using struts2 and hibernate on eclipse  please help me i have to submit this soon
How to put an image on a JButton?
How to put an image on a JButton?  How to put an image on a JButton
Create new file in java
Create new file in java We are going to discuss how to create new file in java... create main method than we use try and catch block. We have created File object.... With File class we create files and directories. Java File represents actual
how to reduce width of jbutton ?
how to reduce width of jbutton ?  how to reduce width of jbutton ? thanks in advancwe
how to create an excel file using java
how to create an excel file using java  how to create an excel file using java
Java create table in html file
Java create table in html file In this section, you will learn how to create table in html file. In the previous section, you have seen different operations... as an argument. Now we have used the html table tag to create a table in html file

Ads