/*
////////////// Read First Installation Instructions //////////////////////////
Code By : Tapas jena
My Email:tapas.friends@gmail.com
Updated Date : january , 2010
================== Directory ====================================================
step 1 : make directory james in c: , that is c:\james
step 2 : place the following files in this directory
a. addrec.java d.Andrew.jpg g.Laura.jpg j.Nancy.jpg
b. james.mdb e.James.jpg h.Margaret.jpg k.Robert.jpg
c. sidebar33.jpg f.Janet.jpg i.Micheal.jpg l.Steven.jpg
download these files from
http://www.geocities.com/james_smith73
Download Menu --> james.zip
================== c:\Autoexec.bat ==================================================
(assumption u have installed jdk in c:\jdk1.2.1 directory , works in higher versions too )
(add below lines in c:\Autoexec.bat - Restart your Computer for Settings to take effect)
(If Nesssary)
step 3 : SET classpath= (NOTE :: To be done only incase there is any problem running the application)
step 4 : SET PATH=c:\windows;c:\windows\command;c:\jdk1.2.1\bin;c:\
step 5 : SET JAVA_HOME=c:\jdk1.2.1
================== ODBC Settings ========================
step 6 : click control panel
step 7 : ODBC Data Sources
step 8 : click UserDSN Tab
step 9 : click ADD Button , Select Microsoft Access Driver , Finish
step 10 : Give Datasource Name as "james" , no double quotes
step 11 : use Select and select james.mdb from c:\james\james.mdb
click ok , ok finish
================== Compile =======================================================
step 12 : cd c:\james
step 13 : javac addrec.java
================= Run ============================================================
step 14 : java addrec
(IF U GET CLASSNOT FOUND ERROR : set classpath= will help u overcome this)
login 'a' and password 'a' for Accoutant (use this)
login 'p' and password 'p' for Principal
================================================================================
I prefer u reading this on Macromedia Homesite Editor 5.0 to Edit my java code
u may as well crack it friends , keep it a secret :) hahahahaha...... ,
//================================================================================
////////////// End Installation Instructions /////////////////////////////////////
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import javax.swing.table.*;
public class addrec extends JFrame
{
boolean inAnApplet = true;
final boolean shouldFill = true;
final boolean shouldWeightX = true;
//Login panel TAB
final static String MYPANEL1 = " LOGIN ";
//Student Detail panel TAB
final static String MYPANEL2 = " ADD STUDENT DETAILS ";
//Modify or Delete Student Details panel TAB
final static String MYPANEL3 = " MODIFY / DELETE STUDENT DETAILS ";
//First Image to Load is declared as steam.gif to welcome user
String imagename = "steam.gif";
ImageIcon ii = new ImageIcon(imagename);
//I Prefer courier new fonts whereever possible
Font dataFont = new Font("courier new",Font.PLAIN,12);
Font titleFont = new Font("courier new",Font.BOLD,14);
//Required to handle Radio buttons Event
ActionListener rlistener = new RadioListener();
//Required to handle Check Box Event
CheckBoxListener myListener = new CheckBoxListener();
//Required to handle Combo Box Events
ComboBoxListener cbListener = new ComboBoxListener();
//I prefer gridbag layout (more flexible to place Elements inside a Tab)
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
GridBagConstraints c2 = new GridBagConstraints();
GridBagConstraints c5 = new GridBagConstraints();
//I need a Dialog Box to ask delete confirmation messages etc..
Container contentPane = getContentPane();
JDialog dialog = new JDialog((Frame) null, "! DELETE CONFIRMATION !", true);
Container dialogContentPane = dialog.getContentPane();
//Radio Buttons need to be grouped so that u check on one other gets unchecked
ButtonGroup bg = new ButtonGroup();
ButtonGroup bg2 = new ButtonGroup();
ButtonGroup bgq = new ButtonGroup();
// Declare All Labels i will be using somewhere or the other
JLabel jLabel, jLabel1, jLabel2, jLabel3, jLabel4, jLabel5, jLabel6, jLabel7;
JLabel jLabel77, jLabel8, jLabel9, jLabelim;
JLabel jLabeldlg = new JLabel("Do You Really want to Delete ?");
JLabel jLabelq1, jLabelq2;
// Declare All Radiobuttons i will be using somewhere or the other
JRadioButton jRadiobutton, Male, Female, mMale, mFemale;
JRadioButton jRadiobuttonq1, jRadiobuttonq2;
// Declare All Checkboxes i will be using somewhere or the other
JCheckBox jCheckbox, SSLC, PHOTO, CHARCERT, NCCCERT, mSSLC, mPHOTO, mCHARCERT, mNCCCERT;
JCheckBox jCheckboxq1, jCheckboxq2, jCheckboxq3, jCheckboxq4, jCheckboxq5;
// Declare All Comboboxes i will be using somewhere or the other
JComboBox jCombobox, jCombobox1, jCombobox2 ,mjCombobox1, mjCombobox2, jCombobox3;
JComboBox jComboboxq1, jComboboxq2;
// Declare All Textboxes i will be using somewhere or the other
JTextField jTextfield;
JTextField loginname = new JTextField(40);
JTextField loginpass = new JTextField(40);
JTextField adds_name = new JTextField(20);
JTextField adds_addr = new JTextField(20);
JTextField adds_imname = new JTextField(20);
JTextField adds_fname = new JTextField(20);
JTextField mods_fname = new JTextField(20);
JTextField mods_name = new JTextField(20);
JTextField mods_addr = new JTextField(20);
// All String type variables
String driver="sun.jdbc.odbc.JdbcOdbcDriver";
String url="jdbc:odbc:james";
String dialogtitle = "Student Record Added";
String dialogf = "LOGIN FAILED";
String dialogm = "DATABASE RECORD UPDATED";
String dialogd = "DATABASE RECORD DELETED";
String Sexsel = "Male";
String Yearsel = "1st PUC";
String Sectionsel = "Section A";
String currname = "";
String tempname = "";
String qry = "";
String condsel="No Condition", Combo1sel="StudentID", Combo2sel="=";
String sql, sql1, dialogmessage, sr, Namesel, reg_no;
// All Integer type variables
int SSLCsel, PHOTOsel, CHARCERTsel, NCCCERTsel, mSSLCsel, mPHOTOsel, mCHARCERTsel, mNCCCERTsel;
int dialogtype = JOptionPane.PLAIN_MESSAGE;
int tempcnt;
int StudentIDsel=0,StudentNamesel=0,Addresssel=0,FathersNamesel=0,qSexsel=0;
//Declaring all JPanel & Tabbed panes to be used
JPanel jPanel, p1, p2, p3;
JTabbedPane tabbedPane = new JTabbedPane();
// Declare All Submit Buttons i will be using somewhere or the other
JButton login = new JButton("LOGIN");
JButton DELETEIT = new JButton("YES DELETE");
JButton DONTDELETE = new JButton("NO DELETE");
JButton button, ADDREC, MODIFY, SHOW_STUDENT_LIST, DELETE;
JButton QUERY;
//Required to Build a JTable in Student Reports (also known as DataGrid by VC++ Users)
Object[] data = new Object[5];
DefaultTableModel defaulttablemodel = new DefaultTableModel();
JTable jtable = new JTable(defaulttablemodel);
public static void main(String args[])
{
addrec app = new addrec();
}
public addrec()
{
// Declare Title to my Application
super("JDBC - SWING Add & Modify Record mail me : james_smith73@yahoo.com");
setup();
pack();
addWindowListener(new WindowEventHandler());
show();
}
void setup()
{
setupMenuBar(); //Call "setupMenuBar()" to Build the Exit Menu
showpane1(); //Call some method to Build the Exit Menu
contentPane.add(tabbedPane, BorderLayout.CENTER);
}
void setupMenuBar()
{
MenuBar menuBar = new MenuBar();
// Main Menu will be "File"
Menu fileMenu = new Menu("File");
// Sub menu under it will be "Exit"
MenuItem fileExit = new MenuItem("Exit");
// Handle this submenu item clcik event to Exit
fileExit.addActionListener(new MenuItemHandler());
fileMenu.add(fileExit);
menuBar.add(fileMenu);
setMenuBar(menuBar);
}
void showpane1()
{
p1 = new JPanel() //LOGIN TAB
{
public Dimension getPreferredSize()
{
Dimension size = super.getPreferredSize();
// i set width=785 & height=495 for my login TAB & OTHER TABS will use it
size.width = 785;
size.height = 495;
return size;
}
};
p1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Login Panel"));
p1.setFont(titleFont);
p1.setLayout(gridbag);
jLabel1 = new JLabel("Enter Username : ");
jLabel1.setFont(dataFont);// Make this Data Font that we declared before
//c.weightx = 0.0; //Horizontal Space not required here
c.weighty = 0.0; //Vertical Space
c.ipady = 2; //Height of Component
c.ipadx = 2; //Width of Component
c.anchor = GridBagConstraints.WEST;
c.gridwidth = 1; //1 columns wide
c.gridx = 0; //aligned with button 0 -- Very Important
c.gridy = 0; //0th Row -- Very Important
// Add all these features to this Label "jLabel1"
gridbag.setConstraints(jLabel1, c);
// Add this featured Label "jLabel1" into out LOGIN TAB
p1.add(jLabel1);
loginname = new JTextField(" ");
c.ipady = 2;
c.ipadx = 2;
c.weighty = 0.0;
c.anchor = GridBagConstraints.WEST;
c.gridwidth = 2;
c.gridx = 1;
c.gridy = 0;
gridbag.setConstraints(loginname, c);
p1.add(loginname);
jLabel2 = new JLabel("Enter Password : ");
jLabel2.setFont(dataFont);
c.ipady = 2;
c.ipadx = 2;
c.weighty = 0.0;
c.anchor = GridBagConstraints.WEST;
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 1;
gridbag.setConstraints(jLabel2, c);
p1.add(jLabel2);
loginpass = new JTextField(" ");
c.ipady = 2;
c.ipadx = 2;
c.weighty = 0.0;
c.anchor = GridBagConstraints.WEST;
c.gridwidth = 2;
c.gridx = 1;
c.gridy = 1;
gridbag.setConstraints(loginpass, c);
p1.add(loginpass);
button = new JButton("LOGIN");
c.ipady = 2;
c.ipadx = 2;
c.weighty = 0.0;
c.anchor = GridBagConstraints.CENTER;
c.gridwidth = 1;
c.gridx = 1;
c.gridy = 2;
gridbag.setConstraints(button, c);
p1.add(button);
button.addActionListener(new ButtonHandler());
tabbedPane.addTab(MYPANEL1, p1);
}
void showpane2() //ADD STUDENT DETAILS TAB
{
p2 = new JPanel();
p2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Add Student Details"));
p2.setLayout(gridbag);
jLabel3 = new JLabel(" Student Name : ");
jLabel3.setFont(dataFont);
c.fill = GridBagConstraints.BOTH;
c.ipady = 2;
c.ipadx = 2;
c.gridx = 0;
c.gridy = 1;
gridbag.setConstraints(jLabel3, c);
p2.add(jLabel3);
c.ipady = 2;
c.ipadx = 2;
c.gridx = 1;
c.gridy = 1;
gridbag.setConstraints(adds_name, c);
p2.add(adds_name);
jLabel4 = new JLabel(" Student Address : ");
jLabel4.setFont(dataFont);
c.ipady = 2;
c.ipadx = 2;
c.gridx = 0;
c.gridy = 2;
gridbag.setConstraints(jLabel4, c);
p2.add(jLabel4);
c.ipady = 2;
c.ipadx = 2;
c.gridx = 1;
c.gridy = 2;
gridbag.setConstraints(adds_addr, c);
p2.add(adds_addr);
jLabel5 = new JLabel(" Father's Name : ");
jLabel5.setFont(dataFont);
c.ipady = 2;
c.ipadx = 2;
c.gridx = 0;
c.gridy = 3;
gridbag.setConstraints(jLabel5, c);
p2.add(jLabel5);
c.ipady = 2;
c.ipadx = 2;
c.gridx = 1;
c.gridy = 3;
gridbag.setConstraints(adds_fname, c);
p2.add(adds_fname);
// I am checking default sex as male by using 'true' ,
// U are free to make this false
Male = new JRadioButton("Male",true);
Male.setFont(dataFont);
c.fill = GridBagConstraints.BOTH;
c.ipady = 2;
c.ipadx = 2;
c.gridx = 1;
c.gridy = 4;
gridbag.setConstraints(Male, c);
Male.addActionListener(rlistener);
bg.add(Male);
p2.add(Male);
Female = new JRadioButton("Female",false);
Female.setFont(dataFont);
c.fill = GridBagConstraints.BOTH;
c.ipady = 2;
c.ipadx = 2;
c.gridx = 2;
c.gridy = 4;
gridbag.setConstraints(Female, c);
Female.addActionListener(rlistener);
bg.add(Female);
p2.add(Female);
jLabel6 = new JLabel(" Class : ");
jLabel6.setFont(dataFont);
c.ipady = 2;
c.ipadx = 2;
c.gridx = 0;
c.gridy = 5;
gridbag.setConstraints(jLabel6, c);
p2.add(jLabel6);
jCombobox1 = new JComboBox();
// Fonts inside combo box be datafont i,e courier new
jCombobox1.setFont(dataFont);
// Adding Elements to combo box
jCombobox1.addItem("1st PUC");
jCombobox1.addItem("2nd PUC");
// fill it align properly with other Elements inside TAB
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(10,0,0,30);
c.ipady = 2;
c.ipadx = 2;
c.gridx = 1;
c.gridy = 5;
jCombobox1.addItemListener(cbListener);
gridbag.setConstraints(jCombobox1, c);
p2.add(jCombobox1);
jCombobox2 = new JComboBox();
jCombobox2.setFont(dataFont);
// Adding Elements to combo box
jCombobox2.addItem("Section A");
jCombobox2.addItem("Section B");
jCombobox2.addItem("Section C");
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(10,0,0,30);
c.ipady = 2;
c.ipadx = 2;
c.gridx = 2;
c.gridy = 5;
jCombobox2.addItemListener(cbListener);
gridbag.setConstraints(jCombobox2, c);
p2.add(jCombobox2);
SSLC = new JCheckBox("SSLC Marks Card ");
//Using a Check Box with some Text here
c.fill = GridBagConstraints.BOTH;
c.ipady = 0;
c.ipadx = 0;
c.gridx = 1;
c.gridy = 6;
gridbag.setConstraints(SSLC, c);
SSLC.setFont(dataFont);
SSLC.addItemListener(myListener);
p2.add(SSLC);
PHOTO = new JCheckBox("PHOTO (Passport Size) ");
c.ipady = 0;
c.ipadx = 0;
c.gridx = 2;
c.gridy = 6;
gridbag.setConstraints(PHOTO, c);
PHOTO.setFont(dataFont);
PHOTO.addItemListener(myListener);
p2.add(PHOTO);
CHARCERT = new JCheckBox("Charecter Certificater");
c.ipady = 0;
c.ipadx = 0;
c.gridx = 1;
c.gridy = 7;
gridbag.setConstraints(CHARCERT, c);
CHARCERT.setFont(dataFont);
CHARCERT.addItemListener(myListener);
p2.add(CHARCERT);
NCCCERT = new JCheckBox("NCC Certificate Given");
c.ipady = 0;
c.ipadx = 0;
c.gridx = 2;
c.gridy = 7;
gridbag.setConstraints(NCCCERT, c);
NCCCERT.setFont(dataFont);
NCCCERT.addItemListener(myListener);
p2.add(NCCCERT);
jLabelim = new JLabel(" Image Name : ");
jLabelim.setFont(dataFont);
c.ipady = 2;
c.ipadx = 2;
c.gridx = 0;
c.gridy = 8;
gridbag.setConstraints(jLabelim, c);
p2.add(jLabelim);
c.ipady = 2;
c.ipadx = 2;
c.gridx = 1;
c.gridy = 8;
gridbag.setConstraints(adds_imname, c);
p2.add(adds_imname);
ADDREC = new JButton("SAVE RECORD");
c.ipady = 2;
c.ipadx = 2;
c.gridx = 2;
c.gridy = 8;
gridbag.setConstraints(ADDREC, c);
// Handle "SAVERECORD" button clicked event by "ButtonHandler()"
ADDREC.addActionListener(new ButtonHandler());
p2.add(ADDREC);
jLabel7 = new JLabel(" ", ii, JLabel.CENTER);
c.fill = GridBagConstraints.BOTH;
c.gridwidth = 5;
c.gridx = 0;
c.gridy = 9;
gridbag.setConstraints(jLabel7, c);
p2.add(jLabel7);
tabbedPane.addTab(MYPANEL2, p2);
}
void showpane3() //MODIFY STUDENT DETAILS TAB
{
p3 = new JPanel();
p3.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Modify Student Details"));
p3.setLayout(gridbag);
jCombobox3 = new JComboBox();
jCombobox3.addItem("Select Student : ");
jCombobox3.setFont(dataFont);
c2.fill = GridBagConstraints.BOTH;
c2.ipady = 2;
c2.ipadx = 2;
c2.gridx = 2;
c2.gridy = 1;
gridbag.setConstraints(jCombobox3, c2);
jCombobox3.addItemListener(cbListener);
p3.add(jCombobox3);
JLabel jl8 = new JLabel(" Student Name : ");
jl8.setFont(dataFont);
c2.fill = GridBagConstraints.BOTH;
c2.ipady = 2;
c2.ipadx = 2;
c2.gridx = 0;
c2.gridy = 1;
gridbag.setConstraints(jl8, c2);
p3.add(jl8);
mods_name = new JTextField(20);
c2.ipady = 2;
c2.ipadx = 2;
c2.gridx = 1;
c2.gridy = 1;
gridbag.setConstraints(mods_name, c2);
p3.add(mods_name);
JLabel jl9 = new JLabel(" Student Address : ");
jl9.setFont(dataFont);
c2.fill = GridBagConstraints.BOTH;
c2.ipady = 2;
c2.ipadx = 2;
c2.gridx = 0;
c2.gridy = 2;
gridbag.setConstraints(jl9, c2);
p3.add(jl9);
mods_addr = new JTextField(20);
c2.ipady = 2;
c2.ipadx = 2;
c2.gridx = 1;
c2.gridy = 2;
gridbag.setConstraints(mods_addr, c2);
p3.add(mods_addr);
jLabel5 = new JLabel(" Father's Name : ");
jLabel5.setFont(dataFont);
c2.ipady = 2;
c2.ipadx = 2;
c2.gridx = 0;
c2.gridy = 3;
gridbag.setConstraints(jLabel5, c2);
p3.add(jLabel5);
c2.ipady = 2;
c2.ipadx = 2;
c2.gridx = 1;
c2.gridy = 3;
gridbag.setConstraints(mods_fname, c2);
p3.add(mods_fname);
mMale = new JRadioButton("Male",true);
mMale.setFont(dataFont);
c2.fill = GridBagConstraints.BOTH;
c2.ipady = 2;
c2.ipadx = 2;
c2.gridx = 1;
c2.gridy = 4;
gridbag.setConstraints(mMale, c2);
mMale.addActionListener(rlistener);
bg2.add(mMale);
p3.add(mMale);
mFemale = new JRadioButton("Female",false);
mFemale.setFont(dataFont);
c2.fill = GridBagConstraints.BOTH;
c2.ipady = 2;
c2.ipadx = 2;
c2.gridx = 2;
c2.gridy = 4;
gridbag.setConstraints(mFemale, c2);
bg2.add(mFemale);
mFemale.addActionListener(rlistener);
p3.add(mFemale);
jLabel6 = new JLabel(" Class : ");
jLabel6.setFont(dataFont);
c2.ipady = 2;
c2.ipadx = 2;
c2.gridx = 0;
c2.gridy = 5;
gridbag.setConstraints(jLabel6, c2);
p3.add(jLabel6);
mjCombobox1 = new JComboBox();
mjCombobox1.setFont(dataFont);
mjCombobox1.addItem("1st PUC");
mjCombobox1.addItem("2nd PUC");
c2.fill = GridBagConstraints.BOTH;
c2.insets = new Insets(10,0,0,30);
c2.ipady = 2;
c2.ipadx = 2;
c2.gridx = 1;
c2.gridy = 5;
mjCombobox1.addItemListener(cbListener);
gridbag.setConstraints(mjCombobox1, c2);
p3.add(mjCombobox1);
mjCombobox2 = new JComboBox();
mjCombobox2.setFont(dataFont);
mjCombobox2.addItem("Section A");
mjCombobox2.addItem("Section B");
mjCombobox2.addItem("Section C");
c2.fill = GridBagConstraints.BOTH;
c2.insets = new Insets(10,0,0,30);
c2.ipady = 2;
c2.ipadx = 2;
c2.gridx = 2;
c2.gridy = 5;
mjCombobox2.addItemListener(cbListener);
gridbag.setConstraints(mjCombobox2, c2);
p3.add(mjCombobox2);
mSSLC = new JCheckBox("SSLC Marks Card ");
c2.fill = GridBagConstraints.BOTH;
c2.ipady = 0;
c2.ipadx = 0;
c2.gridx = 1;
c2.gridy = 6;
gridbag.setConstraints(mSSLC, c2);
mSSLC.setFont(dataFont);
mSSLC.addItemListener(myListener);
p3.add(mSSLC);
mPHOTO = new JCheckBox("PHOTO (Passport Size) ");
c2.ipady = 0;
c2.ipadx = 0;
c2.gridx = 2;
c2.gridy = 6;
gridbag.setConstraints(mPHOTO, c2);
mPHOTO.setFont(dataFont);
mPHOTO.addItemListener(myListener);
p3.add(mPHOTO);
mCHARCERT = new JCheckBox("Charecter Certificater");
c2.ipady = 0;
c2.ipadx = 0;
c2.gridx = 1;
c2.gridy = 7;
gridbag.setConstraints(mCHARCERT, c2);
mCHARCERT.setFont(dataFont);
mCHARCERT.addItemListener(myListener);
p3.add(mCHARCERT);
mNCCCERT = new JCheckBox("NCC Certificate Given");
c2.ipady = 0;
c2.ipadx = 0;
c2.gridx = 2;
c2.gridy = 7;
gridbag.setConstraints(mNCCCERT, c2);
mNCCCERT.setFont(dataFont);
mNCCCERT.addItemListener(myListener);
p3.add(mNCCCERT);
DELETE = new JButton("DELETE RECORD");
c2.ipady = 2;
c2.ipadx = 2;
c2.gridx = 1;
c2.gridy = 8;
gridbag.setConstraints(DELETE, c2);
p3.add(DELETE);
DELETE.addActionListener(new ButtonHandler());
MODIFY = new JButton("MODIFY RECORD");
c2.ipady = 2;
c2.ipadx = 2;
c2.gridx = 2;
c2.gridy = 8;
gridbag.setConstraints(MODIFY, c2);
p3.add(MODIFY);
MODIFY.addActionListener(new ButtonHandler());
jLabel7 = new JLabel(" ", ii, JLabel.CENTER);
c2.fill = GridBagConstraints.BOTH;
c2.gridwidth = 5;
c2.gridx = 0;
c2.gridy = 10;
gridbag.setConstraints(jLabel7, c2);
p3.add(jLabel7);
tabbedPane.addTab(MYPANEL3, p3);
accessDBinit();
}
void showpane2345() // ACCOUNTANT LOGGEDIN
{
showpane2();
showpane3();
}
void accessDB() //Verify Login for username 'a' and password 'a'
{
try
{
String var1 = loginname.getText();
var1 = var1.trim();
String var2 = loginpass.getText();
var2 = var2.trim();
//JDBC step 1 : Write your SQL (structured query language)
sql = "SELECT * FROM login WHERE username='"+var1+"' AND password='"+var2+"'";
//JDBC step 2 : using driver="sun.jdbc.odbc.JdbcOdbcDriver"; for MSACCESS
Class.forName(driver);
//JDBC step 3 : using driver="sun.jdbc.odbc.JdbcOdbcDriver";
Connection connection=DriverManager.getConnection(url);
//JDBC step 4 : url="jdbc:odbc:james"; ODBC Comes into picture here
Statement statement = connection.createStatement();
//JDBC step 5 : Get JDBC connection
boolean hasResults = statement.execute(sql);
//JDBC step 6 : Execute your SQL (see if it has any results)
if(hasResults)
{
ResultSet result = statement.getResultSet();
//JDBC step 7 : if there are results Get the "SET"
if(result!=null)
{
//take this "SET" of results from "SQL"
//call this method "isplayResults(ResultSet r)" and pass ResultSet to it
displayResults(result);
}
//JDBC step 8 : Close Database Connection u Established
connection.close();
}
}
catch(Exception ex)
{
}
}
void displayResults(ResultSet r) throws SQLException
{
ResultSetMetaData rmeta = r.getMetaData();
//Get Metadata from resultset
int foundrec = 0;
int numColumns=rmeta.getColumnCount();
while(r.next())
{
String param3 = r.getString(3).trim();
// 3rd field in the table 'login' , database 'james.mdb'
if (param3.equals("Accounts"))
{
// for login 'a' , password 'a' , if his deparment is "Accounts"
// u found record , so set foundrecord = 1
foundrec = 1;
//remove login TAB
tabbedPane.removeTabAt(0);
// Show other panes 2-3-4-5
showpane2345();
}
}
if(foundrec==0)
{
//if foundrecord is zero , invalid login
dialogmessage = "Please Re-Login";
dialogtype = JOptionPane.INFORMATION_MESSAGE;
//show message
JOptionPane.showMessageDialog((Component) null, dialogmessage, dialogf, dialogtype);
//make login and password textboxes empty
loginname.setText(" ");
loginpass.setText(" ");
}
}
void accessDBADD()
{
try
{
Class.forName(driver);
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
//Generate new register number
String query = "SELECT * FROM student_det";
ResultSet rs = statement.executeQuery(query);
int cnt = 0;
while (rs.next())
{
cnt++;
}
cnt = cnt+1;
char ys = Yearsel.charAt(0);
char ss = Sectionsel.charAt(8);
reg_no = "02-PCM-"+ys+""+ss+"-"+cnt;
String tempimname = adds_imname.getText().trim();
sql = "INSERT INTO student_det (StudentID, Name, Address, FName, Sex,
Yearsel, Sectionsel, SSLC, PHOTO, CHARCERT, NCCCERT, imagename) VALUES ('"+reg_no+"','"+adds_name.getText()+"','"+adds_addr.getText()+"',
'"+adds_fname.getText()+"','"+Sexsel+"','"+Yearsel+"','"+Sectionsel+"',
"+SSLCsel+","+PHOTOsel+","+CHARCERTsel+","+NCCCERTsel+",'"+tempimname+"')";
statement.executeUpdate(sql);
connection.close();
// add this new record to our JTable too (DATAGRID)
data[0] = reg_no;
data[1] = adds_name.getText();
data[2] = adds_addr.getText();
data[3] = adds_fname.getText();
defaulttablemodel.addRow(data);
dialogmessage = " "+adds_name.getText().toUpperCase()+" has Registration Number : "+reg_no;
dialogtype = JOptionPane.INFORMATION_MESSAGE;
//show user the new register number and new record holder name
JOptionPane.showMessageDialog((Component) null, dialogmessage, dialogtitle, dialogtype);
//rollback combobox selection
jCombobox1.setSelectedIndex(0); //section
jCombobox2.setSelectedIndex(0); //standard
String snametemp = adds_name.getText();
jCombobox3.addItem(adds_name.getText());
// add name to combobox in modify names TAB
//set other textboxes black to enable new record entry
adds_name.setText(" ");
adds_addr.setText(" ");
adds_fname.setText(" ");
adds_imname.setText(" ");
// uncheck checkboxes if they are checked while adding data
SSLC.setSelected(false);
PHOTO.setSelected(false);
NCCCERT.setSelected(false);
CHARCERT.setSelected(false);
//click on the radiobutton male , to make it default again
Male.doClick();
}
catch(Exception ex)
{
}
}
void accessDBMod(String nsel)
{
try
{
sql = "SELECT * FROM student_det WHERE Name='"+nsel+"'";
Class.forName(driver);
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
boolean hasResults = statement.execute(sql);
if(hasResults)
{
ResultSet result = statement.getResultSet();
if(result!=null)
{
displayResultsMod(result);
}
connection.close();
}
}
catch(Exception ex)
{
}
}
void displayResultsMod(ResultSet r) throws SQLException
{
ResultSetMetaData rmeta = r.getMetaData();
int foundrec = 0;
int numColumns=rmeta.getColumnCount();
String text2="";
String text3="";
String text4="";
String text5="";
String text6="";
String text7="";
String text8="";
int int9=0;
int int10=0;
int int11=0;
int int12=0;
while(r.next())
{
reg_no=r.getString(2);
text3+=r.getString(3);
text4+=r.getString(4);
text5+=r.getString(5);
text6+=r.getString(6);
text7+=r.getString(7);
text8+=r.getString(8);
int9=r.getInt(9);
int10=r.getInt(10);
int11=r.getInt(11);
int12=r.getInt(12);
imagename=r.getString(13);
}
jLabel7.setIcon(new ImageIcon(imagename));
currname = text3;
mods_name.setText(text3);
mods_addr.setText(text4);
mods_fname.setText(text5);
text6 = text6.trim();
if (text6.equals("Male"))
{
mMale.doClick();
}
else
{
mFemale.doClick();
}
text7 = text7.trim();
if (text7.equals("2nd PUC"))
{
mjCombobox1.setSelectedIndex(1);
}
else
{
mjCombobox1.setSelectedIndex(0);
}
text8 = text8.trim();
if (text8.equals("Section A"))
{
mjCombobox2.setSelectedIndex(0);
}
else if(text8.equals("Section B"))
{
mjCombobox2.setSelectedIndex(1);
}
else
{
mjCombobox2.setSelectedIndex(2);
}
if(int9==1)
{
mSSLC.setSelected(true);
}
else
{
mSSLC.setSelected(false);
}
if(int10==1)
{
mPHOTO.setSelected(true);
}
else
{
mPHOTO.setSelected(false);
}
if(int11==1)
{
mCHARCERT.setSelected(true);
}
else
{
mCHARCERT.setSelected(false);
}
if(int12==1)
{
mNCCCERT.setSelected(true);
}
else
{
mNCCCERT.setSelected(false);
}
}
void accessDBinit() //Get Student List in the List Box
{
try
{
sql = "SELECT Name FROM student_det ORDER BY Name";
Class.forName(driver);
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
boolean hasResults = statement.execute(sql);
if(hasResults)
{
ResultSet result = statement.getResultSet();
if(result!=null)
{
displayResultsinit(result);
}
connection.close();
}
}
catch(Exception ex)
{
}
}
void displayResultsinit(ResultSet r) throws SQLException
{
ResultSetMetaData rmeta = r.getMetaData();
int numColumns=rmeta.getColumnCount();
while(r.next())
{
for(int i=1;i<=numColumns;++i)
{
if(i<=numColumns)
{
jCombobox3.addItem(r.getString(i));
}
}
}
}
void accessDBDel(String nsel) //Delete Record for the Selected Name
{
try
{
Class.forName(driver);
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
sql = "DELETE FROM student_det WHERE Name='"+nsel+"'";
statement.executeUpdate(sql);
connection.close();
mjCombobox1.setSelectedIndex(0);
mjCombobox2.setSelectedIndex(0);
mods_name.setText(" ");
mods_addr.setText(" ");
mods_fname.setText(" ");
mSSLC.setSelected(false);
mPHOTO.setSelected(false);
mNCCCERT.setSelected(false);
mCHARCERT.setSelected(false);
mMale.doClick();
}
catch(Exception ex)
{
}
}
void updateDB() //Update Database when modified
{
try
{
sql = "UPDATE student_det SET Name='"+mods_name.getText()+"', Address='"+mods_addr.getText()+"', FName='"+mods_fname.getText()+"', Sex='"+Sexsel+"', Yearsel='"+Yearsel+"', Sectionsel='"+Sectionsel+"', SSLC="+mSSLCsel+", PHOTO="+mPHOTOsel+", CHARCERT="+mCHARCERTsel+", NCCCERT="+mNCCCERTsel+" WHERE StudentID='"+reg_no+"'";
Class.forName(driver);
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
statement.executeUpdate(sql);
connection.close();
dialogmessage = " "+mods_name.getText().toUpperCase()+" Record is Updated ";
dialogtype = JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog((Component) null, dialogmessage, dialogm, dialogtype);
}
catch(Exception ex)
{
}
}
class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent ev)
{
String s=ev.getActionCommand();
if(s=="LOGIN") //Login is Clicked
{
accessDB();
}
else if(s=="SAVE RECORD") //Save Record is Clicked
{
accessDBADD();
String snametemp = adds_name.getText();
}
else if(s=="MODIFY RECORD") //Modify Record is Clicked
{
String temp_mname = mods_name.getText().trim();
String temp_currname = currname.trim();
if(temp_mname.equals(temp_currname))
{
}
else
{
jCombobox3.addItem(mods_name.getText());
}
updateDB();
}
else if(s=="DELETE RECORD") //Delete Record Button is Clicked
{
dialogContentPane.setLayout(new FlowLayout());
dialogContentPane.add(jLabeldlg);
jLabeldlg.setFont(titleFont);
dialogContentPane.add(DELETEIT);
DELETEIT.addActionListener(new ButtonHandler());
dialogContentPane.add(DONTDELETE);
DONTDELETE.addActionListener(new ButtonHandler());
dialog.setBounds(300, 200, 250, 120);
dialog.show();
}
else if(s=="YES DELETE") //Yes Button is Clicked in Dialog Box
{
dialog.dispose();
Namesel = jCombobox3.getSelectedItem().toString();
accessDBDel(Namesel);
}
else if(s=="NO DELETE") //No Button is Clicked in Dialog Box
{
dialog.dispose();
}
}
}
class MenuItemHandler implements ActionListener
{
public void actionPerformed(ActionEvent ev)
{
String s=ev.getActionCommand();
if(s=="Exit")
{
//close window on menuitem exit
System.exit(0);
}
}
}
class WindowEventHandler extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
//close window on exit
System.exit(0);
}
}
class RadioListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Sexsel = e.getActionCommand();
condsel = e.getActionCommand();
}
}
class ComboBoxListener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
sr = (String)e.getItem();
if (e.getSource().equals(jCombobox1))
{
Yearsel = jCombobox1.getSelectedItem().toString();
}
if (e.getSource().equals(mjCombobox1))
{
Yearsel = mjCombobox1.getSelectedItem().toString();
}
if (e.getSource().equals(jCombobox2))
{
Sectionsel = jCombobox2.getSelectedItem().toString();
}
if (e.getSource().equals(mjCombobox2))
{
Sectionsel = mjCombobox2.getSelectedItem().toString();
}
if (e.getSource().equals(jCombobox3))
{
Namesel = jCombobox3.getSelectedItem().toString();
accessDBMod(Namesel);
}
if (e.getSource().equals(jComboboxq1))
{
Combo1sel = jComboboxq1.getSelectedItem().toString();
}
if (e.getSource().equals(jComboboxq2))
{
Combo2sel = jComboboxq2.getSelectedItem().toString();
}
}
}
class CheckBoxListener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
Object source = e.getItemSelectable();
if ((e.getStateChange() == ItemEvent.SELECTED) && (source==SSLC))
{
SSLCsel = 1;
}
if ((e.getStateChange() == ItemEvent.DESELECTED) && (source==SSLC))
{
SSLCsel = 0;
}
if ((e.getStateChange() == ItemEvent.SELECTED) && (source==PHOTO))
{
PHOTOsel = 1;
}
if ((e.getStateChange() == ItemEvent.DESELECTED) && (source==PHOTO))
{
PHOTOsel = 0;
}
if ((e.getStateChange() == ItemEvent.SELECTED) && (source==NCCCERT))
{
NCCCERTsel = 1;
}
if ((e.getStateChange() == ItemEvent.DESELECTED) && (source==NCCCERT))
{
NCCCERTsel = 0;
}
if ((e.getStateChange() == ItemEvent.SELECTED) && (source==CHARCERT))
{
CHARCERTsel = 1;
}
if ((e.getStateChange() == ItemEvent.DESELECTED) && (source==CHARCERT))
{
CHARCERTsel = 0;
}
if ((e.getStateChange() == ItemEvent.SELECTED) && (source==mSSLC))
{
mSSLCsel = 1;
}
if ((e.getStateChange() == ItemEvent.DESELECTED) && (source==mSSLC))
{
mSSLCsel = 0;
}
if ((e.getStateChange() == ItemEvent.SELECTED) && (source==mPHOTO))
{
mPHOTOsel = 1;
}
if ((e.getStateChange() == ItemEvent.DESELECTED) && (source==mPHOTO))
{
mPHOTOsel = 0;
}
if ((e.getStateChange() == ItemEvent.SELECTED) && (source==mNCCCERT))
{
mNCCCERTsel = 1;
}
if ((e.getStateChange() == ItemEvent.DESELECTED) && (source==mNCCCERT))
{
mNCCCERTsel = 0;
}
if ((e.getStateChange() == ItemEvent.SELECTED) && (source==mCHARCERT))
{
mCHARCERTsel = 1;
}
if ((e.getStateChange() == ItemEvent.DESELECTED) && (source==mCHARCERT))
{
mCHARCERTsel = 0;
}
if ((e.getStateChange() == ItemEvent.SELECTED) && (source==jCheckboxq1))
{
StudentIDsel = 1;
}
if ((e.getStateChange() == ItemEvent.DESELECTED) && (source==jCheckboxq1))
{
StudentIDsel = 0;
}
if ((e.getStateChange() == ItemEvent.SELECTED) && (source==jCheckboxq2))
{
StudentNamesel = 1;
}
if ((e.getStateChange() == ItemEvent.DESELECTED) && (source==jCheckboxq2))
{
StudentNamesel = 0;
}
if ((e.getStateChange() == ItemEvent.SELECTED) && (source==jCheckboxq3))
{
Addresssel = 1;
}
if ((e.getStateChange() == ItemEvent.DESELECTED) && (source==jCheckboxq3))
{
Addresssel = 0;
}
if ((e.getStateChange() == ItemEvent.SELECTED) && (source==jCheckboxq4))
{
FathersNamesel = 1;
}
if ((e.getStateChange() == ItemEvent.DESELECTED) && (source==jCheckboxq4))
{
FathersNamesel = 0;
}
if ((e.getStateChange() == ItemEvent.SELECTED) && (source==jCheckboxq5))
{
qSexsel = 1;
}
if ((e.getStateChange() == ItemEvent.DESELECTED) && (source==jCheckboxq5))
{
qSexsel = 0;
}
}
}
}
The above prograam looks like below -
Monday, January 18, 2010
/*
* This is a simple Text editor(like NotePad) program written in java.
* @Tapas kumar jena
* mail-tapas.friends@gmail.com
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.applet.*;
import java.io.*;
import javax.swing.*;
import javax.swing.undo.*;
import java.util.Hashtable;
class UndoableTextArea extends TextArea implements StateEditable {
//Variable declaration
private final static String KEY_STATE="UndoableTextAreaKey";
private boolean textChanged=false;
private UndoManager undoManager;
private StateEdit currentEdit;
public UndoableTextArea(){
super();
initUndoable();
}
public UndoableTextArea(String string){
super(string);
initUndoable();
}
public UndoableTextArea(int rows,int columns){
super(rows,columns);
initUndoable();
}
public UndoableTextArea(String string,int rows,int columns){
super(string,rows,columns);
initUndoable();
}
public UndoableTextArea(String string,int rows,int columns,int scrollbars){
super(string,rows,columns,scrollbars);
initUndoable();
}
public boolean undo(){
try{
undoManager.undo();
return true;
}
catch(CannotUndoException e){
System.out.println("cannot undo");
return false;
}
}
public boolean redo(){
try{
undoManager.redo();
return true;
}
catch(CannotRedoException e){
System.out.println("cannot redo");
return false;
}
}
public void storeState(Hashtable state){
state.put(KEY_STATE,getText());
}
public void restoreState(Hashtable state){
Object data=state.get(KEY_STATE);
if(data!=null){
setText((String)data);
}
}
private void takeSnapshot(){
if(textChanged){
currentEdit.end();
undoManager.addEdit(currentEdit);
textChanged=false;
currentEdit=new StateEdit(this);
}
}
private void initUndoable(){
undoManager =new UndoManager();
currentEdit=new StateEdit(this);
addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent event){
if(event.isActionKey()){
takeSnapshot();
}
}
});
addFocusListener(new FocusAdapter(){
public void focusLost(FocusEvent fe){
takeSnapshot();
}
});
addTextListener(new TextListener(){
public void textValueChanged(TextEvent e){
textChanged=true;
takeSnapshot();
}
});
}
}
//Main class for TextEditor
public class TextEditor extends Frame {
//Components
boolean b=true;
Frame fm;
FileDialog fd;
Font f;
int style=Font.PLAIN;
int fsize=12;
UndoableTextArea txt;
String filename,st,fn="untitled",dn;
Clipboard clip=getToolkit().getSystemClipboard();
//Constructor
TextEditor(){
f=new Font("Courier",style,fsize);
setLayout(new GridLayout(1,1));
txt=new UndoableTextArea(80,25);
txt.setFont(f);
add(txt);
MenuBar mb=new MenuBar();
Menu fonttype=new Menu("FontType");
MenuItem one,two,three,four,five,six;
one=new MenuItem("TimesRoman");
two=new MenuItem("Helvetica");
three=new MenuItem("Courier");
four=new MenuItem("Arial");
five=new MenuItem("Arial Black");
six=new MenuItem("Century");
fonttype.add(one);
fonttype.add(two);
fonttype.add(three);
fonttype.add(four);
fonttype.add(five);
fonttype.add(six);
one.addActionListener(new Type());
two.addActionListener(new Type());
three.addActionListener(new Type());
four.addActionListener(new Type());
five.addActionListener(new Type());
six.addActionListener(new Type());
Menu fontmenu=new Menu("Font");
MenuItem boldmenu=new MenuItem("Bold");
MenuItem plainmenu=new MenuItem("Plain");
MenuItem italicmenu=new MenuItem("Italic");
fontmenu.add(boldmenu);
fontmenu.add(plainmenu);
fontmenu.add(italicmenu);
boldmenu.addActionListener(new FM());
plainmenu.addActionListener(new FM());
italicmenu.addActionListener(new FM());
Menu size=new Menu("Size");
MenuItem s1,s2,s3,s4,s5,s6,s7,s8,s9,s10;
s1=new MenuItem("10");
s2=new MenuItem("12");
s3=new MenuItem("14");
s4=new MenuItem("16");
s5=new MenuItem("18");
s6=new MenuItem("20");
s7=new MenuItem("22");
s8=new MenuItem("24");
s9=new MenuItem("26");
s10=new MenuItem("28");
size.add(s1);
size.add(s2);
size.add(s3);
size.add(s4);
size.add(s5);
size.add(s6);
size.add(s7);
size.add(s8);
size.add(s9);
size.add(s10);
s1.addActionListener(new Size());
s2.addActionListener(new Size());
s3.addActionListener(new Size());
s4.addActionListener(new Size());
s5.addActionListener(new Size());
s6.addActionListener(new Size());
s7.addActionListener(new Size());
s8.addActionListener(new Size());
s9.addActionListener(new Size());
s10.addActionListener(new Size());
size.addActionListener(new FM());
fontmenu.add(size);
Menu file=new Menu("File");
MenuItem n=new MenuItem("New",new
MenuShortcut(KeyEvent.VK_N));
MenuItem o=new MenuItem("Open",new
MenuShortcut(KeyEvent.VK_O));
MenuItem s=new MenuItem("Save",new
MenuShortcut(KeyEvent.VK_S));
MenuItem e=new MenuItem("Exit",new
MenuShortcut(KeyEvent.VK_E));
n.addActionListener(new New());
file.add(n);
o.addActionListener(new Open());
file.add(o);
s.addActionListener(new Save());
file.add(s);
e.addActionListener(new Exit());
file.add(e);
mb.add(file);
addWindowListener(new Win());
Menu edit=new Menu("Edit");
MenuItem cut=new MenuItem("Cut",new
MenuShortcut(KeyEvent.VK_X));
MenuItem copy=new MenuItem("Copy",new
MenuShortcut(KeyEvent.VK_C));
MenuItem paste=new MenuItem("Paste",new
MenuShortcut(KeyEvent.VK_V));
cut.addActionListener(new Cut());
edit.add(cut);
copy.addActionListener(new Copy());
edit.add(copy);
paste.addActionListener(new Paste());
edit.add(paste);
Menu color =new Menu("Color");
MenuItem Bg=new MenuItem("Background");
MenuItem Fg=new MenuItem("Foreground");
Bg.addActionListener(new BC());
Fg.addActionListener(new BC());
Menu undo=new Menu("Undo&Redo");
MenuItem un=new MenuItem("Undo");
MenuItem re=new MenuItem("Redo");
re.addActionListener(new WW());
un.addActionListener(new WW());
undo.add(un);
undo.add(re);
color.add(Bg);
color.add(Fg);
mb.add(edit);
mb.add(fontmenu);
mb.add(fonttype);
mb.add(color);
mb.add(undo);
setMenuBar(mb);
mylistener mylist=new mylistener();
addWindowListener(mylist);
}
//Action performed defination
class WW implements ActionListener {
public void actionPerformed(ActionEvent e){
String se=e.getActionCommand();
if(se.equals("Undo"))
txt.undo();
if(se.equals("Redo"))
txt.redo();
}
}
//Event for window closeing
class mylistener extends WindowAdapter {
public void windowClosing(WindowEvent we){
if(!b)
System.exit(0);
}
}
//--Action performed for Menu bar options start--
class New implements ActionListener {
public void actionPerformed(ActionEvent ae){
txt.setText(" ");
setTitle(filename);
fn="Untitled";
}
}
class Open implements ActionListener {
public void actionPerformed(ActionEvent ae){
FileDialog fd=new FileDialog(TextEditor.this,"Select File",FileDialog.LOAD);
fd.show();
if((fn=fd.getFile())!=null){
filename=fd.getDirectory()+fd.getFile();
dn=fd.getDirectory();
setTitle(filename);
readFile();
}
txt.requestFocus();
}
}
class Save implements ActionListener {
public void actionPerformed(ActionEvent ae){
FileDialog fd=new FileDialog(TextEditor.this,"Save File",FileDialog.SAVE);
fd.setFile(fn);
fd.setDirectory(dn);
fd.show();
if(fd.getFile()!=null){
filename=fd.getDirectory()+fd.getFile();
setTitle(filename);
writeFile();
txt.requestFocus();
}
}
}
class Exit implements ActionListener {
public void actionPerformed(ActionEvent ae){
System.exit(0);
}
}
void readFile() {
BufferedReader d;
StringBuffer sb=new StringBuffer();
try{
d=new BufferedReader(new FileReader(filename));
String line;
while((line=d.readLine())!=null)
sb.append(line+"");
txt.setText(sb.toString());
d.close();
}catch(FileNotFoundException e){
System.out.println("File not found");
}catch(IOException e){ }
}
public void writeFile() {
try{
DataOutputStream d=new DataOutputStream(new
FileOutputStream(filename));
String line=txt.getText();
BufferedReader br=new BufferedReader(new
StringReader(line));
while((line=br.readLine())!=null){
d.writeBytes(line+"");
}
d.close();
}catch(Exception e){
System.out.println("File not found");
}
}
class Cut implements ActionListener {
public void actionPerformed(ActionEvent ae){
String sel=txt.getSelectedText();
StringSelection ss=new StringSelection(sel);
clip.setContents(ss,ss);
txt.replaceRange("",txt.getSelectionStart(),txt.getSelectionEnd());
}
}
class Copy implements ActionListener {
public void actionPerformed(ActionEvent ae){
String sel=txt.getSelectedText();
StringSelection clipstring=new StringSelection(sel);
clip.setContents(clipstring,clipstring);
}
}
class Paste implements ActionListener{
public void actionPerformed(ActionEvent ae){
Transferable cliptran=clip.getContents(TextEditor.this);
try{
String sel=(String)cliptran.getTransferData(DataFlavor.stringFlavor);
txt.replaceRange(sel,txt.getSelectionStart(),txt.getSelectionEnd());
}catch(Exception e){
System.out.println("not starting flavor");
}
}
}
//Confirmation before Closing Application
class Win extends WindowAdapter{
public void windowClosing(WindowEvent we){
ConfirmDialog cd=new ConfirmDialog();
if(!b)
System.exit(0);
}
}
class ConfirmDialog extends JPanel {
public ConfirmDialog(){
int result;
result=JOptionPane.showConfirmDialog(this,fn+" not saved do you want to save");
switch(result){
case JOptionPane.YES_OPTION:
FileDialog fd=new FileDialog(TextEditor.this,"Save File",FileDialog.SAVE);
fd.setFile(fn);
fd.setDirectory(dn);
fd.show();
if(fd.getFile()!=null){
filename=fd.getDirectory()+fd.getFile();
setTitle(filename);
writeFile();
txt.requestFocus();
}
System.out.println("Yes button pressed");
break;
case JOptionPane.NO_OPTION:
dispose();
System.exit(0);
System.out.println("NO button pressed");
break;
case JOptionPane.CANCEL_OPTION:
setVisible(true);
repaint();
System.out.println("Cancel button pressed");
break;
case JOptionPane.CLOSED_OPTION:
System.out.println("Closed button pressed");
break;
}
}
}
class Size implements ActionListener {
public void actionPerformed(ActionEvent e){
int style=f.getStyle();
String w=e.getActionCommand();
if(w=="10"){
f= new Font("Courier",style,10);
txt.setFont(f);
fsize=f.getSize();
repaint();
}
if(w=="12"){
f= new Font("Courier",style,12);
fsize=f.getSize();
txt.setFont(f);
repaint();
}
if(w=="14"){
f= new Font("Courier",style,14);
fsize=f.getSize();
txt.setFont(f);
repaint();
}
if(w=="16"){
f= new Font("Courier",style,16);
txt. setFont(f);
fsize=f.getSize();
repaint();
}
if(w=="18"){
f= new Font("Courier",style,18);
txt.setFont(f);
fsize=f.getSize();
repaint();
}
if(w=="20"){
f= new Font("Courier",style,20);
txt.setFont(f);
fsize=f.getSize();
repaint();
}
if(w=="22"){
f= new Font("Courier",style,22);
txt.setFont(f);
fsize=f.getSize();
repaint();
}
if(w=="24"){
f= new Font("Courier",style,24);
txt.setFont(f);
fsize=f.getSize();
repaint();
}
if(w=="26"){
f= new Font("Courier",style,26);
txt.setFont(f);
fsize=f.getSize();
repaint();
}
if(w=="28"){
f= new Font("Courier",style,28);
txt.setFont(f);
fsize=f.getSize();
repaint();
}
}
}
//Class for Font handling
class FM extends Applet implements ActionListener{
public void actionPerformed(ActionEvent e){
String b=e.getActionCommand();
if(b=="Bold"){
f= new Font("Courier",Font.BOLD,fsize);
style=f.getStyle();
txt.setFont(f);
}
if(b=="Plain"){
f= new Font("Courier",Font.PLAIN,fsize);
style=f.getStyle();
txt.setFont(f);
}
if(b=="Italic"){
f= new Font("Courier",Font.ITALIC,fsize);
txt.setFont(f);
}
repaint();
}
}
//Class for FontType handling
class Type implements ActionListener {
public void actionPerformed(ActionEvent e){
String lbl=e.getActionCommand();
if(lbl=="TimesRoman"){
f=new Font("TimesRoman",style,fsize);
txt.setFont(f);
}
if(lbl=="Courier"){
f=new Font("Courier",style,fsize);
txt.setFont(f);
}
if(lbl=="Helvetica"){
f=new Font("Helvetica",style,fsize);
txt.setFont(f);
}
if(lbl=="Arial"){
f=new Font("Arial",style,fsize);
txt.setFont(f);
}
if(lbl=="Arial Black"){
f=new Font("ArialBlack",style,fsize);
txt.setFont(f);
}
if(lbl=="Century"){
f=new Font("Century",style,fsize);
txt.setFont(f);
}
repaint();
}
}
//Class for Background Color
class BC implements ActionListener {
public void actionPerformed(ActionEvent e){
st=e.getActionCommand();
JFrame jf=new JFrame("JColorChooser");
colorChooser c=new colorChooser();
c.setSize(400,300);
c.setVisible(true);
}
}
//Class for color chooser
class colorChooser extends JFrame {
Button ok;
JColorChooser jcc;
public colorChooser(){
setTitle("JColorChooser");
jcc=new JColorChooser();
JPanel content=(JPanel)getContentPane();
content.setLayout(new BorderLayout());
content.add(jcc,"Center");
ok=new Button("OK");
content.add(ok,"South");
ok.addActionListener(new B());
}
class B implements ActionListener {
public void actionPerformed(ActionEvent e){
System.out.println("Color Is:"+jcc.getColor().toString());
if(st.equals("Background"))
txt.setBackground(jcc.getColor());
if(st.equals("Foreground"))
txt.setForeground(jcc.getColor());
setVisible(false);
}
}
}//--Action performed for Menu bar options end--
//Main method of the application
public static void main(String args[]) {
Frame fm=new TextEditor();
fm.setSize(800,600);
fm.setVisible(true);
fm.show();
}
}
The above prograam looks like below -Thursday, January 14, 2010
Calculator.java
/**
*This is a simple calculator program written in java.
*@author Tapas kumar jena
*@mail tapas.friends@gmail.com
*/
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JApplet {
//initialization
public void init() {
CalculatorPanel calc = new CalculatorPanel();
getContentPane().add(calc);
}
}
/*
* This the class which actually makes the Calculator components and arrange them.
*/
class CalculatorPanel extends JPanel implements ActionListener {
//Declare required components
JButton n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,plus,minus,mul,div,dot, equal;
static JTextField result = new JTextField("0", 45);
static String lastCommand = null;
JOptionPane p = new JOptionPane();
double preRes = 0, secVal = 0, res;
private static void assign(String no) {
if ((result.getText()).equals("0"))
result.setText(no);
else if (lastCommand == "=") {
result.setText(no);
lastCommand = null;
} else
result.setText(result.getText() + no);
}
//Default constructor
public CalculatorPanel() {
setLayout(new BorderLayout());
result.setEditable(false);
result.setSize(300, 200);
add(result, BorderLayout.NORTH);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));
n7 = new JButton("7");
panel.add(n7);
n7.addActionListener(this);
n8 = new JButton("8");
panel.add(n8);
n8.addActionListener(this);
n9 = new JButton("9");
panel.add(n9);
n9.addActionListener(this);
div = new JButton("/");
panel.add(div);
div.addActionListener(this);
n4 = new JButton("4");
panel.add(n4);
n4.addActionListener(this);
n5 = new JButton("5");
panel.add(n5);
n5.addActionListener(this);
n6 = new JButton("6");
panel.add(n6);
n6.addActionListener(this);
mul = new JButton("*");
panel.add(mul);
mul.addActionListener(this);
n1 = new JButton("1");
panel.add(n1);
n1.addActionListener(this);
n2 = new JButton("2");
panel.add(n2);
n2.addActionListener(this);
n3 = new JButton("3");
panel.add(n3);
n3.addActionListener(this);
minus = new JButton("-");
panel.add(minus);
minus.addActionListener(this);
dot = new JButton(".");
panel.add(dot);
dot.addActionListener(this);
n0 = new JButton("0");
panel.add(n0);
n0.addActionListener(this);
equal = new JButton("=");
panel.add(equal);
equal.addActionListener(this);
plus = new JButton("+");
panel.add(plus);
plus.addActionListener(this);
add(panel, BorderLayout.CENTER);
}//constructor end
//Define Actions and calculation part
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == n1)
assign("1");
else if (ae.getSource() == n2)
assign("2");
else if (ae.getSource() == n3)
assign("3");
else if (ae.getSource() == n4)
assign("4");
else if (ae.getSource() == n5)
assign("5");
else if (ae.getSource() == n6)
assign("6");
else if (ae.getSource() == n7)
assign("7");
else if (ae.getSource() == n8)
assign("8");
else if (ae.getSource() == n9)
assign("9");
else if (ae.getSource() == n0)
assign("0");
else if (ae.getSource() == dot) {
if (((result.getText()).indexOf(".")) == -1)
result.setText(result.getText() + ".");
}else if (ae.getSource() == minus) {
preRes = Double.parseDouble(result.getText());
lastCommand = "-";
result.setText("0");
}else if (ae.getSource() == div) {
preRes = Double.parseDouble(result.getText());
lastCommand = "/";
result.setText("0");
}else if (ae.getSource() == equal) {
secVal = Double.parseDouble(result.getText());
if (lastCommand.equals("/"))
res = preRes / secVal;
else if (lastCommand.equals("*"))
res = preRes * secVal;
else if (lastCommand.equals("-"))
res = preRes - secVal;
else if (lastCommand.equals("+"))
res = preRes + secVal;
result.setText(" " + res);
lastCommand = "=";
}else if (ae.getSource() == mul) {
preRes = Double.parseDouble(result.getText());
lastCommand = "*";
result.setText("0");
}else if (ae.getSource() == plus) {
preRes = Double.parseDouble(result.getText());
lastCommand = "+";
result.setText("0");
}
}//actionPerformed end
}
The above prograam looks like below -
Subscribe to:
Comments (Atom)



