Open Text or Word Document on JButton Click in Java

Open Text or Word Document on JButton Click in Java

How to open Word document or Text File on JButton Click in java
View Answers

April 22, 2010 at 5:22 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");
try{
Runtime rt = Runtime.getRuntime();
rt.exec("cmd.exe /C start Employee.doc");
}
catch(Exception e){}
}
});
}
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)
{}
}
}

Thanks









Related Tutorials/Questions & Answers:
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
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
java code to open and display MS-word document
java code to open and display MS-word document  java code to open and display MS-word document
open word document
open word document  how to open a word document ??   Please go through the following link: Java Read word document file The above link will provide an example that will read the document file using POI library in java
open word document
open word document  how to open a word document ??   Please go through the following link: Java Read word document file The above link will provide an example that will read the document file using POI library in java
open word document
open word document  how to open a word document ??   Please go through the following link: Java Read word document file The above link will provide an example that will read the document file using POI library in java
Scanning a word in a TEXT document......
Scanning a word in a TEXT document......  Hi Genius persons... I'm having many resumes in my FOLDER called HARISH in D:(colon) i want to scan... by searching the JAVA and .net words present in the files..if JAVA word is present means
Scanning a word in a TEXT document......
Scanning a word in a TEXT document......  Hi Genius persons... I'm having many resumes in my FOLDER called HARISH in D:(colon) i want to scan... by searching the JAVA and .net words present in the files..if JAVA word is present means
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 hide text in Word Document using Apache POI library
How to hide text in Word Document using Apache POI library  Is it possible to hide text in MS Word Document using Apache POI library? If yes, please.... Is there any way to hide some kind of text (could be in anywhere in the document
export to word document - Java Beginners
export to word document  hi sir,when i am click on a button under the jtable,for example (print button),then i want to print that jtable in word document,automatically,plz provide program sir  Hi Friend, Try
How to merge two word document using java
How to merge two word document using java   how to merge two word document using java
How to merge two word document using java
How to merge two word document using java   how to merge two word document using java
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
open document
open document  open a word document using jsp   Please go through the following link: Java Read word document file The above link will provide an example that will read the document file using POI library in java
Creating MS Word document in java - Struts
Creating MS Word document in java  I have one template in MS Word which has some content. Now i want to read this doc file and wants to add some... you plz tell me how to do in java
Open website on Button Click - Java Beginners
Open website on Button Click  Hello sir I want to open website on button click using java swing plz help me sir. in my swing application one "VISIT US BUTTON" i want to open my website on Button CLick  Hi Friend
Write String in Word document
Write String in Word document  How to read and write strings in word document
Using poi hwpf,how to create table in word document. - Java Beginners
Using poi hwpf,how to create table in word document.  Can you please tell me how to create a table in the word document in java using Apache poi hwpf.its very urgent.Please send some sample code.Thanks in advance
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
Text change and click events
Text change and click events  Create user login form and apply textchange and click events in java
Edit word document Headers and Footers using POI Library in java?
Edit word document Headers and Footers using POI Library in java?  Hi All, I need code to modify the Header and Footer contents of a ms-word template using POI or JACOB libraries. Could any one help me? Thanks Advance
Search word from text file using Java
How to Search word from text file using Java In this section, we are going to search a word from the text file. For this, we have created a swing button. On clicking, it will open another window that will ask the user to enter name. When
convert word document to pdf
convert word document to pdf  Hi, Could you please help me write a java code that converts the word docuemnt into PDF? I have to save the generated... a java beginner. Please help out in this. Thanks in advance. Regards, Sindhu
create , edit MS WORD like document file using Java Swing - Swing AWT
create , edit MS WORD like document file using Java Swing   In my program I have JTextArea. Text in JTextArea can be set to selected font, font style etc. I want this text in JTextArea to be saved to MS WORD like
APACHE.POI -- Create word Document - Development process
APACHE.POI -- Create word Document  Hi, Could any one please post the code which creates the word document having different paragraphs and two... the code which creates MS Word in Java... Raghu J
store form data into word document
store form data into word document  i want form page data, ex username phone number.. to store it in a word document file
word document reading - Development process
word document reading  I am in the midst of reading a word document while reading charts(EMF or WMF) pictures i am not able to do so can u suggest one class for this to accomplish
JTextArea to Word Document
JTextArea to Word Document Jakarta POI has provided several classes that enable us to perform read, write operations with ms word file. Here we are going to write the data into word document file through a swing component. You can
POI Word document (Letter Template)
POI Word document (Letter Template)  Dear Team, i need code for generating word document(letter format). i am unable to get the code for formats, font settings, letter type settings. please help me for the same. Thanks
Java - search/find a word in a text file - Java Beginners
Java - search/find a word in a text file  Hello, I would like... to search them for a word. The word will be PASS or it can be FAIL. If PASS I...); System.out.print("Enter word to find: "); String word=input.next
ModuleNotFoundError: No module named 'text-word'
ModuleNotFoundError: No module named 'text-word'  Hi, My Python... 'text-word' How to remove the ModuleNotFoundError: No module named 'text... have to install padas library. You can install text-word python with following
ModuleNotFoundError: No module named 'text-word'
ModuleNotFoundError: No module named 'text-word'  Hi, My Python... 'text-word' How to remove the ModuleNotFoundError: No module named 'text... have to install padas library. You can install text-word python with following
ModuleNotFoundError: No module named 'text-word'
ModuleNotFoundError: No module named 'text-word'  Hi, My Python... 'text-word' How to remove the ModuleNotFoundError: No module named 'text... have to install padas library. You can install text-word python with following
Text field save as word file
Text field save as word file  Dear experts At run how to save set of text field contains text into single Word file. To save in our desktop computer. Solve my problem
3D JButton in java?
3D JButton in java?  how we can create 3D button in java. thanks
stop word removal from text file
stop word removal from text file  i need java source code for stop word removal from a text file
Adding a Rollover and Pressed Icon to a JButton Component in Java
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 in java. Rollover means moving mouse pointer above the icon on the button. 
Java search word from text file
Java search word from text file In this tutorial, you will learn how to search a word from text file and display data related to that word. Here, we have created a text file student.txt which consists of id, name and marks of few
jbutton
jbutton  How to use enter function in jbutton?plzz help
Jbutton
Jbutton  How to get the enter key function in multiple jbutton
jbutton - Java Beginners
jbutton  Hi, I have jlist and jbutton options.If i select... are displyed in the jlist using jbutton(refresh button). I want to know how to refresh... listModel; JList list ; public JListExample() { super(Select); JButton
Convert Text To Word
Convert Text To Word       In this example, You will learn how to convert text to word file. Here, we are going to discuss about the conversion of text to word file.  Core
upload and retrieve a word document - JSP-Servlet
upload and retrieve a word document  Hi, I have a problem with uploading a word document to My sql database to a BLOB data type variable. And also unable to retrieve the word document from database. I am writing the code
java script to replace words starting and ending with tild(~) symbol in a text area with the word highlighted yellow.
java script to replace words starting and ending with tild(~) symbol in a text area with the word highlighted yellow.  Hi, I have an issue with highlighting text in a text area. I am designing a page to send email. currently
Will i be able to get the content of a webpage in a word document (Note:i don t have access to the source code)through java code?
Will i be able to get the content of a webpage in a word document (Note:i don t have access to the source code)through java code?  public class... am getting output to this code as html tags.... But what i want is in a word
PDF to Word Conversion - Java Beginners
PDF to Word Conversion  Hello, Can we convert a PDF document to Microsoft word document thru Java. If its not possible in Java, is it possible in any other language
create MS Word in Java - Java Beginners
create MS Word in Java  Hi, Could any one please post the code which creates the word document having different paragraphs and two tables. i have... which creates MS Word in Java
how to match the key word from a text file
how to match the key word from a text file  p>Hi all, I have the code to match the key word and from the text. I have input like this reader.txt... ((devstr = br.readLine()) != null) { String text = devstr; String text1

Ads