Code how to?

Code how to?

View Answers

April 14, 2009 at 1:27 PM

Hi Friend,

We are providing you a code that will display a form on clicking the 'Form' button. After filling the fields, when you click the button 'Submit', the entered values get inserted into excel file.
Now to open the excel file, click the button 'Open Excel file'.

step1) Create CreateForm.java

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;

class CreateForm extends JFrame{
CreateForm()
{
JPanel panel;
JLabel label1,label2,label3,label4,label5;
final JTextField text1,text2,text3,text4,text5;
JButton button=new JButton("Submit");
label1 = new JLabel();
label1.setText("UserID:");
text1 = new JTextField(20);

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

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

label4 = new JLabel();
label4.setText("Address:");
text4 = new JTextField(20);

label5 = new JLabel();
label5.setText("Email:");
text5 = new JTextField(20);

panel=new JPanel(new GridLayout(6,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(label5);
panel.add(text5);
panel.add(button);
add(panel,BorderLayout.CENTER);

button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String value1=text1.getText();
String value2=text2.getText();
String value3=text3.getText();
String value4=text4.getText();
String value5=text5.getText();
try{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(" sheet");
HSSFRow row = sheet.createRow((short)0);
HSSFCell cell = row.createCell((short)0);
cell.setCellValue(1);
row.createCell((short)0).setCellValue(value1);
row.createCell((short)1).setCellValue(value2);
row.createCell((short)2).setCellValue(value3);
row.createCell((short)3).setCellValue(value4);
row.createCell((short)4).setCellValue(value5);
FileOutputStream fileOut = new FileOutputStream("c:\\File.xls");
wb.write(fileOut);
fileOut.close();
}catch ( Exception ex ){
}
}
});
setTitle("FORM");
setVisible(true);
setSize(300,300);
}
}

April 14, 2009 at 1:29 PM

cont........

step 2) Create AccessFormDemo.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
class Form extends JFrame
{
JButton ADD,SAVE;
JPanel panel;

Form(){
ADD=new JButton("Form");
SAVE=new JButton("Open Excel file");

panel=new JPanel(new GridLayout(2,2));
panel.add(ADD);
panel.add(SAVE);
add(panel,BorderLayout.CENTER);
setTitle("FORM");

SAVE.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
try {
Runtime rt = Runtime.getRuntime();
rt.exec("cmd.exe /C start C:\\File.xls");
}
catch(Exception ex){
ex.printStackTrace();
}
}
});
ADD.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
CreateForm tab=new CreateForm();
tab.setVisible(true);
}
});
}
}
class AccessFormDemo
{
public static void main(String arg[])
{
try
{
Form frame=new Form();
frame.setSize(300,100);
frame.setVisible(true);
}
catch(Exception e)
{}
}
}

Thanks









Related Tutorials/Questions & Answers:
ModuleNotFoundError: No module named 'tod'
ModuleNotFoundError: No module named 'toi'
Advertisements
ModuleNotFoundError: No module named 'tok'
ModuleNotFoundError: No module named 'rio-toa'
ModuleNotFoundError: No module named 'toh-gym'
ModuleNotFoundError: No module named 'tol'
ModuleNotFoundError: No module named 'tol'
ModuleNotFoundError: No module named 'tot'
ModuleNotFoundError: No module named 'django-tof'
ModuleNotFoundError: No module named 'color-tol'
ModuleNotFoundError: No module named 'tor'
ModuleNotFoundError: No module named 'tor'
ModuleNotFoundError: No module named 'ToE'
org.ton - ton-kotlin-crypto-iosarm64 version 0.2.9 Maven dependency. How to use ton-kotlin-crypto-iosarm64 version 0.2.9 in pom.xml?
ModuleNotFoundError: No module named 'tow'
ModuleNotFoundError: No module named 'django-tos'
ModuleNotFoundError: No module named 'django-tos'
ModuleNotFoundError: No module named 'django-tos'
ModuleNotFoundError: No module named 'toc'
How to set left and top positions of alert box using JQuery in the below code?
ModuleNotFoundError: No module named 'tor_access'
ModuleNotFoundError: No module named 'tor_access'
ModuleNotFoundError: No module named 'tor_access'
ModuleNotFoundError: No module named 'tor-proxy'
ModuleNotFoundError: No module named 'tor-router'
tom project
ModuleNotFoundError: No module named 'ton-client'
ModuleNotFoundError: No module named 'toy-model'
ModuleNotFoundError: No module named 'toy-model'
ModuleNotFoundError: No module named 'toy-model'
ModuleNotFoundError: No module named 'toy-orm'
ModuleNotFoundError: No module named 'colab-mkdocs-tos'
ModuleNotFoundError: No module named 'colab-mkdocs-tos'
ModuleNotFoundError: No module named 'colab-mkdocs-tos'
ModuleNotFoundError: No module named 'colab-mkdocs-tos'
ModuleNotFoundError: No module named 'TOM'
ModuleNotFoundError: No module named 'TOM'
ModuleNotFoundError: No module named 'tor_async_couchdb'
ModuleNotFoundError: No module named 'tor_async_util'
ModuleNotFoundError: No module named 'tor_async_couchdb'
ModuleNotFoundError: No module named 'tor_async_util'
ModuleNotFoundError: No module named 'cloudflare-tor-whitelister'
ModuleNotFoundError: No module named 'markdown-toc'
ModuleNotFoundError: No module named 'md-toc'
ModuleNotFoundError: No module named 'nb_toc'
ModuleNotFoundError: No module named 'nb_toc'
ModuleNotFoundError: No module named 'sphinxcontrib-toc'
ModuleNotFoundError: No module named 'tic-toc'
ModuleNotFoundError: No module named 'djangocms_toc'
ModuleNotFoundError: No module named 'gfm-toc'

Ads