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,
plz Help Me.
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:
Advertisements
Open TextFile on JButton Click - Java BeginnersOpen 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 clickreact
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
jbuttonjbutton How to use enter function in
jbutton?plzz help
JbuttonJbutton How to get the enter key function in multiple
jbutton how to open a file with a jbutton? - Java Beginnershow 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 fileclick 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
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 create ipa filehow 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 fileHow 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
Java file create new fileJava
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 ubuntuhow 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 ubuntuhow 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 JAVACREATE 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 changeJbutton[] 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 - XMLHow 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 BeginnersCreate 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 FileHow 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 jQueryShow 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
Jbutton in JTable cellsJbutton 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
Create JSP fileCreate 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 fileJava 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
Create a temporary fileCreate 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
Create new file in javaCreate 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
Java create table in html fileJava
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