Friday, 17 April 2020

Tic Toc Toe Game Code : (JtextFiled Key Released Event)

    private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {                                       
        // TODO add your handling code here:
        String val1 = jTextField1.getText();
        String val2 = jTextField2.getText();
        String val3 = jTextField3.getText();
        String val4 = jTextField4.getText();
        String val5 = jTextField5.getText();
        String val7 = jTextField7.getText();
        String val9 = jTextField9.getText();

        if (val1.equals("o") && val2.equals("o") && val3.equals("o")) {
            jTextField1.setBackground(Color.red);
            jTextField2.setBackground(Color.red);
            jTextField3.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "o player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
           
            jTextField1.setBackground(Color.white);
            jTextField2.setBackground(Color.white);
            jTextField3.setBackground(Color.white);
           
        } else if (val1.equals("x") && val2.equals("x") && val3.equals("x")) {
            jTextField1.setBackground(Color.red);
            jTextField2.setBackground(Color.red);
            jTextField3.setBackground(Color.red);
           
           
            JOptionPane.showMessageDialog(this, "x Player Win");
            jTextField1.setBackground(Color.white);
            jTextField2.setBackground(Color.white);
            jTextField3.setBackground(Color.white);
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
        } else if (val1.equals("o") && val4.equals("o") && val7.equals("o")) {
            jTextField1.setBackground(Color.red);
            jTextField4.setBackground(Color.red);
            jTextField7.setBackground(Color.red);
           
            JOptionPane.showMessageDialog(this, "o player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField1.setBackground(Color.white);
            jTextField4.setBackground(Color.white);
            jTextField7.setBackground(Color.white);
        } else if (val1.equals("x") && val4.equals("x") && val7.equals("x")) {
            jTextField1.setBackground(Color.red);
            jTextField4.setBackground(Color.red);
            jTextField7.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "x player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField1.setBackground(Color.white);
            jTextField4.setBackground(Color.white);
            jTextField7.setBackground(Color.white);
        } else if (val1.equals("o") && val5.equals("o") && val9.equals("o")) {
            jTextField1.setBackground(Color.red);
            jTextField5.setBackground(Color.red);
            jTextField9.setBackground(Color.red);
           
            JOptionPane.showMessageDialog(this, "o Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField1.setBackground(Color.white);
            jTextField5.setBackground(Color.white);
            jTextField9.setBackground(Color.white);
           
        } else if (val1.equals("x") && val5.equals("x") && val9.equals("x")) {
            jTextField1.setBackground(Color.red);
            jTextField5.setBackground(Color.red);
            jTextField9.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "x Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField1.setBackground(Color.white);
            jTextField5.setBackground(Color.white);
            jTextField9.setBackground(Color.white);
        }
    }                                     

    private void jTextField2KeyReleased(java.awt.event.KeyEvent evt) {                                       
        // TODO add your handling code here:
        String val1 = jTextField1.getText();
        String val2 = jTextField2.getText();
        String val3 = jTextField3.getText();
        String val5 = jTextField5.getText();
        String val8 = jTextField8.getText();

        if (val1.equals("o") && val2.equals("o") && val3.equals("o")) {
            jTextField1.setBackground(Color.red);
            jTextField2.setBackground(Color.red);
            jTextField3.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "o Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField1.setBackground(Color.white);
            jTextField2.setBackground(Color.white);
            jTextField3.setBackground(Color.white);
        } else if (val1.equals("x") && val2.equals("x") && val3.equals("x")) {
            jTextField1.setBackground(Color.red);
            jTextField2.setBackground(Color.red);
            jTextField3.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "x Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField1.setBackground(Color.white);
            jTextField2.setBackground(Color.white);
            jTextField3.setBackground(Color.white);
        } else if (val2.equals("o") && val5.equals("o") && val8.equals("o")) {
            jTextField2.setBackground(Color.red);
            jTextField5.setBackground(Color.red);
            jTextField8.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "o Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField2.setBackground(Color.white);
            jTextField5.setBackground(Color.white);
            jTextField8.setBackground(Color.white);
        } else if (val2.equals("x") && val5.equals("x") && val8.equals("x")) {
            jTextField2.setBackground(Color.red);
            jTextField5.setBackground(Color.red);
            jTextField8.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "x Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField2.setBackground(Color.white);
            jTextField5.setBackground(Color.white);
            jTextField8.setBackground(Color.white);
        }
    }                                     

    private void jTextField3KeyReleased(java.awt.event.KeyEvent evt) {                                       
        // TODO add your handling code here:
        String val1 = jTextField1.getText();
        String val2 = jTextField2.getText();
        String val3 = jTextField3.getText();
        String val5 = jTextField5.getText();
        String val6 = jTextField6.getText();
        String val7 = jTextField7.getText();
        String val9 = jTextField9.getText();

        if (val1.equals("o") && val2.equals("o") && val3.equals("o")) {
            jTextField1.setBackground(Color.red);
            jTextField2.setBackground(Color.red);
            jTextField3.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "o Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField1.setBackground(Color.white);
            jTextField2.setBackground(Color.white);
            jTextField3.setBackground(Color.white);
        } else if (val1.equals("x") && val2.equals("x") && val3.equals("x")) {
            jTextField1.setBackground(Color.red);
            jTextField2.setBackground(Color.red);
            jTextField3.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "x Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField1.setBackground(Color.white);
            jTextField2.setBackground(Color.white);
            jTextField3.setBackground(Color.white);
        } else if (val3.equals("o") && val6.equals("o") && val9.equals("o")) {
            jTextField3.setBackground(Color.red);
            jTextField6.setBackground(Color.red);
            jTextField9.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "o Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField3.setBackground(Color.white);
            jTextField6.setBackground(Color.white);
            jTextField9.setBackground(Color.white);
        } else if (val3.equals("x") && val6.equals("x") && val9.equals("x")) {
            jTextField3.setBackground(Color.red);
            jTextField6.setBackground(Color.red);
            jTextField9.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "x Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField3.setBackground(Color.white);
            jTextField6.setBackground(Color.white);
            jTextField9.setBackground(Color.white);
        } else if (val3.equals("o") && val5.equals("o") && val7.equals("o")) {
           
            jTextField3.setBackground(Color.red);
            jTextField5.setBackground(Color.red);
            jTextField7.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "o Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
           
            jTextField3.setBackground(Color.white);
            jTextField5.setBackground(Color.white);
            jTextField7.setBackground(Color.white);
           
        } else if (val3.equals("x") && val5.equals("x") && val7.equals("x")) {
            jTextField3.setBackground(Color.red);
            jTextField5.setBackground(Color.red);
            jTextField7.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "x Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField3.setBackground(Color.white);
            jTextField5.setBackground(Color.white);
            jTextField7.setBackground(Color.white);
        }
    }                                     

    private void jTextField4KeyReleased(java.awt.event.KeyEvent evt) {                                       
        // TODO add your handling code here:
        String val1 = jTextField1.getText();
        String val4 = jTextField4.getText();
        String val5 = jTextField5.getText();
        String val6 = jTextField6.getText();
        String val7 = jTextField7.getText();

        if (val1.equals("o") && val4.equals("o") && val7.equals("o")) {
            jTextField1.setBackground(Color.red);
            jTextField4.setBackground(Color.red);
            jTextField7.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "o Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField1.setBackground(Color.white);
            jTextField4.setBackground(Color.white);
            jTextField7.setBackground(Color.white);
        } else if (val1.equals("x") && val4.equals("x") && val7.equals("x")) {
            jTextField1.setBackground(Color.red);
            jTextField4.setBackground(Color.red);
            jTextField7.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "x Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField1.setBackground(Color.white);
            jTextField4.setBackground(Color.white);
            jTextField7.setBackground(Color.white);
        } else if (val4.equals("o") && val5.equals("o") && val6.equals("o")) {
            jTextField4.setBackground(Color.red);
            jTextField5.setBackground(Color.red);
            jTextField6.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "o Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField4.setBackground(Color.white);
            jTextField5.setBackground(Color.white);
            jTextField6.setBackground(Color.white);
        } else if (val4.equals("x") && val5.equals("x") && val6.equals("x")) {
            jTextField4.setBackground(Color.red);
            jTextField5.setBackground(Color.red);
            jTextField6.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "x Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField4.setBackground(Color.white);
            jTextField5.setBackground(Color.white);
            jTextField6.setBackground(Color.white);
        }
    }                                     

    private void jTextField5KeyReleased(java.awt.event.KeyEvent evt) {                                       
        // TODO add your handling code here:
        String val1 = jTextField1.getText();
        String val2 = jTextField2.getText();
        String val3 = jTextField3.getText();
        String val4 = jTextField4.getText();
        String val5 = jTextField5.getText();
        String val6 = jTextField6.getText();
        String val7 = jTextField7.getText();
        String val8 = jTextField8.getText();
        String val9 = jTextField9.getText();

        if (val4.equals("o") && val5.equals("o") && val6.equals("o")) {
            jTextField4.setBackground(Color.red);
            jTextField5.setBackground(Color.red);
            jTextField6.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "o Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
           
            jTextField4.setBackground(Color.white);
            jTextField5.setBackground(Color.white);
            jTextField6.setBackground(Color.white);
        } else if (val4.equals("x") && val5.equals("x") && val6.equals("x")) {
            jTextField4.setBackground(Color.red);
            jTextField5.setBackground(Color.red);
            jTextField6.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "x Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
           
            jTextField4.setBackground(Color.white);
            jTextField5.setBackground(Color.white);
            jTextField6.setBackground(Color.white);
        } else if (val2.equals("o") && val5.equals("o") && val8.equals("o")) {
            jTextField2.setBackground(Color.red);
            jTextField5.setBackground(Color.red);
            jTextField8.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "o Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField2.setBackground(Color.white);
            jTextField5.setBackground(Color.white);
            jTextField8.setBackground(Color.white);
        } else if (val2.equals("x") && val5.equals("x") && val8.equals("x")) {
        jTextField2.setBackground(Color.red);
            jTextField5.setBackground(Color.red);
            jTextField8.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "x Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField2.setBackground(Color.white);
            jTextField5.setBackground(Color.white);
            jTextField8.setBackground(Color.white);
        } else if (val1.equals("o") && val5.equals("o") && val9.equals("o")) {
            jTextField1.setBackground(Color.red);
            jTextField5.setBackground(Color.red);
            jTextField9.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "0 Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField1.setBackground(Color.white);
            jTextField5.setBackground(Color.white);
            jTextField9.setBackground(Color.white);
        } else if (val1.equals("x") && val5.equals("x") && val9.equals("x")) {
            jTextField1.setBackground(Color.red);
            jTextField5.setBackground(Color.red);
            jTextField9.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "x Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField1.setBackground(Color.white);
            jTextField5.setBackground(Color.white);
            jTextField9.setBackground(Color.white);
        } else if (val3.equals("o") && val5.equals("o") && val7.equals("o")) {
            jTextField3.setBackground(Color.red);
            jTextField5.setBackground(Color.red);
            jTextField7.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "o Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField3.setBackground(Color.white);
            jTextField5.setBackground(Color.white);
            jTextField7.setBackground(Color.white);
        } else if (val3.equals("x") && val5.equals("x") && val7.equals("x")) {
            jTextField3.setBackground(Color.red);
            jTextField5.setBackground(Color.red);
            jTextField7.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "x Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField3.setBackground(Color.white);
            jTextField5.setBackground(Color.white);
            jTextField7.setBackground(Color.white);
        }
    }                                     

    private void jTextField6KeyReleased(java.awt.event.KeyEvent evt) {                                       
        // TODO add your handling code here:
        String val1 = jTextField1.getText();
        String val2 = jTextField2.getText();
        String val3 = jTextField3.getText();
        String val4 = jTextField4.getText();
        String val5 = jTextField5.getText();
        String val6 = jTextField6.getText();
        String val7 = jTextField7.getText();
        String val8 = jTextField8.getText();
        String val9 = jTextField9.getText();

        if (val4.equals("o") && val5.equals("o") && val6.equals("o")) {
            jTextField4.setBackground(Color.red);
            jTextField5.setBackground(Color.red);
            jTextField6.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "o player  Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField4.setBackground(Color.white);
            jTextField5.setBackground(Color.white);
            jTextField6.setBackground(Color.white);
        } else if (val4.equals("x") && val5.equals("x") && val6.equals("x")) {
            jTextField4.setBackground(Color.red);
            jTextField5.setBackground(Color.red);
            jTextField6.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "x player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField4.setBackground(Color.white);
            jTextField5.setBackground(Color.white);
            jTextField6.setBackground(Color.white);
        } else if (val3.equals("o") && val6.equals("o") && val9.equals("o")) {
            jTextField3.setBackground(Color.red);
            jTextField6.setBackground(Color.red);
            jTextField9.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "o player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
           
            jTextField3.setBackground(Color.white);
            jTextField6.setBackground(Color.white);
            jTextField9.setBackground(Color.white);
        } else if (val3.equals("x") && val6.equals("x") && val9.equals("x")) {
            jTextField3.setBackground(Color.red);
            jTextField6.setBackground(Color.red);
            jTextField9.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "x player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField3.setBackground(Color.white);
            jTextField6.setBackground(Color.white);
            jTextField9.setBackground(Color.white);
        }
    }                                     

    private void jTextField7KeyReleased(java.awt.event.KeyEvent evt) {                                       
        // TODO add your handling code here:
        String val1 = jTextField1.getText();
        String val2 = jTextField2.getText();
        String val3 = jTextField3.getText();
        String val4 = jTextField4.getText();
        String val5 = jTextField5.getText();
        String val6 = jTextField6.getText();
        String val7 = jTextField7.getText();
        String val8 = jTextField8.getText();
        String val9 = jTextField9.getText();

        if (val1.equals("o") && val4.equals("o") && val7.equals("o")) {
            jTextField1.setBackground(Color.red);
            jTextField4.setBackground(Color.red);
            jTextField7.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "o Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField1.setBackground(Color.white);
            jTextField4.setBackground(Color.white);
            jTextField7.setBackground(Color.white);
        } else if (val1.equals("x") && val4.equals("x") && val7.equals("x")) {
            jTextField1.setBackground(Color.red);
            jTextField4.setBackground(Color.red);
            jTextField7.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "x Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField1.setBackground(Color.white);
            jTextField4.setBackground(Color.white);
            jTextField7.setBackground(Color.white);
        } else if (val7.equals("o") && val8.equals("o") && val9.equals("o")) {
            jTextField7.setBackground(Color.red);
            jTextField8.setBackground(Color.red);
            jTextField9.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "o Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField7.setBackground(Color.white);
            jTextField8.setBackground(Color.white);
            jTextField9.setBackground(Color.white);
        } else if (val7.equals("x") && val8.equals("x") && val9.equals("x")) {
            jTextField7.setBackground(Color.red);
            jTextField8.setBackground(Color.red);
            jTextField9.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "x Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
           
            jTextField7.setBackground(Color.white);
            jTextField8.setBackground(Color.white);
            jTextField9.setBackground(Color.white);
        } else if (val3.equals("o") && val5.equals("o") && val7.equals("o")) {
            jTextField3.setBackground(Color.red);
            jTextField5.setBackground(Color.red);
            jTextField7.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "o Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField3.setBackground(Color.white);
            jTextField5.setBackground(Color.white);
            jTextField7.setBackground(Color.white);
        } else if (val3.equals("x") && val5.equals("x") && val7.equals("x")) {
            jTextField3.setBackground(Color.red);
            jTextField5.setBackground(Color.red);
            jTextField7.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "x Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField3.setBackground(Color.white);
            jTextField5.setBackground(Color.white);
            jTextField7.setBackground(Color.white);
        }
    }                                     

    private void jTextField8KeyReleased(java.awt.event.KeyEvent evt) {                                       
        // TODO add your handling code here:
        String val1 = jTextField1.getText();
        String val2 = jTextField2.getText();
        String val3 = jTextField3.getText();
        String val4 = jTextField4.getText();
        String val5 = jTextField5.getText();
        String val6 = jTextField6.getText();
        String val7 = jTextField7.getText();
        String val8 = jTextField8.getText();
        String val9 = jTextField9.getText();

        if (val2.equals("o") && val5.equals("o") && val8.equals("o")) {
            jTextField2.setBackground(Color.red);
            jTextField5.setBackground(Color.red);
            jTextField8.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "o Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
           
            jTextField2.setBackground(Color.white);
            jTextField5.setBackground(Color.white);
            jTextField8.setBackground(Color.white);
        } else if (val2.equals("x") && val5.equals("x") && val8.equals("x")) {
            jTextField2.setBackground(Color.red);
            jTextField5.setBackground(Color.red);
            jTextField8.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "x Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField2.setBackground(Color.white);
            jTextField5.setBackground(Color.white);
            jTextField8.setBackground(Color.white);
        } else if (val7.equals("o") && val8.equals("o") && val9.equals("o")) {
            jTextField7.setBackground(Color.red);
            jTextField8.setBackground(Color.red);
            jTextField9.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "o Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField7.setBackground(Color.white);
            jTextField8.setBackground(Color.white);
            jTextField9.setBackground(Color.white);
        } else if (val7.equals("x") && val8.equals("x") && val9.equals("x")) {
            jTextField7.setBackground(Color.red);
            jTextField8.setBackground(Color.red);
            jTextField9.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "x Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField7.setBackground(Color.white);
            jTextField8.setBackground(Color.white);
            jTextField9.setBackground(Color.white);
        }
    }                                     

    private void jTextField9KeyReleased(java.awt.event.KeyEvent evt) {                                       
        // TODO add your handling code here:
       
        String val1 = jTextField1.getText();
        String val2 = jTextField2.getText();
        String val3 = jTextField3.getText();
        String val4 = jTextField4.getText();
        String val5 = jTextField5.getText();
        String val6 = jTextField6.getText();
        String val7 = jTextField7.getText();
        String val8 = jTextField8.getText();
        String val9 = jTextField9.getText();
       

        if (val1.equals("o") && val5.equals("o") && val9.equals("o")) {
            jTextField1.setBackground(Color.red);
            jTextField5.setBackground(Color.red);
            jTextField9.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "o Player  Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField1.setBackground(Color.white);
            jTextField5.setBackground(Color.white);
            jTextField9.setBackground(Color.white);
        } else if (val1.equals("x") && val5.equals("x") && val9.equals("x")) {
            jTextField1.setBackground(Color.red);
            jTextField5.setBackground(Color.red);
            jTextField9.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "x Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField1.setBackground(Color.white);
            jTextField5.setBackground(Color.white);
            jTextField9.setBackground(Color.white);
        } else if (val7.equals("o") && val8.equals("o") && val9.equals("o")) {
            jTextField7.setBackground(Color.red);
            jTextField8.setBackground(Color.red);
            jTextField9.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "o Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField7.setBackground(Color.white);
            jTextField8.setBackground(Color.white);
            jTextField9.setBackground(Color.white);
        } else if (val7.equals("x") && val8.equals("x") && val9.equals("x")) {
            jTextField7.setBackground(Color.red);
            jTextField8.setBackground(Color.red);
            jTextField9.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "x Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField7.setBackground(Color.white);
            jTextField8.setBackground(Color.white);
            jTextField9.setBackground(Color.white);
        } else if (val3.equals("o") && val6.equals("o") && val9.equals("o")) {
            jTextField3.setBackground(Color.red);
            jTextField6.setBackground(Color.red);
            jTextField9.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "o player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField3.setBackground(Color.white);
            jTextField6.setBackground(Color.white);
            jTextField9.setBackground(Color.white);
        } else if (val3.equals("x") && val6.equals("x") && val9.equals("x")) {
            jTextField3.setBackground(Color.red);
            jTextField6.setBackground(Color.red);
            jTextField9.setBackground(Color.red);
            JOptionPane.showMessageDialog(this, "x Player Win");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextField7.setText("");
            jTextField8.setText("");
            jTextField9.setText("");
            jTextField3.setBackground(Color.white);
            jTextField6.setBackground(Color.white);
            jTextField9.setBackground(Color.white);
        }
    }                                     

Wednesday, 8 April 2020

Java web Logout Example Source

index.html

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
      <center><form action="LoginServlet" method="post"> 
            <table>
                <tr>
                    <th>UserName:</th><th><input type="text" name="username"/></th>
                </tr>
                <tr>
                    <th>Password:</th><th><input type="password" name="password"/></th>
                </tr>
                <tr>
                    <th></th>
                    <th><input type="submit" value="login"/></th> 
            </tr>
            </table>
        </form>  </center>

    </body>
</html>


2. Home.jsp

<%--
    Document   : Home
    Created on : Apr 8, 2020, 5:13:44 PM
    Author     : Sahaj
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
    <Center>
        Welcome <%=session.getAttribute("user")%>
               
        </br></br> Home Page (Login Success)
       
       </br></br> <a href="./ProfileServlet">My Profile</a> 
        </br></br> <a href="./LogoutServlet">Logout</a> 
       
    </Center>
    </body>
</html>


3.ProfilePage.html

<!DOCTYPE html>
<!--
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.
-->
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
    <center>
        This is Profile Page
    </center>
    </body>
</html>


4.LoginServlet.java

/*
 * 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.
 */

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Sahaj
 */
@WebServlet(urlPatterns = {"/LoginServlet"})
public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        // accept username and password from index.html file
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        
            if(username.equals("abc")&&password.equals("123")){
                //here set static username and password if user abc and pass 123 than go to home page with session value
                HttpSession session = request.getSession();
                //get username from login username value and set it to session attribute
                String name = request.getParameter("username");
                session.setAttribute("user", name);
                response.sendRedirect("Home.jsp");
               
            }else{
                //wrong username and password
                out.println("wrong username and password...");
            }
    }

    }

   
5 .LogoutServlet.java


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Sahaj
 */
@WebServlet(urlPatterns = {"/LogoutServlet"})
public class LogoutServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            //get current session
            HttpSession session = request.getSession();
            //clear
            session.invalidate();
            //than go to login page
            
            response.sendRedirect("index.html");
    }


}


6.ProfileServlet.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Sahaj
 */
@WebServlet(urlPatterns = {"/ProfileServlet"})
public class ProfileServlet extends HttpServlet {


   
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            //getsession
            HttpSession session = request.getSession();
            //check if session is null or not if null than redirect to login
            if(session.getAttribute("user")!=null){
                //if session not null than go to profilepage
                response.sendRedirect("ProfilePage.html");
            }else{
                // is session attribute is null than redirect to login page
                response.sendRedirect("index.html");
            }
    }

   

}










Sunday, 22 September 2019

Connect JAVA with MYSQL database | insert Data into Table Example Source Code

/* action perform event for insert data into table
 Database : student_info
Table : class5
root username of mysql db
mysql@123 is a password of db

*/
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        //first get rollno and name enter by user
        String rollNo = txtRollNo.getText();
        String Name = txtName.getText();
       
        //convert roll no into integer
        int RNo = Integer.parseInt(rollNo);
        //connection
        try{
            //open connection
            Class.forName("com.mysql.jdbc.Driver");
           
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_info","root","mysql@123");
            //here student_info is a database
            //root is username of mysql and mysql@123 is a password
           
            //for insert data into table
            Statement stm = con.createStatement();
           
            String sql = "INSERT INTO CLASS5  VALUES("+RNo+",'"+Name+"')";
            //class5 in above queary is a table
            //EXECUTE STATEMENT
            stm.executeUpdate(sql); //here insert query apply
           
            //display message of record inserted
            JOptionPane.showMessageDialog(this, "student Record Inserted");
            txtName.setText("");
            txtRollNo.setText("");
            //close connection
            con.close();
           
           
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
    }                                        

Friday, 26 July 2019

Game Source Netbeans java

package oddevengame;

import java.util.Random;
import javax.swing.JOptionPane;

/**
 *
 * @author Sahaj
 */
public class ODDEven extends javax.swing.JFrame {
        static int score = 0; // score each corrent question add 1
        static int count = 0; // number of  question out of 10
       
        //generate random number for each question(number)
        Random r = new Random();
        int randomNum;
       
    /**
     * Creates new form ODDEven
     */
    public ODDEven() {
        initComponents();
        //first time generate random
        randomNum = r.nextInt(100);
        lblNumber.setText(String.valueOf(randomNum));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                         
    private void initComponents() {

        lblNumber = new javax.swing.JLabel();
        btnOdd = new javax.swing.JButton();
        btnEven = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        lblScore = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lblNumber.setFont(new java.awt.Font("Tahoma", 1, 48)); // NOI18N
        lblNumber.setForeground(new java.awt.Color(255, 0, 51));

        btnOdd.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        btnOdd.setText("ODD");
        btnOdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOddActionPerformed(evt);
            }
        });

        btnEven.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        btnEven.setText("EVEN");
        btnEven.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEvenActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setText("SCORE");

        lblScore.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(51, 51, 51)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnOdd, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(34, 34, 34)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblScore, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnEven, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(122, 122, 122)
                        .addComponent(lblNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(40, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(lblNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnOdd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnEven, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(47, 47, 47)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblScore, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(41, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                       

    private void btnOddActionPerformed(java.awt.event.ActionEvent evt) {                                     
        // TODO add your handling code here:
        //odd operation
        count = count+1; //each button click count question add 1 out of 10
        int num = Integer.parseInt(lblNumber.getText());  //lblnumber is random num and convert to int for odd even operation
        if(count==10){
            //when counter reach last question(number)
            //than it calculate for last number
            if(num%2!=0){
                // number corret add score
                score = score+1;
                lblScore.setText(String.valueOf(score)); //set value of score when answer correct
                randomNum = r.nextInt(100); // random number generate for next question number
               
            }else{
                //if not odd number
                randomNum = r.nextInt(100);
                lblNumber.setText(String.valueOf(randomNum)); //set randomnumber
                             
            }
           
            //result display on 10th question
            JOptionPane.showMessageDialog(this, "Your score is: "+score+"/10");
            //reset values after score
            count=0;
            score =0;
            lblScore.setText("");
            randomNum = r.nextInt(100);
            lblNumber.setText(String.valueOf(randomNum));
           
        }else{
            // if number not reach 10th qestion ( for 1 to 9 )
            if(num%2!=0){
                score = score+1;
                lblScore.setText(String.valueOf(score)); //set score value
                //generate random number for next question
                randomNum = r.nextInt(100);
                lblNumber.setText(String.valueOf(randomNum));
            }else{
                //if not odd than generate random for next
                randomNum = r.nextInt(100);
                lblNumber.setText(String.valueOf(randomNum));
            }
        }
       
       
       
    }                                     

    private void btnEvenActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        // for even do opposite
        //paste here and change
       
        count = count+1; //each button click count question add 1 out of 10
        int num = Integer.parseInt(lblNumber.getText());  //lblnumber is random num and convert to int for odd even operation
        if(count==10){
            //when counter reach last question(number)
            //than it calculate for last number
            if(num%2==0){
                // number corret add score
                score = score+1;
                lblScore.setText(String.valueOf(score)); //set value of score when answer correct
                randomNum = r.nextInt(100); // random number generate for next question number
               
            }else{
                //if not odd number
                randomNum = r.nextInt(100);
                lblNumber.setText(String.valueOf(randomNum)); //set randomnumber
                             
            }
           
            //result display on 10th question
            JOptionPane.showMessageDialog(this, "Your score is: "+score+"/10");
            //reset values after score
            count=0;
            score =0;
            lblScore.setText("");
            randomNum = r.nextInt(100);
            lblNumber.setText(String.valueOf(randomNum));
           
        }else{
            // if number not reach 10th qestion ( for 1 to 9 )
            if(num%2==0){
                score = score+1;
                lblScore.setText(String.valueOf(score)); //set score value
                //generate random number for next question
                randomNum = r.nextInt(100);
                lblNumber.setText(String.valueOf(randomNum));
            }else{
                //if not odd than generate random for next
                randomNum = r.nextInt(100);
                lblNumber.setText(String.valueOf(randomNum));
            }
        }
       
       
    }                                     

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ODDEven.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ODDEven.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ODDEven.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ODDEven.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ODDEven().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                   
    private javax.swing.JButton btnEven;
    private javax.swing.JButton btnOdd;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lblNumber;
    private javax.swing.JLabel lblScore;
    // End of variables declaration                 
}

JTable Hide and Show in Java Swing