2017-03-18 61 views
-1

是否有可能在jTextfield中鍵入一個值後,它會自動按下而不使用jButton?我不知道它有可能..好病是用它在我的條碼掃描儀掃描後顯示的值JTextField中會自動接收並開始對數據庫是否有可能在jTextfield中鍵入一個值後它會自動按下而不使用jButton?

+0

是的,這是可能的鍵盤緩衝區,但解決方案的細節取決於您的問題和代碼的細節。請解釋更多,並顯示更多,最好是[mcve]代碼。 –

+0

或者它是否可以在發射條形碼掃描器後自動啓動對我的數據庫的查詢搜索,它會顯示項目信息?對於java和編程新手抱歉 – JoMS

+0

你在問你的問題,就好像我們知道你的代碼是什麼樣子一樣,就好像我們知道目前隱藏的問題的細節。 **再次**請編輯您的問題,更詳細地解釋您正在嘗試做什麼,並顯示相關的代碼。 –

回答

-1

查詢添加KeyListener的初始化文本框後:

textfield.addKeyListener(new KeyListener() { 

    @Override 
    public void keyTyped(KeyEvent e) { 
     System.out.println("typed: "+e.getKeyChar()); 
    } 

    @Override 
    public void keyReleased(KeyEvent e) { 
     System.out.println("released: "+e.getKeyChar()); 
    } 

    @Override 
    public void keyPressed(KeyEvent e) { 
     System.out.println("pressed: "+e.getKeyChar()); 
    } 
}); 

,並根據需要

+0

不,從來不建議在文本組件中使用KeyListener。 –

+0

這會混淆文本組件的固有功能,還有許多其他更高級別的解決方案可用,但是OP尚未告知我們足夠讓我們知道哪個最適合使用。 –

1

是否有可能修改輸入的值在JTextField中它會自動按不使用的JButton後?

您可以將ActionListener添加到文本字段。

當文本字段具有焦點並按下Enter鍵時,將會調用偵聽器。

2

答案取決於你對「新聞」的定義。在我的經驗,期望是,當條形碼掃描,將無需用戶做任何額外的執行動作

基於這個假設,你有兩個基本的選擇

固定長度

如果您知道條碼的長度(和它的常數),你可以使用一個DocumentFilter檢測達到長時,引發行動

public class BarCodeLengthDocumentListener implements DocumentListener { 

    private ActionListener actionListener; 
    private int barCodeLength; 

    public BarCodeLengthDocumentListener(int barCodeLength, ActionListener actionListener) { 
     this.actionListener = actionListener; 
     this.barCodeLength = barCodeLength; 
    } 

    @Override 
    public void insertUpdate(DocumentEvent e) { 
     doCheck(e); 
    } 

    @Override 
    public void removeUpdate(DocumentEvent e) { 
     doCheck(e); 
    } 

    @Override 
    public void changedUpdate(DocumentEvent e) { 
     doCheck(e); 
    } 

    protected void doCheck(DocumentEvent e) { 
     Document doc = e.getDocument(); 
     if (doc.getLength() >= barCodeLength) { 
      try { 
       String text = doc.getText(0, doc.getLength()); 
       ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, text); 
       actionListener.actionPerformed(evt); 
      } catch (BadLocationException exp) { 
       exp.printStackTrace(); 
       ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, null); 
       actionListener.actionPerformed(evt); 
      } 
     } 
    } 

} 

所以,基本上,這允許您指定的EXP條形碼的ected長度和它達到的時候,它會觸發ActionListener,使文本通過ActionEvent

行動遲緩

如果你不知道的長度(或它的變量),另一個選擇是注入某種延遲的當文檔事件發生時,當你觸發ActionListener

public class DelayedDocumentListener implements DocumentListener { 

    private ActionListener actionListener; 
    private String text; 
    private Timer timer; 

    public DelayedDocumentListener(ActionListener actionListener) { 
     this.actionListener = actionListener; 
     timer = new Timer(1000, new ActionListener() { 
             @Override 
             public void actionPerformed(ActionEvent e) { 
              ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, text); 
              actionListener.actionPerformed(evt); 
             } 
            }); 
     timer.setRepeats(false); 
    } 

    @Override 
    public void insertUpdate(DocumentEvent e) { 
     doCheck(e); 
    } 

    @Override 
    public void removeUpdate(DocumentEvent e) { 
     doCheck(e); 
    } 

    @Override 
    public void changedUpdate(DocumentEvent e) { 
     doCheck(e); 
    } 

    protected void doCheck(DocumentEvent e) { 
     try { 
      Document doc = e.getDocument(); 
      text = doc.getText(0, doc.getLength()); 
     } catch (BadLocationException ex) { 
      ex.printStackTrace(); 
     } 
     timer.restart(); 
    } 

} 

所以這裏採用當一個文檔事件發生之間的搖擺Timer產生的延遲(在本例中爲1秒)之間何時會觸發ActionListener,每個新文檔事件中斷Timer,導致它重新啓動。這意味着最後一次文檔事件和觸發ActionListener之間必須至少有1秒鐘的時間。

因爲有時候人們需要手動輸入條形碼,所以你可能想玩這個延遲。

Runnable的例子...

所以,這基本上呈現兩種思路,它採用了java.awt.Robot到擊鍵注入應該模擬大部分的條碼掃描器

import java.awt.AWTException; 
import java.awt.EventQueue; 
import java.awt.Robot; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import javax.swing.event.DocumentEvent; 
import javax.swing.event.DocumentListener; 
import javax.swing.text.BadLocationException; 
import javax.swing.text.Document; 

public class Test { 

    public static void main(String[] args) { 
     new Test(); 
    } 

    public Test() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
      BarCodeLengthDocumentListener lengthListener = new BarCodeLengthDocumentListener(7, new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        String text = e.getActionCommand(); 
        JOptionPane.showMessageDialog(TestPane.this, text); 
       } 
      }); 
      DelayedDocumentListener delayedListener = new DelayedDocumentListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        String text = e.getActionCommand(); 
        JOptionPane.showMessageDialog(TestPane.this, text); 
       } 
      }); 

      JTextField field1 = new JTextField(7); 
      field1.getDocument().addDocumentListener(lengthListener); 
      JTextField field2 = new JTextField(7); 
      field2.getDocument().addDocumentListener(delayedListener); 

      add(field1); 
      add(field2); 

      JButton simLength = new JButton("Simulate Length"); 
      simLength.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        field1.setText(null); 
        field1.requestFocusInWindow(); 
        Thread t = new Thread(new Simulator()); 
        t.start(); 
       } 
      }); 
      JButton simDelay = new JButton("Simulate Delay"); 
      simDelay.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        field2.setText(null); 
        field2.requestFocusInWindow(); 
        Thread t = new Thread(new Simulator()); 
        t.start(); 
       } 
      }); 

      add(simLength); 
      add(simDelay); 
     } 

    } 

    public class Simulator implements Runnable { 

     @Override 
     public void run() { 
      try { 
       Robot bot = new Robot(); 
       type(KeyEvent.VK_1, bot); 
       type(KeyEvent.VK_2, bot); 
       type(KeyEvent.VK_3, bot); 
       type(KeyEvent.VK_4, bot); 
       type(KeyEvent.VK_5, bot); 
       type(KeyEvent.VK_6, bot); 
       type(KeyEvent.VK_7, bot); 
      } catch (AWTException ex) { 
       ex.printStackTrace(); 
      } 
     } 

     protected void type(int keyStoke, Robot bot) { 
      bot.keyPress(keyStoke); 
      bot.keyRelease(keyStoke); 
     } 

    } 

    public class BarCodeLengthDocumentListener implements DocumentListener { 

     private ActionListener actionListener; 
     private int barCodeLength; 

     public BarCodeLengthDocumentListener(int barCodeLength, ActionListener actionListener) { 
      this.actionListener = actionListener; 
      this.barCodeLength = barCodeLength; 
     } 

     @Override 
     public void insertUpdate(DocumentEvent e) { 
      doCheck(e); 
     } 

     @Override 
     public void removeUpdate(DocumentEvent e) { 
      doCheck(e); 
     } 

     @Override 
     public void changedUpdate(DocumentEvent e) { 
      doCheck(e); 
     } 

     protected void doCheck(DocumentEvent e) { 
      Document doc = e.getDocument(); 
      if (doc.getLength() >= barCodeLength) { 
       try { 
        String text = doc.getText(0, doc.getLength()); 
        ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, text); 
        actionListener.actionPerformed(evt); 
       } catch (BadLocationException exp) { 
        exp.printStackTrace(); 
        ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, null); 
        actionListener.actionPerformed(evt); 
       } 
      } 
     } 

    } 

    public class DelayedDocumentListener implements DocumentListener { 

     private ActionListener actionListener; 
     private String text; 
     private Timer timer; 

     public DelayedDocumentListener(ActionListener actionListener) { 
      this.actionListener = actionListener; 
      timer = new Timer(1000, new ActionListener() { 
              @Override 
              public void actionPerformed(ActionEvent e) { 
               ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, text); 
               actionListener.actionPerformed(evt); 
              } 
             }); 
      timer.setRepeats(false); 
     } 

     @Override 
     public void insertUpdate(DocumentEvent e) { 
      doCheck(e); 
     } 

     @Override 
     public void removeUpdate(DocumentEvent e) { 
      doCheck(e); 
     } 

     @Override 
     public void changedUpdate(DocumentEvent e) { 
      doCheck(e); 
     } 

     protected void doCheck(DocumentEvent e) { 
      try { 
       Document doc = e.getDocument(); 
       text = doc.getText(0, doc.getLength()); 
      } catch (BadLocationException ex) { 
       ex.printStackTrace(); 
      } 
      timer.restart(); 
     } 

    } 
} 
相關問題