This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2017/11/15

OK JButton to work

divinity divinity

2017/11/15

#
hi danpost is there anyway possible that there are any source for an ok jbutton to work. have been trying to get a OK Jbutton to work but everything ah tried is not working.
danpost danpost

2017/11/15

#
The basic procedure I would be inclined to do is to first create and set up the button:
JButton button = new JButton("Okay");
button.setActionCommand("okay");
button.addActionListener(this);
Then add it to a JPanel that is in turn added to the JFrame. For its actions I would then add the 'actionPerformed' method:
public void actionPerformed(ActionEvent e)
{
    if ("okay".equals(e.getActionCommand()))
    {
        // perform button action here
    }
}
divinity divinity

2017/11/16

#
hi danpost with the electronic role am created I have 6 interfaces that need to be link. the first one is the login, second: the student homepage, third is the lecturer home page,, new student page, s search page and an attendance page, how do i link those page. the link should be first, you login, from there u go either to the student homepage or the lecture homepage, depending on who is the person login. and then it should go to the search page (to search for a student) and then to the new std page and attendance to make the attendance. how is that supposed to be done
danpost danpost

2017/11/16

#
I presume they would all be put in a single package. The login one would have your 'main' method and everything else would be linked by way of code (through buttons, conditions, etc). You must understand, that all my java experience is within greenfoot and all I have to go on as far as my responses are concerned is gut intuition. I have done some swing things; however, nothing close to what you are doing.
divinity divinity

2017/11/24

#
hi danpost i am now doing the db part of the electronic role and I am getting this runtime error. have created 2 classes which is called the dbcon and the other is called model. can u tell me what is causing the error here is the codes for each class the model class is an inheritance of the dbcon
package ElectronicRole;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
/**
 *
 * @author luanataylor
 */
public class Model extends DBCon {
    
    ResultSet rs;
    
    
    public boolean isLoginValid(HashMap <String, String> login) {
        
        boolean valid = false;
         String querystr, table;
         
         //check for the usertype if it is equal to student when check for the login
        if( login.get("usertype").equals("STUDENT")){
           
            table = "students";
        }
        else{
            table  =  "lecturers";// it is not the students, it will be the lecturer
        }
        querystr = String.format("SELECT COUNT (STUDENTID) AS count FROM %s WHERE StudentID=\"%s\" AND Password=\"%s\"", table, login.get("userId"),login.get("password"));
        System.out.println(querystr);
        try {
         
             this.rs =  this.executeQuery(querystr);
             rs.next();
             if(rs.getInt("count") > 0){
             valid = true;
        }
        }
             catch(SQLException e){
                     
      }
        return valid;
  }
             
  }
    

here is the dbcon 

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ElectronicRole;

import java.sql.*;
/**
 *
 * @author luanataylor
 */
public class DBCon {
    
    public Connection con;
    public Statement stmt;
    
    public DBCon(){
        
        try{
            
            //connecting to the db
            Class.forName("com.mysql.jdbc.Driver");
            this.con = DriverManager.getConnection("dbc.sql://localhost:3306/electronic role", "dbuser", "dbpass");
            this.stmt = con.createStatement();
            
        }
        catch(Exception e){
            
            System.out.println(e);
        }
    }
        public ResultSet executeQuery(String querystr) throws SQLException{
            
            ResultSet rs = this.stmt.executeQuery(querystr);
            
            return rs;
        }
            
    
    
}
    

    

and there is the login 


package ElectronicRole;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;


public class Login extends javax.swing.JFrame implements ActionListener {
     
    private boolean isLoginValid;
            
    public Login() {
       
        initComponents();
        this.isLoginValid = false;
         
     
    }
    public String getUserType(){
        
        //this codes here is to check to see if it is the lecturer or the student
        
        int i = jBoxselect.getSelectedIndex();
        String UserType ="";
        if(i == 0){
            
            UserType = "STUDENT";
        }
        else
            if(i == 1){
               
                UserType =  "LECTURER";
            }
          return UserType;
    }
    public boolean validateLogin(){
      //this checking to see if the userid, password and usertype is valid or not
        
        Model model = new Model();
        HashMap <String, String > login = new HashMap<String, String>();   // values are pass to the ash map
        login.put("userid",txtuserid.getText());
        login.put("password", pass.getText());
        login.put("usertype", this.getUserType());
        return  model.isLoginValid(login);
        
    }
    // check to see if the login is valid or not
    public boolean isLoginValid(){
        return this.isLoginValid;
    }
    
}
here is the appmain
where the program will run from
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ElectronicRole;

/**
 *
 * @author luanataylor
 */
public class AppMain {
    
    
      public static void main(String args[]) {
        
        
            }
       
   
        //</editor-fold>
             Login logscrn = new Login();
             logscrn.setVisible(true);
             
            while(!logscrn.isLoginValid()){
                
                //do nothing
            }
            if(logscrn.getUserType().equals("STUDENT")){
                StudentHomePage shp = new StudentHomePage();
                shp.setVisible(true);
            }
            else if( logscrn.getUserType().equals("LECTURER")){
             LecturerHomePage lhp = new LecturerHomePage();
             lhp.setVisible(true);
      }
            logscrn.setVisible(false);            
            
   }
     
}

divinity divinity

2017/11/24

#
ah keep getting a null values for the student id
divinity divinity

2017/11/24

#
this is the error am getting
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
SELECT COUNT (STUDENTID) AS count FROM students WHERE StudentID="null" AND Password="lt748"
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at ElectronicRole.DBCon.executeQuery(DBCon.java:35)
	at ElectronicRole.Model.isLoginValid(Model.java:37)
	at ElectronicRole.Login.validateLogin(Login.java:52)
	at ElectronicRole.Login.btnOKActionPerformed(Login.java:197)
	at ElectronicRole.Login.access$100(Login.java:16)
	at ElectronicRole.Login$2.actionPerformed(Login.java:110)
Super_Hippo Super_Hippo

2017/11/24

#
What I am wondering right now is... why is your main method empty and the rest is somehow outside of methods?
danpost danpost

2017/11/24

#
What class is line 71 supposed to be initializing? What happens if you just remove that line (is it really needed, there)?
You need to login to post a reply.