Search code examples
javaswingjtablelistenerlistselectionlistener

Updating jtextfield's from jtable


Can someone help me with this listener?

table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
    public void valueChanged(ListSelectionEvent e){
        if(e.getValueIsAdjusting()){
            ListSelectionModel model = table.getSelectionModel();  
            int lead = model.getLeadSelectionIndex(); 
            displayRowValues(lead);
        }
    }
    private void displayRowValues(int rowIndex){
        String country = "";   
        Object oCountry = table.getValueAt(rowIndex, 0);  
        country += oCountry.toString();
        countryTxt.setText(country );
    }
});

It's supposed to send data from cell in jtable (table) into a textfield (countryTxt) when one of the row's is selected, but it works only when I click on row and not when I'm cycling trough my table with arrow key's.


Solution

  • If you comment out e.getValueIsAdjusting() it works.

    http://docs.oracle.com/javase/6/docs/api/javax/swing/ListSelectionModel.html#setValueIsAdjusting(boolean)