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

Ads









Related Tutorials/Questions & Answers:
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
Advertisements
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
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
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
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
Write String in Word document
Write String in Word document  How to read and write strings in word document
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
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
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
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
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
3D JButton in java?
3D JButton in java?  how we can create 3D button in java. thanks
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. 
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
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
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
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
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
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
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
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