我对java很陌生。我正在开发一个库存管理系统,我想在按下“ENTER”键时将数据添加到jtable中。但我不知道该怎么做。我已经搜索过键绑定(bind),但在初始阶段对我没有任何帮助。这是我想在按下按键时执行的操作..
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();
}
这是我的默认构造函数
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);
}
最佳答案
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 键时将调用它。请参阅Enter Key and Button寻求解决方案。
关于按下 java 键来执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17812319/