Sunteți pe pagina 1din 9

import import import import

java.sql.*; java.awt.*; java.awt.event.*; javax.swing.*;

public class frmBranch extends JInternalFrame implements ActionListener { //JPanel Variables JPanel jpnlMain = new JPanel(); //JScrollPane Variables public static JScrollPane BrchTblJSP = new JScrollPane(); //JTable Variables public static JTable JTBrchTbl; //Connection Variables Connection cnBranch; //Statement Variables public static Statement stmtBranch; //ResultSet Variables public static ResultSet rsBranch; //String Variables public static String sSQL; public static String Content[][]; //int Variables public static int rowNum = 0; public static int total = 0; //boolean Variables boolean goEOF; Dimension screen creenSize(); JFrame JFParentFrame; //JButton Variables JButton bttnAddNew 6x16/save_all.png")); JButton bttnEdit mages/i16x16/editcopy.png")); JButton bttnDelete mages/i16x16/delete.png")); JButton bttnSearch mages/i16x16/search.png")); JButton bttnPreview mages/i16x16/preview.png")); JButton bttnPrint mages/i16x16/prints.png")); JButton bttnRefresh mages/i16x16/reload.png")); JButton bttnExit mages/i16x16/exit.png")); = new JButton("Add", new ImageIcon("images/i1 = Toolkit.getDefaultToolkit().getS

= new JButton("Modify", new ImageIcon("i = new JButton("Delete", new ImageIcon("i = new JButton("Search", new ImageIcon("i = new JButton("Preview",new ImageIcon("i = new JButton("Print", new ImageIcon("i = new JButton("Reload", new ImageIcon("i = new JButton("Exit", new ImageIcon("i

//JLabel Varibales JLabel lblHeader = new JLabel(new ImageIcon("images/iCust om/iTools/Banner-Header.png")); JLabel lblIcon = new JLabel(new ImageIcon("images/i32x3 2/branch.png")); JLabel lblCaption = new JLabel("NOTE: This form contains a ll information about the Branch Records.");

//ImageIcon Variables ImageIcon icon1 g"); ImageIcon icon2 //Class Variables clsSettings settings clsSQL sqls //JMenubar Variables JMenuBar menubar //JMenu Variables JPopupMenu mnuTools //JMenuItem Variables JMenuItem itmAddNew JMenuItem itmModify JMenuItem itmDelete JMenuItem itmSearch JMenuItem itmPreview JMenuItem itmPrint JMenuItem itmRefresh JMenuItem itmExit JMenuItem itmCustBal tion {

= new ImageIcon("images/i16x16/branch.pn = new ImageIcon("images/i16x16/normal.png"); = new clsSettings(); = new clsSQL(); = new JMenuBar(); = new JPopupMenu(); = new JMenuItem(); = new JMenuItem(); JMenuItem(); JMenuItem(); JMenuItem(); = new JMenuItem(); JMenuItem(); = new JMenuItem(); JMenuItem();

= new = new = new = new = new

public frmBranch(Connection conn, JFrame getParentFrame) throws SQLExcep super("Branch Records",false,true,false,true); jpnlMain.setBackground(Color.white); jpnlMain.setLayout(null); JFParentFrame = getParentFrame; cnBranch = conn; stmtBranch = cnBranch.createStatement(ResultSet.TYPE_SCROLL_INSE NSITIVE,ResultSet.CONCUR_READ_ONLY); sSQL = "SELECT * FROM qryBranch ORDER BY BranchID ASC"; //Set the Settings JTBrchTbl = CreateTable(); BrchTblJSP.getViewport().add(JTBrchTbl); BrchTblJSP.setBounds(5,55,780,470); jpnlMain.add(BrchTblJSP); //Set the Settings of JButton settings.setJButton(bttnAddNew,5,535,95,25,"add","New"); bttnAddNew.setMnemonic(KeyEvent.VK_A); bttnAddNew.addActionListener(JBActionListener); settings.setJButton(bttnEdit,102,535,95,25,"modify","Modify"); bttnEdit.setMnemonic(KeyEvent.VK_M); bttnEdit.addActionListener(JBActionListener); settings.setJButton(bttnDelete,198,535,100,25,"delete","Delete") ; bttnDelete.setMnemonic(KeyEvent.VK_D); bttnDelete.addActionListener(JBActionListener); settings.setJButton(bttnSearch,300,535,99,25,"search","Search");

bttnSearch.setMnemonic(KeyEvent.VK_S); bttnSearch.addActionListener(JBActionListener); settings.setJButton(bttnPreview,400,535,99,25,"preview","Preview "); bttnPreview.setMnemonic(KeyEvent.VK_P); bttnPreview.addActionListener(JBActionListener); settings.setJButton(bttnPrint,500,535,95,25,"print","Print"); bttnPrint.setMnemonic(KeyEvent.VK_I); bttnPrint.addActionListener(JBActionListener); settings.setJButton(bttnRefresh,595,535,99,25,"Reload","Reload") ; bttnRefresh.setMnemonic(KeyEvent.VK_R); bttnRefresh.addActionListener(JBActionListener); settings.setJButton(bttnExit,695,535,90,25,"exit","Exit"); bttnExit.setMnemonic(KeyEvent.VK_E); bttnExit.addActionListener(JBActionListener); //Set the Settings of JLable settings.setJLabel(lblHeader,0,0,800,40); settings.setJLabel(lblIcon,5,2,50,40); settings.setJLabel(lblCaption,60,2,500,40); lblCaption.setFont(new Font("Dialog", Font.BOLD, 12)); lblCaption.setForeground(new Color(255,255,255)); //Add JLabel to JPanel jpnlMain.add(lblCaption); jpnlMain.add(lblIcon); jpnlMain.add(lblHeader); //Add JButton to JPanel jpnlMain.add(bttnAddNew); jpnlMain.add(bttnEdit); jpnlMain.add(bttnDelete); jpnlMain.add(bttnSearch); jpnlMain.add(bttnPreview); jpnlMain.add(bttnPrint); jpnlMain.add(bttnRefresh); jpnlMain.add(bttnExit); getContentPane().setLayout(new BorderLayout(0,0)); getContentPane().add(BorderLayout.CENTER, jpnlMain); setJMenuBar(CreateJMenuBar()); setFrameIcon(new ImageIcon("images/i16x16/branch.png")); setSize(800,600); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); setLocation((screen.width - 800)/2,((screen.height-600)/2)-60); //Add listener to components that can bring up popup menus. MouseListener popupListener = new PopupListener(); JTBrchTbl.addMouseListener(popupListener); menubar.addMouseListener(popupListener); } class PopupListener extends MouseAdapter {

public void mousePressed(MouseEvent e) {maybeShowPopup(e);} public void mouseReleased(MouseEvent e) {maybeShowPopup(e);} private void maybeShowPopup(MouseEvent e) { if (e.isPopupTrigger()) {mnuTools.show(e.getComponent(), e.getX(), e.getY()); } } } protected JMenuBar CreateJMenuBar() //Create a Menubar { //BEGIN CREATING MENUBAR *********************************************** *********************************************** //START SUBMENU ************************************************ **************************************************** //Menu Tools itmAddNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.ALT_MASK)); itmModify.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, ActionEvent.ALT_MASK)); itmDelete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.ALT_MASK)); itmSearch.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.ALT_MASK)); itmPreview.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P ,ActionEvent.ALT_MASK)); itmPrint.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I,A ctionEvent.ALT_MASK)); itmRefresh.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R ,ActionEvent.ALT_MASK)); itmExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,Ac tionEvent.ALT_MASK)); itmAddNew.addActionListener(this); itmModify.addActionListener(this); itmDelete.addActionListener(this); itmSearch.addActionListener(this); itmPreview.addActionListener(this); itmPrint.addActionListener(this); itmRefresh.addActionListener(this); itmExit.addActionListener(this); mnuTools.add(settings.setJMenuItem(itmAddNew,"Add New","images /i16x16/save_all.png")); mnuTools.addSeparator(); mnuTools.add(settings.setJMenuItem(itmModify,"Modify","images/ i16x16/editcopy.png")); mnuTools.addSeparator(); mnuTools.add(settings.setJMenuItem(itmDelete,"Delete","images/ i16x16/delete.png")); mnuTools.addSeparator(); mnuTools.add(settings.setJMenuItem(itmSearch,"Search","images/

i16x16/search.png")); mnuTools.addSeparator(); mnuTools.add(settings.setJMenuItem(itmPreview,"Preview","image s/i16x16/preview.png")); mnuTools.addSeparator(); mnuTools.add(settings.setJMenuItem(itmPrint,"Print","images/i1 6x16/prints.png")); mnuTools.addSeparator(); mnuTools.add(settings.setJMenuItem(itmRefresh,"Reload","images /i16x16/Reload.png")); mnuTools.addSeparator(); mnuTools.add(settings.setJMenuItem(itmExit,"Exit","images/i16x 16/exit.png")); //END SUBMENU ******************************************************** *********************************************** menubar.setBackground(new Color(255,255,255));//Set the Background of JMenuBar. return menubar; //END CREATING MENUBAR ************************************************* *********************************************** } public static JTable CreateTable() { String ColumnHeaderName[] = {"Branch ID","Branch Name","Incharge Person","City","Contact No" }; try{ rsBranch = stmtBranch.executeQuery(sSQL); total = 0; //Move to the last record rsBranch.afterLast(); //Get the current record position if(rsBranch.previous())total = rsBranch.getRow(); //Move back to the first record; rsBranch.beforeFirst(); if(total != 0) { Content = new String[total][8]; while(rsBranch.next()) { Content[rowNum][0] = "" + rsBranch.getSt ring("BranchID"); Content[rowNum][1] = "" + rsBranch.getSt ring("BranchName"); Content[rowNum][2] = "" + rsBranch.getSt ring("InchargePerson"); Content[rowNum][3] = "" + rsBranch.getSt ring("City"); Content[rowNum][4] = "" + rsBranch.getSt ring("ContactNo"); rowNum++; } } else { Content = new String[0][6];

Content[0][0] Content[0][1] Content[0][2] Content[0][3] Content[0][4]

= = = = =

" " " " "

"; "; "; "; ";

} } catch(Exception eE){} JTable NewTable = new JTable (Content,ColumnHeaderName) { public boolean isCellEditable (int iRows, int iCols) {return false;} }; NewTable.setPreferredScrollableViewportSize(new Dimension(780, 4 70)); NewTable.setBackground(Color.white); //Start resize the table column NewTable.getColumnModel().getColumn(0).setMinWidth(0); NewTable.getColumnModel().getColumn(0).setPreferredWidth(100); NewTable.getColumnModel().getColumn(1).setPreferredWidth(200); NewTable.getColumnModel().getColumn(2).setPreferredWidth(100); NewTable.getColumnModel().getColumn(3).setPreferredWidth(100); //End resize the table column //Disposed variables ColumnHeaderName=null; Content=null; rowNum = 0; return NewTable; } public static void reloadRecord(String srcSQL) { sSQL = srcSQL; BrchTblJSP.getViewport().remove(JTBrchTbl); JTBrchTbl=CreateTable(); BrchTblJSP.getViewport().add(JTBrchTbl); } public void FormAddNew() { JDialog JDAdd = new frmBranch_Modify(true,JFParentFrame,cnBranch ,""); JDAdd.show(); }//Load Form AddNew BRANCH public void FormModify() { if(total != 0) { try { if(JTBrchTbl.getValueAt(JTBrchTbl.getSelectedRow (),JTBrchTbl.getSelectedColumn()) != null) { JDialog JDModify = new frmBranch_Modify( false,JFParentFrame,cnBranch,"SELECT * FROM tblBranch WHERE BranchID ='" + JTBrc hTbl.getValueAt(JTBrchTbl.getSelectedRow(),0) + "'");

JDModify.show(); } } catch(Exception sqlE) { if(sqlE.getMessage() != null){System.out.println (sqlE.getMessage());} else { JOptionPane.showMessageDialog(null,"Plea se select a record in the list to modify.","No Record Selected",JOptionPane.INFO RMATION_MESSAGE); } } } }//Load Form Modify BRANCH public void FormSearch() { JDialog JDSearch = new frmSearch(JFParentFrame, "Branch"); JDSearch.show(); }//Load Form Search BRANCH public void FormDelete() { if(total != 0) { try { if(JTBrchTbl.getValueAt(JTBrchTbl.getSelectedRow (),JTBrchTbl.getSelectedColumn()) != null) { String ObjButtons[] = {"Yes","No"}; int PromptResult = JOptionPane.showOptio nDialog(null,"Are you sure you want to removed " + JTBrchTbl.getValueAt(JTBrchTb l.getSelectedRow(),1) + " in the record?","Delete Record",JOptionPane.DEFAULT_OP TION,JOptionPane.ERROR_MESSAGE,null,ObjButtons,ObjButtons[1]); if(PromptResult==0) { sqls.recREMOVE(true, stmtBranch, "tblBranch", "BranchID", JTBrchTbl,0); reloadRecord(sSQL); JOptionPane.showMessageDialog(nu ll,"Record has been successfully removed.","Comfirm Delete",JOptionPane.INFORMAT ION_MESSAGE); } } } catch(Exception sqlE) { if(sqlE.getMessage()!=null) { JOptionPane.showMessageDialog(null,"You cannot delete this BRANCH because it is being used by another user.\nIn order to delete this BRANCH, delete its data from another table.","Comfirm Delete",JOpti onPane.WARNING_MESSAGE); } else { JOptionPane.showMessageDialog(null,"Plea se select a record in the list to deleted.","No Record Selected",JOptionPane.INF

ORMATION_MESSAGE); } } } }//Load Form Delete BRANCH public void FormPreview() { if(total != 0) { try { if(JTBrchTbl.getValueAt(JTBrchTbl.getSelectedRow (),JTBrchTbl.getSelectedColumn()) != null) { JDialog JDView = new frmBranch_view(JFPa rentFrame,cnBranch,"SELECT * FROM tblBranch WHERE BranchID ='" + JTBrchTbl.getVa lueAt(JTBrchTbl.getSelectedRow(),0) + "'"); JDView.show(); } } catch(Exception sqlE) { if(sqlE.getMessage() != null){System.out.println (sqlE.getMessage());} else { JOptionPane.showMessageDialog(null,"Plea se select a record in the list to Preview the information.","No Record Selected" ,JOptionPane.INFORMATION_MESSAGE); } } } }//Load Form Preview BRANCH public void FormReload() { reloadRecord("SELECT * FROM qryBranch ORDER BY BranchID ASC"); }//Load Form Reload BRANCH Records public void FormCancel() { setVisible(false); dispose(); }//Unload Form Branch Records ActionListener JBActionListener = new ActionListener() { public void actionPerformed(ActionEvent e) { String srcObj = e.getActionCommand(); if(srcObj == "add") New();} else if(srcObj == rmModify();} else else else else else } }; if(srcObj if(srcObj if(srcObj if(srcObj if(srcObj == == == == == "search") "delete") "preview") "Reload") "exit") {FormSearch();} {FormDelete();} {FormPreview();} {FormReload();} {FormCancel();} "modify" || srcObj == "edit"){Fo

{FormAdd

public void menubar_actionPerformed(ActionEvent event, int intWhich) { switch(intWhich) { case 0: FormAddNew(); break; case 1: FormModify(); break; case 2: FormSearch(); break; case 3: FormDelete(); break; case 4: FormPreview(); break; case 5: FormPreview(); break; case 6: FormReload(); break; case 7: FormCancel(); } } public void actionPerformed(ActionEvent event) { Object object = event.getSource(); if(object == itmAddNew){menubar_actionPerformed(ev ent,0);} else if(object == itmModify){menubar_actionPerformed(event,1);} else if(object == itmSearch){menubar_actionPerformed(event,2);} else if(object == itmDelete){menubar_actionPerformed(event,3);} else if(object == itmPreview){menubar_actionPerformed(event,4); } else if(object == itmPrint){menubar_actionPerformed(event,5);} else if(object == itmRefresh){menubar_actionPerformed(event,6); } else if(object == itmExit){menubar_actionPerformed(event,7);} } }

S-ar putea să vă placă și