Unable to bind text box to selected item in JTable in NetBeans

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

JTable gets its data from a subclass of AbstractTableModel, which returns Cow objects. Currently, each Cow object is displayed as a string using the toString method.

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

I bound the text property JTextField in NetBeans to:

flowTable[${selectedElement.name}]

The result is 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 associated text field value is always null.

What am I doing wrong?

+4
source share
3 answers

Does your Cow class have a public String getName() method that returns a name?

If this does not happen, the expected result will be expected. If so, can you post more code (your data class, model table, table ...).

0
source

If you are only interested in the row 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()); } } ); 
0
source

Does your Cow class support adding PropertyChangeListener ? I have not often used beans binding support from NetBeans, but I remember that you need it. Anyway, a little more code can help figure out what is going wrong.

0
source

Source: https://habr.com/ru/post/1305726/


All Articles