Cannot bind text field to selected item in JTable in NetBeans

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?

Answers


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


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

Does your Cow class support adding a PropertyChangeListener? I did not use the beans binding support from NetBeans that often, but I remember you needed it. Anyway, a little more code could help with finding out whats going wrong.


Need Your Help

Moment.js diff between UTC dates

javascript node.js date momentjs

I'm using moments.js for working with dates in javascript. All dates are in UTC (or should be).

How to mix bash with python

python bash

I enjoy using unix commands very much, but I came to the point, where I would find embedded python parts useful. This is my code: