views:

356

answers:

2

I am trying to use NetBeans to bind a JTextField to the selected element of a JTable.

The JTable gets its data from an AbstractTableModel subclass which returns Cow objects. At present, each Cow object is displayed as a String through its toString method.

I am trying to bind the text property of the JTextField to the name property of the Cow object which is selected in the JTable.

I bound the text property of the JTextField in NetBeans to:

flowTable[${selectedElement.name}]

This produces the following line of generated code:

org.jdesktop.beansbinding.Binding binding = 
  org.jdesktop.beansbinding.Bindings.createAutoBinding(
    org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, 
    cowTable, 
    org.jdesktop.beansbinding.ELProperty.create("${selectedElement.name}"), 
    cowNameTextField, 
    org.jdesktop.beansbinding.BeanProperty.create("text"));

The bound value of the text field is always null.

What am I doing wrong?

A: 

Does your Cow class have a public String getName() method returning the name?

If it doesn't, the outcome you're getting would be expected. If it does, could you post more code (your data class, tablemodel, table...).

JRL
A: 

If you only are interested in a String in the table, and not the Cow object itself:

table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
    @Override
    public void valueChanged(ListSelectionEvent e) {
        if(!e.getValueIsAdjusting()) {
             Object value = table.getValueAt(e.getFirstIndex(), COLUMN_X);
             jTextField.setText(value.toString());
        }
    }
);
Avall