title: How to set focus on JDateChooser when frame is loaded in Swing?
body: <p><img src="https://i.stack.imgur.com/0kx5m.png" alt="enter image description here"></p>
  
  <p>How to show focus on date chooser when frame is loaded?</p>
  
  <ol>
  <li>how could verify textfield that user can enter only date format and </li>
  <li>way how could i set focus on jdatechooser icon  in swing. </li>
  </ol>
  
  <p>code</p>
  
  <pre><code>private void jDateChooser2FocusGained(java.awt.event.FocusEvent evt) {
      // TODO add your handling code here:
          //JDateChooser2 jdc = new JDateChooser2("DD/MM/YYYY", true);
      //jDateChooser2.getDateEditor().getUiComponent().requestFocusInWindow();
    jDateChooser2.requestFocusInWindow();
  
  }      
  </code></pre>
  
  <p>source code</p>
  
  <pre><code>public class welcome extends javax.swing.JFrame {
  
      public static final String DATE_FORMAT_NOW = "dd/MMM/YYYY ";
      Calendar cal = Calendar.getInstance();
      SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
      Date date = new Date();
  
  
  
      /**
       * Creates new form welcome
       */
      public welcome() {
          initComponents();
  
  
  
           sdf.setLenient(false);
  
           String dt = sdf.format(cal.getTime());
           System.out.println(dt);
  
  
              try  
              {  
                  date = sdf.parse(dt); 
                   System.out.println(date);
              }  
              catch(ParseException pe)  
              {  
                  System.out.println("pe: " + pe.getMessage());  
                  Toolkit.getDefaultToolkit().beep();  
              }  
              jLabel3.setText(dt);
              jTextField1.setText(dt);
       }
  
  private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {                                       
          // TODO add your handling code here:
     //   System.out.println("guihgio");
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
  
              System.out.println("enter press key");
  
  
                this code is not working//
  
        char c = evt.getKeyChar();
        if (!((c &gt;= '0') &amp;&amp; (c &lt;= '9') ||
           (c == KeyEvent.VK_BACK_SPACE) ||
           (c == KeyEvent.VK_DELETE) || (c == KeyEvent.VK_SLASH)))        
        {
  
             JOptionPane.showMessageDialog(null, "Please Enter Valid");
             evt.consume();
        }
    }
  </code></pre>
  
  <p>MY EVENT CODE  private void jSpinner1KeyPressed(java.awt.event.KeyEvent evt) {<br>
          // TODO add your handling code here:
              System.out.println("spiinejngyjgkur");</p>
  
  <pre><code>     if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
          //password pwd = new password();
          //pwd.setVisible(true);
  
  
  
  
  
  
  
  
  
  
       jSpinner1.requestFocus();
  
         if (evt.getSource() == jSpinner1) //add
      {
          try {
              String host = "jdbc:mysql://localhost:3306/indospirit";
              String uName = "root";
              String uPass = "paras123";
  
              //Class.forName("com.mysql.jdbc.Driver").newInstance();
              java.sql.Driver d = new com.mysql.jdbc.Driver();
  
              Connection con = DriverManager.getConnection(host, uName, uPass);
  
              PreparedStatement ps;
                  System.out.println("weww");
  
        ps = con.prepareStatement("INSERT INTO `log1`(`date`)VALUES('" + dateString + "')");
  
              int i = ps.executeUpdate();
  
              if (i &gt; 0) {
                  JOptionPane.showMessageDialog(null, "Record Added");
              } else {
                  JOptionPane.showMessageDialog(null, "Record NOT Added");
              }
          } catch (SQLException ex) {
              //ex.printStackTrace();
              System.err.println(ex.toString());
          } catch (Exception ex1) {
              //ex1.printStackTrace();
              System.err.println(ex1.toString());
          }
      }
  
      try {
          String host = "jdbc:mysql://localhost:3306/indospirit";
          String uName = "root";
          String uPass = "paras123";
  
          //Class.forName("com.mysql.jdbc.Driver").newInstance();
          java.sql.Driver d = new com.mysql.jdbc.Driver();
  
          Connection con = DriverManager.getConnection(host, uName, uPass);
          Statement stmt = con.createStatement();
          ResultSet rs = stmt.executeQuery("SELECT * from `log1` ORDER BY `date` DESC limit 1");
  
          while (rs.next()) {
  
              int col = rs.getInt("id");
              String str = rs.getString("date");
  
              jLabel10.setText(str);
  
              System.out.println(col + " " + str + " ");
  
          }
  
      } catch (SQLException ex) {
          System.err.println(ex.toString());
      } catch (Exception ex1) {
          System.err.println(ex1.toString());
      }
  
           } 
  
  
          event not working
  </code></pre>
  
  <p>private void jSpinner1StateChanged(javax.swing.event.ChangeEvent evt) {<br>
          // TODO add your handling code here:
         System.out.println("gijjhbip");
           System.out.println("Source: " + evt.getSource());
      }                                      </p>
  
  <pre><code>     how could  I check THAT date Enterd by user in date format in JFormattedTextField...please help  IN KEY PRESSED EVENT
  
         my code is here
  </code></pre>
  
  <p>private void jFormattedTextField1KeyPressed(java.awt.event.KeyEvent evt) {<br>
          // TODO add your handling code here:</p>
  
  <pre><code>    System.out.println(evt.getKeyChar());
  
     if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
  
         Date date = (Date) evt.getSource();
        if(!(date.equals(format)))
        {
            evt.consume();
        }
     }
  </code></pre>