简体   繁体   English

按下java键执行操作

[英]java key pressed to perform an action

i am very new to java.I am developing a inventory management system where i want to add the data to the jtable when "ENTER" key is pressed.but i don't know how to do so.i have searched about key bindings but got nothing helpful for me at that initial stage.here is my action that i want to perform at key pressed.. 我是java的新手。我正在开发一个库存管理系统,我想在按下“ENTER”键时将数据添加到jtable。但是我不知道该怎么做。我搜索过关键绑定但是在那个初始阶段对我没有任何帮助。我想在按键时执行我的动作..

 private void addItemActionPerformed(java.awt.event.ActionEvent evt) {                                        
   int quantity,price;
   Product p=new Product();
   String[] result=new String[8];
   String data[]=new String[6];
   int i=0;
   result=p.getInfo(this.addItemField.getText());
    for(String s:result){
        data[i]=s;
        i+=1;
    }
    data[0]="1";
    quantity=Integer.parseInt(data[0]);
    price=Integer.parseInt(data[5]);
    int tPrice=price*quantity;
    data[5]=Integer.toString(tPrice);
    System.out.println(quantity+" "+price);
    table.addRow(data);
    this.addItemField.grabFocus();
  } 

and here is my default constructor 这是我的默认构造函数

    public SellWindow() {

    initComponents();
    String title[]={"Qty","Code","Name","Unit Value","ml/kg","Line Total","Action"};
    entry.getColumnModel().getColumn(0).setPreferredWidth(20);
    table.setColumnIdentifiers(title);
    this.entry.setModel(table);

}

If the data to be added is being entered in a JTextField, an actionEvent should be fired when you press enter. 如果要在JTextField中输入要添加的数据,则在按Enter键时应触发actionEvent。

inputField.addActionListener(listener);  

Where listener is the container for your actionPerformed method. listener是actionPerformed方法的容器。

But otherwise go with Nizil's suggestion and use KetListener. 但是否则请遵循Nizil的建议并使用KetListener。

i mean the action is executed when i clicked the button(Add item).i want it to be executed also when i pressed the ENTER in keyboard 我的意思是当我点击按钮(添加项目)时执行动作。我希望它也可以在按下键盘上的ENTER时执行

You can make a button on the dialog the default button. 您可以在对话框上创建一个默认按钮。 It will be invoked when the Enter key is pressed. 按下Enter键时将调用它。 See Enter Key and Button for a solution. 请参阅输入键和按钮以获取解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM