0

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..

 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);

}
Rahat Islam Khan
  • 125
  • 1
  • 6
  • 25
  • 1
    Your question is not very clear. Normally when you add data to a table you need to add multiple pieces of data because a row usually contains multiple columns. Therefore, people would generally use a model JDialog to gather the data. Then when the user enters the data they would click on a "Save" or "Cancel" button. In this case you would just add an ActionListen to the button to get the data and then invoke the addRow() method on the table model. Your question about using enter is not very clear to me. In any case DO NOT use a KeyListener. – camickr Jul 23 '13 at 14:36
  • For [example](http://stackoverflow.com/a/9095442/230513). – trashgod Jul 23 '13 at 14:56
  • @camickr 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. – Rahat Islam Khan Jul 23 '13 at 16:55

2 Answers2

1

If the data to be added is being entered in a JTextField, an actionEvent should be fired when you press enter.

inputField.addActionListener(listener);  

Where listener is the container for your actionPerformed method.

But otherwise go with Nizil's suggestion and use KetListener.

GreenGodot
  • 6,030
  • 10
  • 37
  • 66
1

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

You can make a button on the dialog the default button. It will be invoked when the Enter key is pressed. See Enter Key and Button for a solution.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • it somehow helped me but not the actual ans ..i get from here (http://www.dreamincode.net/forums/topic/245148-java-key-binding-tutorial-and-demo-program/) – Rahat Islam Khan Jul 23 '13 at 20:27
  • @RahatIslamKhan, that is NOT a good example. That is NOT the way to invoke a button when the enter key is pressed. – camickr Jul 23 '13 at 21:13