ලේසියෙන්ම හොයාගන්න මෙතනින්

NetBeans වලින් Java Application එකක් සඳහා Main Window එක හදමු

මේ තියෙන්නේ මගේ Transport Officer සඳහා Design කල Window එකයි. සහ එහි codings ...





public class Home_TO extends javax.swing.JFrame {
    String name;
    public Home_TO() {
        initComponents();
        this.setLocationRelativeTo(null);
        showAllAvailable();
        showVehicles();
        showTravelGuides();
        showTransportOfficers();
    }

    public Home_TO(String fullName) {
        initComponents();
        this.setLocationRelativeTo(null);
        labelWelcome.setText(labelWelcome.getText()+fullName);
        showAllAvailable();
        showVehicles();
        showTravelGuides();
        showTransportOfficers();
        name=fullName;
    }
   
    final void showAllAvailable(){
        try{
            ResultSet result = DB_Access.getData("select vehicle, travel_guide, from_date, to_date from Reserve");
            DefaultTableModel defaultTableModel = (DefaultTableModel) tableRequests.getModel();
            int row = defaultTableModel.getRowCount();
            for(int i=0; i<row; i++){
                defaultTableModel.removeRow(0);
            }
            while(result.next()){
                Object object[]={result.getString(1),result.getString(2),result.getString(3),result.getString(4)};
                defaultTableModel.addRow(object);
            }
        }catch (Exception e){
            JOptionPane.showMessageDialog(this,"Error");
        }
    }
   
    final void showVehicles(){
        try{
            ResultSet result = DB_Access.getData("select * from vehicle");
            DefaultTableModel defaultTableModel = (DefaultTableModel) tableVehicles.getModel();
            int row = defaultTableModel.getRowCount();
            for(int i=0; i<row; i++){
                defaultTableModel.removeRow(0);
            }
            while(result.next()){
                Object object[]={result.getString(1),result.getString(2),result.getString(3),result.getString(4)};
                defaultTableModel.addRow(object);
            }
        }catch (Exception e){
            JOptionPane.showMessageDialog(this,"Error");
        }
    }
   
    final void showTravelGuides(){
        try{
            ResultSet result = DB_Access.getData("select * from TravelGuide");
            DefaultTableModel defaultTableModel = (DefaultTableModel) tableTravelGuides.getModel();
            int row = defaultTableModel.getRowCount();
            for(int i=0; i<row; i++){
                defaultTableModel.removeRow(0);
            }
            while(result.next()){
                Object object[]={result.getString(1),result.getString(3),result.getString(4),result.getString(5)};
                defaultTableModel.addRow(object);
            }
        }catch (Exception e){
            JOptionPane.showMessageDialog(this,"Error");
        }
    }
   
    final void showTransportOfficers(){
        try{
            ResultSet result = DB_Access.getData("select * from TransportOfficer");
            DefaultTableModel defaultTableModel = (DefaultTableModel) tableTransportOfficers.getModel();
            int row = defaultTableModel.getRowCount();
            for(int i=0; i<row; i++){
                defaultTableModel.removeRow(0);
            }
            while(result.next()){
                Object object[]={result.getString(1),result.getString(3),result.getString(4),result.getString(5)};
                defaultTableModel.addRow(object);
            }
        }catch (Exception e){
            JOptionPane.showMessageDialog(this,"Error");
        }
    }
   
    boolean isNumber(String check){
        char [] x = check.toCharArray();
        char y;
        for(int i=0;i<x.length;i++){
            y = x[i];
            if(!Character.isDigit(y)){
                return false;
            }
        }
        return true;
    }
   
    void updateVehicle(){
        String vehicle = (String) tableVehicles.getValueAt(tableVehicles.getSelectedRow(),0);
        String driver = (String) tableVehicles.getValueAt(tableVehicles.getSelectedRow(),1);
        String noOfSeats = (String) tableVehicles.getValueAt(tableVehicles.getSelectedRow(),3);
        if(isNumber(noOfSeats)){
            try{
                DB_Access.setData("update vehicle set driver='"+driver+"', no_of_seats='"+noOfSeats+"' where reg_no='"+vehicle+"'");
                JOptionPane.showMessageDialog(this,"Update Vehicle Succeeded","Update Vehilce",0);
            }catch(Exception e){
                JOptionPane.showMessageDialog(this,"Invalid Data Input. Check and Try Again","Error on Updating Vehicle",1);
            }
        }else{
            JOptionPane.showMessageDialog(this, "Invalid Data Input. Numeric Value Required","Invalid Data",0);
        }
    }
   
    void updateTransportOfficer(){
        String username = (String) tableTransportOfficers.getValueAt(tableTransportOfficers.getSelectedRow(),0);
        String salary = (String) tableTransportOfficers.getValueAt(tableTransportOfficers.getSelectedRow(),2);
        if(isNumber(salary)){
            try{
                DB_Access.setData("update TransportOfficer set salary='"+salary+"' where username='"+username+"'");
                JOptionPane.showMessageDialog(this,"Update Transport Officer Succeeded","Update Transport Officer",1);
            }catch(Exception e){
                JOptionPane.showMessageDialog(this,"Invalid Data Input. Check and Try Again","Error on Updating Transport Officer",0);
            }
        }else{
            JOptionPane.showMessageDialog(this, "Invalid Data Input. Numeric Value Required","Invalid Data",0);
        }
       
    }
   
    void updateTravelGuide(){
        String username = (String) tableTravelGuides.getValueAt(tableTravelGuides.getSelectedRow(),0);
        String address = (String) tableTravelGuides.getValueAt(tableTravelGuides.getSelectedRow(),2);
        String contract_end_date = (String) tableTravelGuides.getValueAt(tableTravelGuides.getSelectedRow(),3);
        try{
            DB_Access.setData("update TravelGuide set address='"+address+"', contract_end_date='"+contract_end_date+"' where username='"+username+"'");
            JOptionPane.showMessageDialog(this,"Update Travel Guide Succeeded","Update Travel Guide",1);
        }catch(Exception e){
            JOptionPane.showMessageDialog(this,"Invalid Data Input. Check and Try Again","Error on Updating Travel Guide",0);
        }
    }

private void buttonApproveActionPerformed(java.awt.event.ActionEvent evt) {                                             
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String today = dateFormat.format(date);
        String vehicle = (String) tableRequests.getValueAt(tableRequests.getSelectedRow(),0);
        String travel_guide = (String) tableRequests.getValueAt(tableRequests.getSelectedRow(),1);
        String from_date = (String) tableRequests.getValueAt(tableRequests.getSelectedRow(),2);
        String to_date = (String) tableRequests.getValueAt(tableRequests.getSelectedRow(),3);
        try{
            DB_Access.setData("update Reserve set approved_officer='"+name+"', approved_date_time='"+today+"' where vehicle='"+vehicle+"' and travel_guide='"+travel_guide+"' and from_date='"+from_date+"' and to_date='"+to_date+"'" );
            JOptionPane.showMessageDialog(this,"Approve Reservation Details Succeeded","Reservation Update",1);
        }catch(Exception e){
            JOptionPane.showMessageDialog(this,"Invalid Data Input. Check and Try Again","Error on Approving",0);
        }
   
    }                                            

    private void buttonRejectActionPerformed(java.awt.event.ActionEvent evt) {                                            
        String vehicle = (String) tableRequests.getValueAt(tableRequests.getSelectedRow(),0);
        String travel_guide = (String) tableRequests.getValueAt(tableRequests.getSelectedRow(),1);
        String from_date = (String) tableRequests.getValueAt(tableRequests.getSelectedRow(),2);
        String to_date = (String) tableRequests.getValueAt(tableRequests.getSelectedRow(),3);
        try{
            DB_Access.setData("update Reserve set approved_officer='"+"rejected"+"' where vehicle='"+vehicle+"' and travel_guide='"+travel_guide+"' and from_date='"+from_date+"' and to_date='"+to_date+"'" );
            JOptionPane.showMessageDialog(this,"Reject Reservation Succeeded","Reservation Update",1);
        }catch(Exception e){
            JOptionPane.showMessageDialog(this,"Invalid Data Input. Check and Try Again","Error on Rejecting",0);
        }
    }                                           

    private void tabStateChanged(javax.swing.event.ChangeEvent evt) {                                
        showTransportOfficers();
        showVehicles();
        showTravelGuides();
    }                               

    private void buttonInsertActionPerformed(java.awt.event.ActionEvent evt) {                                            
         Vehicle vehicle = new Vehicle(name);
        vehicle.setVisible(true);
    }                                           

    private void buttonGuideActionPerformed(java.awt.event.ActionEvent evt) {                                           
        Travel_Guide tg = new Travel_Guide(name);
        tg.setVisible(true);
    }                                          

    private void buttonOfficerActionPerformed(java.awt.event.ActionEvent evt) {                                             
        Transport_Officers to = new Transport_Officers(name);
        to.setVisible(true);
    }                                            

    private void buttonUpdateActionPerformed(java.awt.event.ActionEvent evt) {                                            
        int selectedTab = tab.getSelectedIndex();
        if(selectedTab==0){
            updateVehicle();
        }else if(selectedTab==1){
            updateTransportOfficer();
        }else if(selectedTab==2){
            updateTravelGuide();
        }else{
            JOptionPane.showMessageDialog(this, "Please Select a data row");
        }
    }                                           

    private void buttonDeleteActionPerformed(java.awt.event.ActionEvent evt) {                                            
        int selectedTab = tab.getSelectedIndex();
        String selected = null;
        if(selectedTab==0){
            try{
                selected = (String) tableVehicles.getValueAt(tableVehicles.getSelectedRow(),0);
                DB_Access.setData("delete from vehicle where reg_no='"+selected+"'" );
                JOptionPane.showMessageDialog(this, "Successfully Deleted");
                showVehicles();
            }catch (Exception e){
                JOptionPane.showMessageDialog(this, "Delete Failed");
            }
        }else if(selectedTab==1){
            try{
                selected = (String) tableTransportOfficers.getValueAt(tableTransportOfficers.getSelectedRow(),0);
                DB_Access.setData("delete from TransportOfficer where username='"+selected+"'" );
                JOptionPane.showMessageDialog(this, "Successfully Deleted");
                showTransportOfficers();
            }catch (Exception e){
                JOptionPane.showMessageDialog(this, "Delete Failed");
            }
        }else if(selectedTab==2){
            try{
                selected = (String) tableTravelGuides.getValueAt(tableTravelGuides.getSelectedRow(),0);
                DB_Access.setData("delete from TravelGuide where username='"+selected+"'" );
                JOptionPane.showMessageDialog(this, "Successfully Deleted");
                showTravelGuides();
            }catch (Exception e){
                JOptionPane.showMessageDialog(this, "Delete Failed");
            }
        }else{
            JOptionPane.showMessageDialog(this, "Please Select a data row");
        }
    }                                           

    private void tableVehiclesFocusGained(java.awt.event.FocusEvent evt) {                                         
        buttonUpdate.setEnabled(true);
        buttonDelete.setEnabled(true);
    }                                        

    private void tableTransportOfficersFocusGained(java.awt.event.FocusEvent evt) {                                                  
       buttonUpdate.setEnabled(true);
        buttonDelete.setEnabled(true);
    }                                                 

    private void tableTravelGuidesFocusGained(java.awt.event.FocusEvent evt) {                                             
        buttonUpdate.setEnabled(true);
        buttonDelete.setEnabled(true);
    }                                            

    private void buttonGenerateActionPerformed(java.awt.event.ActionEvent evt) {                                              
        Generate_Report generate = new Generate_Report(name);
        generate.setVisible(true);
       
    }                                             

    private void tabMouseClicked(java.awt.event.MouseEvent evt) {                                
        showVehicles();
        showTransportOfficers();
        showTravelGuides();
    }