发布的密钥上的可编辑JcomboBox Action事件无法正常工作

时间:2023-01-27 21:38:21

I have a JComboBox named c , which i have kept editable , i want to call a function named search() whenever any key is pressed/released in that ComboBox ? i tried this but this neither calls my function nor does the job i have written in if() when the condition is true.

我有一个名为c的JComboBox,我保持可编辑,我想在ComboBox中按下/释放任何键时调用名为search()的函数?我尝试了这个,但这既没有调用我的函数,也没有在条件为真时写入if()的工作。

Please let me know how to fire an action when a key is pressed from keyboard through an editable combobox , i m using netbeans.

请通过可编辑的组合框从键盘按下键,让我知道如何触发动作,我使用netbeans。

private void cKeyReleased(java.awt.event.KeyEvent evt) {                              

     if(evt.getKeyCode()==KeyEvent.VK_A )
    {
        //DOES NOT EVEN ENTER THIS METHOD WHEN A IS PRESSED.
        try 
         {
             search();      // MY CALL TO FUNCTION
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }

     if(evt.getKeyCode()==KeyEvent.VK_B )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_C )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_D )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_E )
    {
         try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_F )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_G )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_H )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_I )
    {
        try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_J )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_K )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_L )
    {
      try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_M )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_N )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }




     if(evt.getKeyCode()==KeyEvent.VK_O )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_P )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_Q )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_R )
    {

          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_S )
    {

         try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_T )
    {

          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }



     if(evt.getKeyCode()==KeyEvent.VK_U )
    {

          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_V )
    {

          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }

    if(evt.getKeyCode()==KeyEvent.VK_W )
    {

         try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    } 

    if(evt.getKeyCode()==KeyEvent.VK_X )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }

     if(evt.getKeyCode()==KeyEvent.VK_Y )
    {

         try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }

    }

   if(evt.getKeyCode()==KeyEvent.VK_Z )
    {

         try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }  

     if(evt.getKeyCode()==KeyEvent.VK_1 )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_2 )
    {

          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }

    }


     if(evt.getKeyCode()==KeyEvent.VK_3 )
    {

          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_4 )
    {

          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_5 )
    {

          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }

    }


     if(evt.getKeyCode()==KeyEvent.VK_6 )
    {

          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }

    }


     if(evt.getKeyCode()==KeyEvent.VK_7 )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_8 )
    {

          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }

    }


     if(evt.getKeyCode()==KeyEvent.VK_9 )
    {
         try 
         {
             search();
         } catch (     ClassNotFoundException | SQLException ex) 
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }


    }


     if(evt.getKeyCode()==KeyEvent.VK_0 )
    {
         try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }

    }


}       

1 个解决方案

#1


0  

I USED THIS , EVEN KEY LISTENER DID THE JOB , i did not need document listener @camickr trust me , all i wanted was how to use document listener , i still would love to use it ! i repeat i m new to java and i m developing a project by reading question and answers molding them to fit my requirements ! when i say i did my research , i mean i did it even before posting my question sir. again a small snippet of how to use document listener would be of great help , as i m using netbeans i just want to make a function call whenever key is typed in my editable combobox.

我使用了这个,即使是关键的听众也没有工作,我不需要文件监听器@camickr相信我,我想要的是如何使用文件监听器,我仍然愿意使用它!我重复我的java新手,我正在开发一个项目,通过阅读问题和答案塑造它们以满足我的要求!当我说我做了我的研究时,我的意思是我甚至在发表我的问题之前就做过了。再一个如何使用文档监听器的小片段将会有很大帮助,因为我使用netbeans我只想在我的可编辑组合框中键入键时进行函数调用。

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ComboBoxEditor;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.text.DateFormatter;


public class Adhoc_JComboBox extends JFrame implements KeyListener{

private static JComboBox combobox; 
String a1,a2,a3;
public static void main(String[] args) {
new Adhoc_JComboBox();
}

private Adhoc_JComboBox(){
String array[]= new String[3];

a1=new String("a1");
a2=new String("a2");
a3=new String("a3");
array[0]= a1;
array[1]= a2;
array[2]= a3;
combobox = new JComboBox(array);
combobox.setEditable(true);
JTextField editor = (JTextField) combobox.getEditor().getEditorComponent();
editor.addKeyListener(this);
setLayout(new FlowLayout());
add(combobox);
pack();
setVisible(true);
}

public void keyTyped(KeyEvent arg0) {
System.out.println("Key Typed " +arg0.getKeyCode());
}

public void keyPressed(KeyEvent arg0) {
System.out.println("Key Pressed " +arg0.getKeyCode());
} 
public void keyReleased(KeyEvent arg0) {
System.out.println("Key Released " +arg0.getKeyCode());}}

#1


0  

I USED THIS , EVEN KEY LISTENER DID THE JOB , i did not need document listener @camickr trust me , all i wanted was how to use document listener , i still would love to use it ! i repeat i m new to java and i m developing a project by reading question and answers molding them to fit my requirements ! when i say i did my research , i mean i did it even before posting my question sir. again a small snippet of how to use document listener would be of great help , as i m using netbeans i just want to make a function call whenever key is typed in my editable combobox.

我使用了这个,即使是关键的听众也没有工作,我不需要文件监听器@camickr相信我,我想要的是如何使用文件监听器,我仍然愿意使用它!我重复我的java新手,我正在开发一个项目,通过阅读问题和答案塑造它们以满足我的要求!当我说我做了我的研究时,我的意思是我甚至在发表我的问题之前就做过了。再一个如何使用文档监听器的小片段将会有很大帮助,因为我使用netbeans我只想在我的可编辑组合框中键入键时进行函数调用。

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ComboBoxEditor;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.text.DateFormatter;


public class Adhoc_JComboBox extends JFrame implements KeyListener{

private static JComboBox combobox; 
String a1,a2,a3;
public static void main(String[] args) {
new Adhoc_JComboBox();
}

private Adhoc_JComboBox(){
String array[]= new String[3];

a1=new String("a1");
a2=new String("a2");
a3=new String("a3");
array[0]= a1;
array[1]= a2;
array[2]= a3;
combobox = new JComboBox(array);
combobox.setEditable(true);
JTextField editor = (JTextField) combobox.getEditor().getEditorComponent();
editor.addKeyListener(this);
setLayout(new FlowLayout());
add(combobox);
pack();
setVisible(true);
}

public void keyTyped(KeyEvent arg0) {
System.out.println("Key Typed " +arg0.getKeyCode());
}

public void keyPressed(KeyEvent arg0) {
System.out.println("Key Pressed " +arg0.getKeyCode());
} 
public void keyReleased(KeyEvent arg0) {
System.out.println("Key Released " +arg0.getKeyCode());}}