<div style='background-color: none transparent;'></div>
Home » » Student & Libary database using file& Swing

Student & Libary database using file& Swing

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.text.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.border.*;
import javax.swing.colorchooser.*;
import javax.swing.filechooser.*;
import javax.accessibility.*;

import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.util.*;
import java.io.*;
import java.applet.*;
import java.net.*;

import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;

public class SampleDesktop13 extends JFrame implements ActionListener,ListSelectionListener
{
public JDesktopPane desk;
//JTabbedPane fpane = new JTabbedPane(SwingConstants.TOP);
Properties ht = new Properties();
Properties ht1 = new Properties();
String name,fname,number,add,tele,qua,sums,email;
String newSelection;
JMenuBar mb;
JMenu menu,menu1,menu2;
JMenuItem sadd,exit,lrecord,about;
JInternalFrame hide=new JInternalFrame();
int document = 1,tabIndex=0;
JTextField[] nameField;
JList list;
DefaultListModel listModel= new DefaultListModel();
JPanel pix22 = new JPanel();


int wcoboxchange=0;
int count1=0;
JList list1;
DefaultListModel listModel1= new DefaultListModel();
JPanel pix12 = new JPanel();

GregorianCalendar gcal=new GregorianCalendar();
String idate,doi,dor;
String rdate;
int id,im,iy,id1,im1,iy1;
int listboxno;

JRadioButton allButton;
JRadioButton javaButton;
JRadioButton cButton;
JRadioButton vbButton;
JLabel stb =new JLabel("The Student Holds This Books..");

int count;
String[] isb;

ArrayList alist=new ArrayList();

String bname,isbn,booktype,author,edition,booksum;

JPanel p=new JPanel();




public SampleDesktop13(String title){

super(title);
setDefaultCloseOperation(EXIT_ON_CLOSE);

desk = new JDesktopPane();
setContentPane(desk);
mb = new JMenuBar();
menu = new JMenu("Student DataBase");
menu1 = new JMenu("Libary");
menu2 = new JMenu("Help");
setJMenuBar(menuBar());
mb.add(menu);
mb.add(menu1);
mb.add(menu2);

}

private JMenuBar menuBar(){
sadd=new JMenuItem(" Student Record");
sadd.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
sadd.addActionListener(this);
menu.add(sadd);
exit=new JMenuItem(" Exit");
exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
exit.addActionListener(this);
menu.add(exit);

lrecord=new JMenuItem(" Libary Records");
lrecord.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
lrecord.addActionListener(this);
menu1.add(lrecord);

about=new JMenuItem(" About");
about.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
about.addActionListener(this);
menu2.add(about);

return mb;
}


public void actionPerformed(ActionEvent e){
if ((e.getSource()==sadd))
hide=newFile();
if ((e.getSource()==lrecord))
hide=lrecord1();
if ((e.getSource()==about))

JOptionPane.showMessageDialog( desk,"This is Created By Prosun");
if ((e.getSource()==exit))
System.exit(0);

if ("Cancel".equals(e.getActionCommand())){

hide.dispose();}

if ("OK".equals(e.getActionCommand())){

save();
JOptionPane.showMessageDialog( hide,"One Record saved");
hide.dispose();


}
if ("Update".equals(e.getActionCommand())){
if(wcoboxchange==0){
JOptionPane.showMessageDialog( hide,"Plz select from Combo");
}else{
Update();
JOptionPane.showMessageDialog( hide,"One Record Updated");
hide.dispose();
wcoboxchange=0;}
}
if ("Delete".equals(e.getActionCommand())){
if(wcoboxchange==0){
JOptionPane.showMessageDialog( hide,"Plz select from Combo");
}else{
Delete();
JOptionPane.showMessageDialog( hide,"One Record Deleted");
hide.dispose();
wcoboxchange=0;}}
if ("Finished".equals(e.getActionCommand())){
JOptionPane.showMessageDialog( hide,"One Record Showed");

hide.dispose();}
if ("Issue".equals(e.getActionCommand())){
if(nameField[2].getText().length()<1||nameField[1].getText().length()<1)
{
JOptionPane.showMessageDialog( hide,"PLz Give value for All Fields");
if(nameField[2].getText().length()<1){
nameField[2].requestFocus();}
else{nameField[1].requestFocus();}
}
else{
Issue();
hide.dispose();}
}
if ("Return".equals(e.getActionCommand())){
if(nameField[1].getText().length()<1){
JOptionPane.showMessageDialog( hide,"PLz Give value for All Fields");
nameField[1].requestFocus();
}else{
Returnbook();
hide.dispose();}
}
if ("Add".equals(e.getActionCommand())){
if(nameField[0].getText().length()<1||nameField[1].getText().length()<1)
{
JOptionPane.showMessageDialog( hide,"PLz Give value for All Fields");
if(nameField[0].getText().length()<1){
nameField[0].requestFocus();}
else{nameField[1].requestFocus();}
}
else{
Addbook();
JOptionPane.showMessageDialog( hide,"One Book Added To Libary");
hide.dispose();}}

if ("Delete Book".equals(e.getActionCommand())){
if(nameField[0].getText().length()<1){
JOptionPane.showMessageDialog( hide,"PLz Give value for All Fields");
nameField[0].requestFocus();
}else{
Deletebook();
JOptionPane.showMessageDialog( hide,"One Book Deleted From Libary");
hide.dispose();}}


if (e.getSource() == allButton){
//for all books
nameField[2].setText("");
changelist("allbooks.txt");


}
if (e.getSource() == javaButton){
//for java books
nameField[2].setText("");
changelist("javabooks.txt");

}
if (e.getSource() == cButton){
//for c books
nameField[2].setText("");
changelist("cbooks.txt");


}

if (e.getSource() == vbButton){
//for vb books
nameField[2].setText("");
changelist("vbbooks.txt");
}


}



//list listeners

public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {

if (list.getSelectedIndex() == -1) {
}
else{
if(listboxno==0){
String sd=(String)alist.get(list.getSelectedIndex());

nameField[2].setEditable(true);
nameField[2].setText(sd);
nameField[2].setEditable(false);}
else{

////////////////////////////////////////////

int n=0;
fileopen("issuedbook.txt");


String sums1 = (String) ht.get(newSelection);

String bdetails1="";

StringTokenizer st = new StringTokenizer(sums1,"#");
while(st.hasMoreTokens())
{
n++;
nameField[n].setEditable(true);

nameField[n].setText(st.nextToken());
nameField[n].setEditable(false);


}

///////////////////////////


nameField[4].setEditable(true);
nameField[4].setText(idate);

nameField[4].setEditable(false);
}
}
}
}



//list listner upto here


private void changelist(String s){
int n=0;



for( int i=0; i<count; i++ )
listModel.removeElementAt(0);

count=0; /////////////////////////////////////////////////////////

alist.clear();
addtoarray(s); if(!alist.isEmpty()){
for (Iterator iter = alist.iterator(); iter.hasNext();) {
fileopen("books.txt");


String sums = (String) ht.get((String)(iter.next()));

String bdetails="";

StringTokenizer st = new StringTokenizer(sums,"#");
while(st.hasMoreTokens())
{
bdetails=bdetails+st.nextToken()+" ";
}

listModel.addElement(bdetails);
count++;
}}
else{
listModel.addElement(alist);}
}

//aadd to arraay list

private void addtoarray(String s){
count=0;

try
{
DataInputStream readFile = new DataInputStream(new FileInputStream(s));
String rec = readFile.readLine();


while(rec != null)
{
StringTokenizer st = new StringTokenizer(rec,"#");
while(st.hasMoreTokens())
{

alist.add(st.nextToken());


rec = readFile.readLine(); //Random Access File
}
readFile.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}



}

//upto here arraylist




// trying libary Record

private JInternalFrame lrecord1(){
final JInternalFrame f1 = new JInternalFrame ("Test",false,true,false,true);
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
f1.setBounds( 0, 0, width-10, height - 80 );
f1.setTitle("All Fields Relating Books...");
//setDefaultCloseOperation(DISPOSE_ON_CLOSE );

//alist.clear();
//addtoarray("allbooks.txt");

final JTabbedPane fpane = new JTabbedPane(SwingConstants.TOP);

final JPanel pix1 = new JPanel();
final JPanel pix11 = new JPanel();
//final JPanel pix12 = new JPanel();

final JPanel pix2 = new JPanel();
final JPanel pix21 = new JPanel();
//final JPanel pix22 = new JPanel();
//final JLabel stb =new JLabel("The Student Holds This Books..");

final JPanel pix3 = new JPanel();
final JPanel pix4 = new JPanel();
getContentPane().setLayout(new SpringLayout());
fpane.addTab("Issue ",pix1);
fpane.addTab("Return ",pix2);
fpane.addTab("Add ",pix3);
fpane.addTab("Delete ",pix4);
fpane.setSelectedIndex(0);


final String[] labelStrings = {
"Name : ",
"Roll No : ","ISBN No :",
"DOI : ","DOR :"

};

final String[] labelStrings1 = {
"Name : ",
"ISBN NO : ",
"DOI : ","ADOR:","DOR :"

};

final String[] labelStrings2 = {
"Book Name : ",
"ISBN NO : ","Book Type :",
"Author : ","Edition :"

};


final String[] labelStrings3 = {

"ISBN NO : "


};


fpane.getModel().addChangeListener(
new ChangeListener() {
public void stateChanged(ChangeEvent e) {
SingleSelectionModel model = (SingleSelectionModel) e.getSource();
if(model.getSelectedIndex() == fpane.getTabCount()-1) {
tabIndex=4;
pix4.removeAll();

f1.setSize(300,150);

pix4.setLayout(new BoxLayout(pix4,BoxLayout.PAGE_AXIS));

pix4.add(createEntryFields(labelStrings3));
pix4.add(createButtons("Delete Book","Cancel"));


}

if(model.getSelectedIndex() == fpane.getTabCount()-2) {

pix3.removeAll();

f1.setSize(300,250);

tabIndex=3;
pix3.setLayout(new BoxLayout(pix3,BoxLayout.PAGE_AXIS));

pix3.add(createEntryFields(labelStrings2));
pix3.add(createButtons("Add","Cancel"));


}
if(model.getSelectedIndex() == fpane.getTabCount()-3) {

pix2.removeAll();
pix21.removeAll();
pix22.removeAll();

pix2.setLayout(new GridLayout());


pix22.setLayout(new GridBagLayout());
GridBagConstraints c1 = new GridBagConstraints();
c1.fill = GridBagConstraints.HORIZONTAL;
c1.weightx = 0.5;
c1.gridx = 0;
c1.gridy = 0;


pix22.add(stb,c1);

c1.ipady = 30; //make this component tall
c1.weightx = 0.0;
c1.gridwidth = 3;
c1.gridx = 0;
c1.gridy = 1;


stb.setVisible(false);
//listModel.clear();
//alist.clear();
pix22.add(drawlist(),c1);
pix22.setVisible(false);
listboxno=1;



pix21.setLayout(new BoxLayout(pix21,BoxLayout.PAGE_AXIS));
pix21.add(createcombo(1));
pix21.add(createEntryFields(labelStrings1));
pix21.add(createButtons("Return","Cancel"));

pix2.add(pix21);
pix2.add(pix22);
f1.pack();
}
if(model.getSelectedIndex() == fpane.getTabCount()-4) {

tabIndex=1;
pix1.removeAll();
pix11.removeAll();
pix12.removeAll();

pix1.setLayout(new GridLayout());

pix11.setLayout(new BoxLayout(pix11,BoxLayout.PAGE_AXIS));
pix11.add(createEntryFields(labelStrings));
pix11.add(createButtons("Issue","Cancel"));

pix12.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;

c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;


pix12.add(addradiobutton(),c);


c.ipady = 30; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;

//listModel.clear();
pix12.add(drawlist(),c);
changelist("allbooks.txt");
listboxno=0;

nameField[3].setText(idate);
nameField[3].setEditable(false);

returndate();

pix1.add(pix11);
pix1.add(pix12);
f1.pack();



}
}
}
);

pix1.removeAll();
pix11.removeAll();
pix12.removeAll();


pix1.setLayout(new GridLayout());

pix11.setLayout(new BoxLayout(pix11,BoxLayout.PAGE_AXIS));
pix11.add(createEntryFields(labelStrings));
pix11.add(createButtons("Issue","Cancel"));

pix12.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;

c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;


pix12.add(addradiobutton(),c);


c.ipady = 30; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;


//listModel.clear();
pix12.add(drawlist(),c);
changelist("allbooks.txt");
listboxno=0;
id= gcal.get(Calendar.DATE);
im=(int)gcal.get(Calendar.MONTH)+1;
iy=gcal.get(Calendar.YEAR);


idate=id+"/"+im+"/"+iy;

nameField[3].setText(idate);
nameField[3].setEditable(false);
returndate();

pix1.add(pix11);
pix1.add(pix12);
f1.pack();




f1.getContentPane().add(fpane);

JDesktopPane desktop = new JDesktopPane();
desktop.add(f1);

desk.add(f1);

f1.setVisible(true);
//f1.pack();
return f1;
}

//libary record upto here

//Issueing a book

private void Issue(){
String isbn1=nameField[2].getText();



fileopen("srecord.txt");

try{
number=nameField[1].getText();
String ert=(String)ht.get(number);
ht.clear();
insertroll(number,"irollno.txt");

if(ert.length()>1){

doi=nameField[3].getText();
dor=nameField[4].getText();

String tmp=isbn1+"#"+doi+"#"+dor;
ht1.clear();
fileopen1("issuedbook.txt");
ht1.put(number,tmp);
writetofile1("issuedbook.txt");
JOptionPane.showMessageDialog( hide,"One Book Issued");

}
}catch(NullPointerException e){
JOptionPane.showMessageDialog( hide,"Sorry u R Not A Student");}
}


protected void writetofile1(String s){
try{

File f=new File(s);
f.delete();
FileOutputStream fout = new FileOutputStream(s,true) ;
ht1.store(fout,"Prosun~s creation");

fout.close();
}catch(IOException e){}

}


//book issue upto here

//return a book

private void Returnbook(){
comparedate();

fileopen("issuedbook.txt");
deletearecord("issuedbook.txt",newSelection);
deletefromsuppotedfile("irollno.txt",newSelection);


JOptionPane.showMessageDialog( hide,"One Book Returned");
ht.clear();
}

//function for deleting a record from file

private void deletearecord(String fn,String key){

File f=new File(fn);
f.delete();
try{
FileOutputStream fout = new FileOutputStream(fn,true);
ht.remove(key);
ht.store(fout,"Prosun~s creation");
fout.close();
}catch(IOException e){}

ht.clear();

}

//function for deleting a record from file upto here

//book return upto here

//delete book from libary
private void Deletebook(){
String sd=nameField[0].getText();
fileopen("books.txt");
deletearecord("books.txt",sd);
deletefromsuppotedfile("cbooks.txt",sd);
deletefromsuppotedfile("javabooks.txt",sd);
deletefromsuppotedfile("vbbooks.txt",sd);
deletefromsuppotedfile("allbooks.txt",sd);

}

//delete book from libary upto here


/////////////////Delete Book no from Supporting files////////////////

private void deletefromsuppotedfile(String fn,String key){

try
{
DataInputStream readFile = new DataInputStream(new FileInputStream(fn));
String rec = readFile.readLine();
String rolllist=null;

DataOutputStream writeFile1 = new DataOutputStream(new FileOutputStream("temp1.txt",true));




while(rec != null)
{
StringTokenizer st = new StringTokenizer(rec,"#");
while(st.hasMoreTokens())
{

String n=st.nextToken();
rec = readFile.readLine();
if(!n.equals(key)){
//writimg to a tempfile
writeFile1.writeBytes(n+"#");
}

}
readFile.close();
writeFile1.close();
}


filecopy("temp1.txt",fn);



}
catch(Exception e)
{
e.printStackTrace();
}


}
////////////////////////upto here///////////////////////////////////////



private void comparedate(){
String sr=nameField[3].getText();
int rd,rm,ry;
StringTokenizer st = new StringTokenizer(sr,"/");
rd=Integer.parseInt(st.nextToken());
rm=Integer.parseInt(st.nextToken());
ry=Integer.parseInt(st.nextToken());
if(iy>ry){
JOptionPane.showMessageDialog( hide,"You Must Give Fine For Delay");
}else{
if(im>rm){
JOptionPane.showMessageDialog( hide,"You Must Give Fine For Delay");}
else{
if(id>rd)
JOptionPane.showMessageDialog( hide,"You Must Give Fine For Delay");}
}
}

private void returndate(){

im1=im;
iy1=iy;
id1=id+15;

if(im1==1||im1==3||im1==5||im1==7||im1==8||im1==10||im1==12){
if(id1>31){
im1=im1+1;
id1=id1-31;
if(im1>12){
im1=im1-12;
iy1=iy1+1;
}}}


if(im1==4||im1==6||im1==9||im1==11){
if(id1>30){
im1=im1+1;
id1=id1-30;
if(im1>12){
im1=im1-12;
iy1=iy1+1;}
}}


if(im1==2){
if(id1>28){
im1=im1+1;
id1=id1-28;
if(im1>12){
im1=im1-12;
iy1=iy1+1;
}}}

rdate = id1+"/"+im1+"/"+iy1;
nameField[4].setEditable(true);
nameField[4].setText(rdate);
nameField[4].setEditable(false);

}

private JPanel addradiobutton(){

JPanel pix12= new JPanel();
String allString= "All";
String javaString= "Java";
String cString= "C";
String vbString= "Vb";




allButton = (JRadioButton) pix12.add(new JRadioButton(allString));
allButton.setActionCommand(allString);
allButton.setSelected(true);

javaButton =(JRadioButton) pix12.add( new JRadioButton(javaString));

javaButton.setActionCommand(javaString);

cButton =(JRadioButton) pix12.add( new JRadioButton(cString));

cButton.setActionCommand(cString);

vbButton =(JRadioButton) pix12.add( new JRadioButton(vbString));

vbButton.setActionCommand(vbString);



//Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(allButton);
group.add(javaButton);
group.add(cButton);
group.add(vbButton);




//Register a listener for the radio buttons.
allButton.addActionListener(this);
javaButton.addActionListener(this);
cButton.addActionListener(this);
vbButton.addActionListener(this);



return pix12;


}

private JPanel drawlist() {




listModel.addElement("prosun");
count++;

//Create the list and put it in a scroll pane.

list = new JList(listModel);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSelectedIndex(0);
list.addListSelectionListener(this);
list.setVisibleRowCount(5);
JScrollPane listScrollPane = new JScrollPane(list);


String name = listModel.getElementAt(
list.getSelectedIndex()).toString();

//Create a panel that uses BoxLayout.
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new BoxLayout(buttonPane,
BoxLayout.LINE_AXIS));

buttonPane.add(Box.createHorizontalStrut(5));
//buttonPane.add(new JSeparator(SwingConstants.HORIZONTAL));
buttonPane.add(Box.createHorizontalStrut(5));



buttonPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

buttonPane.add(listScrollPane, BorderLayout.CENTER);




return buttonPane;
}





private void Addbook(){
fileopen("books.txt");
bname=nameField[0].getText();
isbn=nameField[1].getText();
booktype=nameField[2].getText();
author=nameField[3].getText();
edition=nameField[4].getText();

booksum=bname+"#"+author+"#"+edition;
ht.put(isbn,booksum);
writetofile("books.txt");


try{
DataOutputStream writeFile2 = new DataOutputStream(new FileOutputStream("allbooks.txt",true)); writeFile2.writeBytes(isbn+"#");
writeFile2.close();
}catch(IOException e){}




if(booktype.equals("c")){
try{
DataOutputStream writeFile1 = new DataOutputStream(new FileOutputStream("cbooks.txt",true)); writeFile1.writeBytes(isbn+"#");
writeFile1.close();
}catch(IOException e){}
}


if(booktype.equals("vb")){
try{
DataOutputStream writeFile1 = new DataOutputStream(new FileOutputStream("vbbooks.txt",true)); writeFile1.writeBytes(isbn+"#");
writeFile1.close();
}catch(IOException e){}
}

if(booktype.equals("java")){
try{
DataOutputStream writeFile1 = new DataOutputStream(new FileOutputStream("javabooks.txt",true)); writeFile1.writeBytes(isbn+"#");
writeFile1.close();
}catch(IOException e){}
}

}


private JInternalFrame newFile(){
final JInternalFrame f = new JInternalFrame ("Test",false,true,false,true);
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
f.setBounds( 0, 0, width-10, height - 80 );
f.setTitle("All Fields Regarding Students..");



final JTabbedPane fpane = new JTabbedPane(SwingConstants.TOP);


final JPanel pix1 = new JPanel();
final JPanel pix2 = new JPanel();
final JPanel pix3 = new JPanel();
final JPanel pix4 = new JPanel();


getContentPane().setLayout(new SpringLayout());

fpane.addTab("Add",pix1);
fpane.addTab("Update",pix2);
fpane.addTab("Search",pix3);
fpane.addTab("Delete",pix4);
fpane.setSelectedIndex(0);

final String[] labelStrings = {
"Name : ",
"Father Name: ","Roll No :",
"Address: ","Qualification :",
"E_Mail: ","Tele Ph :"
};

final String[] labelStrings1 = {
"Name : ",
"Father Name: ",
"Address: ","Qualification :",
"E_Mail: ","Tele Ph :"
};



fpane.getModel().addChangeListener(
new ChangeListener() {
public void stateChanged(ChangeEvent e) {
SingleSelectionModel model = (SingleSelectionModel) e.getSource();
if(model.getSelectedIndex() == fpane.getTabCount()-1) {
tabIndex=4;
wcoboxchange=0;
pix4.removeAll();
pix4.setLayout(new BoxLayout(pix4,BoxLayout.PAGE_AXIS));
pix4.add(createcombo(0));
pix4.add(createEntryFields(labelStrings1));
pix4.add(createButtons("Delete","Cancel"));


}

if(model.getSelectedIndex() == fpane.getTabCount()-2) {

pix3.removeAll();
wcoboxchange=0;
pix3.setLayout(new BoxLayout(pix3,BoxLayout.PAGE_AXIS));
pix3.add(createcombo(0));
pix3.add(createEntryFields(labelStrings1));
pix3.add(createButtons("Finished","Cancel"));
tabIndex=3;
}
if(model.getSelectedIndex() == fpane.getTabCount()-3) {
wcoboxchange=0;
pix2.removeAll();
pix2.setLayout(new BoxLayout(pix2,BoxLayout.PAGE_AXIS));
pix2.add(createcombo(0));
pix2.add(createEntryFields(labelStrings1));
pix2.add(createButtons("Update","Cancel"));
tabIndex=2;

}
if(model.getSelectedIndex() == fpane.getTabCount()-4) {
wcoboxchange=0;
tabIndex=1;
pix1.removeAll();
pix1.setLayout(new BoxLayout(pix1,BoxLayout.PAGE_AXIS));

pix1.add(createEntryFields(labelStrings));
pix1.add(createButtons("OK","Cancel"));


}
}
}
);



pix1.setLayout(new BoxLayout(pix1,BoxLayout.PAGE_AXIS));

pix1.add(createEntryFields(labelStrings));
pix1.add(createButtons("OK","Cancel"));



f.getContentPane().add(fpane);

JDesktopPane desktop = new JDesktopPane();
desktop.add(f);

desk.add(f);
f.setLocation(100,75);
f.setVisible(true);
return f;
}


//trying from downloded code


public JFormattedTextField getTextField(JSpinner spinner) {
JComponent editor = spinner.getEditor();
if (editor instanceof JSpinner.DefaultEditor) {
return ((JSpinner.DefaultEditor)editor).getTextField();
} else {
System.err.println("Unexpected editor type: "
+ spinner.getEditor().getClass()
+ " isn~t a descendant of DefaultEditor");
return null;
}
}

//downloded code ends heres

protected JComponent createEntryFields(String[] labelStrings) {
JPanel panel = new JPanel(new SpringLayout());

JLabel[] labels = new JLabel[labelStrings.length];
JComponent[] fields = new JComponent[labelStrings.length];
int fieldNum = 0;
nameField = new JTextField[labelStrings.length];



for(int p=0;p<labelStrings.length;p++){
nameField[p] = new JTextField(20);
nameField[p].setColumns(20);
fields[fieldNum++] = nameField[p];

nameField[p].setMaximumSize(nameField[p].getPreferredSize());}


//Associate label/field pairs, add everything,
//and lay it out.
for (int i = 0; i < labelStrings.length; i++) {
labels[i] = new JLabel(labelStrings[i],
JLabel.TRAILING);
labels[i].setLabelFor(fields[i]);
labels[i] .setMaximumSize(labels[i] .getPreferredSize());
panel.add(labels[i]);
panel.add(fields[i]);

//Add listeners to each field.
JTextField tf = null;
tf = (JTextField)fields[i];
tf.addActionListener(this);

//nameField1[i]=nameField[i];



}
makeCompactGrid(panel,
labelStrings.length, 2,
6, 6, //init x,y
7, 7);//xpad, ypad
return panel;
}


protected void fileopen(String s){
FileInputStream fin = null;
ht.clear();
try{
fin = new FileInputStream(s);
}catch(FileNotFoundException e){

//ignoring missing file
}

try {
if(fin != null){
ht.load(fin);
fin.close();
}
}catch(IOException e) {
System.out.println("Error reading file");
}
}

protected void fileopen1(String s){
FileInputStream fin = null;
try{
fin = new FileInputStream(s);
}catch(FileNotFoundException e){

//ignoring missing file
}

try {
if(fin != null){
ht1.load(fin);
fin.close();
}
}catch(IOException e) {
System.out.println("Error reading file");
}


}

protected void writetofile(String s){
try{

File f=new File(s);
f.delete();
FileOutputStream fout = new FileOutputStream(s,true) ;
ht.store(fout,"Prosun~s creation");

fout.close();
}catch(IOException e){}

}

private void save(){

fileopen("srecord.txt");


name=nameField[0].getText();

fname=nameField[1].getText();
number=nameField[2].getText();
add=nameField[3].getText();
qua=nameField[4].getText();
email=nameField[5].getText();
tele=nameField[6].getText();

insertroll(number,"roll.txt");

sums=name+"#"+fname+"#"+add+"#"+qua+"#"+email+"#"+tele;

ht.put(number,sums);



writetofile("srecord.txt");

}
//save upto here


private void Update(){

fileopen("srecord.txt");


name=nameField[0].getText();

fname=nameField[1].getText();
//number=nameField2[2].getText();
number=newSelection;
add=nameField[2].getText();
qua=nameField[3].getText();
email=nameField[4].getText();
tele=nameField[5].getText();

//insertroll(number);

sums=name+"#"+fname+"#"+add+"#"+qua+"#"+email+"#"+tele;

ht.put(number,sums);



writetofile("srecord.txt");

}

public void insertroll(String number,String fn)
{
try
{
DataOutputStream writeFile = new DataOutputStream(new FileOutputStream(fn,true));
String rec = number+"#";
writeFile.writeBytes(rec);


writeFile.close();
}
catch(Exception e)
{
//e.printStackTrace();
}
}




//trying to delete

private void Delete(){

fileopen("srecord.txt");



File f=new File("d:\prosun\SWING\srecord.txt");
f.delete();
try{
FileOutputStream fout = new FileOutputStream("srecord.txt",true);
ht.remove(newSelection);
ht.store(fout,"Prosun~s creation");
fout.close();
}catch(IOException e){}


deletefromsuppotedfile("roll.txt",newSelection);


}


//detete upto here

//file copy

private void filecopy(String a,String b){


int i;
FileInputStream fin;
FileOutputStream fout;


try{

try{
fin =new FileInputStream(a);
}catch(FileNotFoundException e){
System.out.println("FILE NOT FOUND");
return;}

try{
File f1=new File("d:\prosun\SWING\roll.txt");
f1.delete();

fout =new FileOutputStream(b);
}catch(FileNotFoundException e){
System.out.println("OUTPUT FILE NOT FOUND");
return;}
}

catch(ArrayIndexOutOfBoundsException e){
System.out.println("usage");
return;}

// read characters until EOF

try{

do{
i=fin.read();
if(i !=-1)
fout.write(i);
//System.out.print((char) i);
}
while(i!=-1);}
catch(IOException e){
System.out.println("file Error");
}
try{
fin.close();
fout.close();

File f2=new File("d:\prosun\SWING\temp1.txt");
f2.delete();
}catch(IOException e){}

}
//copy file upto here

protected JComponent createButtons(String s,String t) {
JPanel panel = new JPanel(new FlowLayout(FlowLayout.TRAILING));

JButton button = new JButton(s);
button.addActionListener(this);
button.setActionCommand(s);
panel.add(button);

button = new JButton(t);
button.addActionListener(this);
button.setActionCommand(t);
panel.add(button);

//Match the SpringLayout~s gap, subtracting 5 to make
//up for the default gap FlowLayout provides.
panel.setBorder(BorderFactory.createEmptyBorder(0, 0,
10, 10));
return panel;
}

/*//adding actionlistener to the buttons
public void actionPerformed(ActionEvent e) {
if ("clear".equals(e.getActionCommand())) {
System.out.exit(0);

}}*/
//upto here for buttons


protected JComponent createcombo(final int t) {
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEADING));
JLabel rol=new JLabel(" Roll No :");


JComboBox petList = new JComboBox();
DataInputStream readFile;
try
{
if(t==1){
readFile = new DataInputStream(new FileInputStream("irollno.txt"));}
else{
readFile = new DataInputStream(new FileInputStream("roll.txt"));}
String rec = readFile.readLine();


while(rec != null)
{
StringTokenizer st = new StringTokenizer(rec,"#");
while(st.hasMoreTokens())
{
//System.out.println(st.nextToken()+" ");
petList.addItem(st.nextToken());
rec = readFile.readLine(); //Random Access File
}
readFile.close();
}
}
catch(Exception e)
{
//e.printStackTrace();
}

//petList.setSelectedIndex(0);
petList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource() ;
newSelection = (String)cb.getSelectedItem();
if(t==1){
//count1=0;
reformat1(newSelection); }

if(t==0){
reformat(newSelection);
wcoboxchange=1;
}
}
});




panel.add("EAST",rol);
panel.add(petList);



//Match the SpringLayout~s gap, subtracting 5 to make
//up for the default gap FlowLayout provides.
panel.setBorder(BorderFactory.createEmptyBorder(0, 0,
1, 1));
return panel;
}




private void reformat1(String s){


stb.setVisible(true);
pix22.setVisible(true);
int n=0;



for( int i=0; i<count; i++ ) {
listModel.removeElementAt(0);
}
count=0;
fileopen("issuedbook.txt");
String sums1 = (String) ht.get(s);
String bdetails1="";
ht.clear();

fileopen("srecord.txt");
String sums2 = (String) ht.get(s);
ht.clear();
StringTokenizer st3 = new StringTokenizer(sums2,"#");
nameField[0].setText(st3.nextToken());

StringTokenizer st = new StringTokenizer(sums1,"#");
while(st.hasMoreTokens())
{
n++;
String em= st.nextToken();
//changing inside

if(n==1||n==4||n==7){

fileopen1("books.txt");
String rec1= (String) ht1.get(em);
//nameField[0].setText(em);

StringTokenizer st1 = new StringTokenizer(rec1,"#");
while(st1.hasMoreTokens()){
bdetails1=bdetails1+st1.nextToken()+" ";


}

listModel.addElement(bdetails1);
bdetails1="";
count++;

}else{
count1=count1;
st.nextToken();}
//changing innside upto here


}



}




//change

private void reformat(String s){
FileInputStream fin = null;

try{
fin = new FileInputStream("srecord.txt");
}catch(FileNotFoundException e){

//ignoring missing file
}

try {
if(fin != null){
ht.load(fin);
fin.close();
}
}catch(IOException e) {
System.out.println("Error reading file");
}

String sums = (String) ht.get(s);
int i=0;
StringTokenizer st = new StringTokenizer(sums,"#");
while(st.hasMoreTokens())
{
nameField[i].setText(st.nextToken());
i++;
}
//fin.close();
}

//change for compactgrid
public static void makeCompactGrid(Container parent,
int rows, int cols,
int initialX, int initialY,
int xPad, int yPad) {
SpringLayout layout;
try {
layout = (SpringLayout)parent.getLayout();
} catch (ClassCastException exc) {
System.err.println("The first argument to makeCompactGrid must use SpringLayout.");
return;
}

//Align all cells in each column and make them the same width.
Spring x = Spring.constant(initialX);
for (int c = 0; c < cols; c++) {
Spring width = Spring.constant(0);
for (int r = 0; r < rows; r++) {
width = Spring.max(width,
getConstraintsForCell(r, c, parent, cols).
getWidth());
}
for (int r = 0; r < rows; r++) {
SpringLayout.Constraints constraints =
getConstraintsForCell(r, c, parent, cols);
constraints.setX(x);
constraints.setWidth(width);
}
x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
}

//Align all cells in each row and make them the same height.
Spring y = Spring.constant(initialY);
for (int r = 0; r < rows; r++) {
Spring height = Spring.constant(0);
for (int c = 0; c < cols; c++) {
height = Spring.max(height,
getConstraintsForCell(r, c, parent, cols).
getHeight());
}
for (int c = 0; c < cols; c++) {
SpringLayout.Constraints constraints =
getConstraintsForCell(r, c, parent, cols);
constraints.setY(y);
constraints.setHeight(height);
}
y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
}

//Set the parent~s size.
SpringLayout.Constraints pCons = layout.getConstraints(parent);
pCons.setConstraint(SpringLayout.SOUTH, y);
pCons.setConstraint(SpringLayout.EAST, x);
}


//upto here compactgrid
//change cons
private static SpringLayout.Constraints getConstraintsForCell(
int row, int col,
Container parent,
int cols) {
SpringLayout layout = (SpringLayout) parent.getLayout();
Component c = parent.getComponent(row * cols + col);
return layout.getConstraints(c);
}

//traints




public static void main(String args[]){
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
SampleDesktop13 td = new SampleDesktop13("Digital Libary System");
td.setSize(width,height-30);
td.setVisible(true);
}
}
Share this article :
 
Copyright © 2011. B.Sc B.Tech MCA Ploytechnic Mini,Main Projects | Free Main Projcets Download | MCA |B.tech . All Rights Reserved
Company Info | Contact Us | Privacy policy | Term of use | Widget | Advertise with Us | Site map
Template Modify by Creating Website. Inpire by Darkmatter Rockettheme Proudly powered by Blogger