Results 1 to 3 of 3
  1. #1
    usha is offline Member
    Join Date
    Oct 2010
    Posts
    11
    Rep Power
    0

    Default how to add keyListener event to JTable

    hi,
    I added a keyListener event to one JTable "table1", and i implemented the method keyReleased. now my requirement is when i edit row data of table1 and when i press enter key, one new row should added to another JTable "table2". i implemented the code as below its working perfectly when i press enter key first time and one row also added to table2 ,but when i press enter key second time, the corresponding function calling two times and two rows are added into table2 .

    i implemented the code as follows

    jTable1.addKeyListener(new java.awt.event.KeyAdapter() {

    public void keyReleased(java.awt.event.KeyEvent ke){
    if(ke.getKeyCode()==KeyEvent.VK_ENTER){

    javax.swing.table.DefaultTableModel d1=(javax.swing.table.DefaultTableModel)jTable2.ge tModel();
    d1.addRow(new Object[]{(String)jTable1.getValueAt(jTable1.getRowCount()-1,0)});
    jTable2.updateUI();
    );


    please help me.

    Thanks in advance,
    usha
    Last edited by usha; 10-23-2010 at 11:56 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    Can't know the source of your stated problem without an SSCCE (Short, Self Contained, Compilable and Executable) example, but why on earth are you invoking updateUI() ? Did you even take a look at the documentation for the method?
    Notification from the UIManager that the L&F has changed. Replaces the current UI object with the latest version from the UIManager.

    Another note: Wherever possible, use key bindings, not a KeyListener.

    db
    Last edited by DarrylBurke; 10-23-2010 at 01:20 PM.

  3. #3
    Horcrux7 is offline Member
    Join Date
    Sep 2010
    Posts
    33
    Rep Power
    0

    Default

    I think you have register the key listener 2 times. Add a debug out on the place where you register the listener.
    Volker Berlin
    www.inetsoftware.de

Similar Threads

  1. keyListener not doing anything
    By imorio in forum AWT / Swing
    Replies: 10
    Last Post: 08-17-2010, 10:46 PM
  2. Problem with KeyListener in Jtable
    By sandeepsai17 in forum New To Java
    Replies: 0
    Last Post: 06-30-2009, 10:14 AM
  3. KeyListener - Is this what I need?
    By dbashby in forum New To Java
    Replies: 26
    Last Post: 04-18-2009, 04:14 PM
  4. KeyListener Example
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:46 PM
  5. how to add a KeyListener
    By leonard in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 04:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •