5

I wonder, if it possible to make a data binding in java like we can do this in c#? Googled a lot, but there are no answers for me.
I want to bind, for example, collection to Jtable, and want JTable to be updated when I add, delete, or change some elements in collection.

Thanks in advance!

mr.nothing
  • 5,141
  • 10
  • 53
  • 77

4 Answers4

2

Yes, there is Java Beans Binding.

Take a look at:

  1. NetBeans BeansBinding Tutorial
  2. A Scott Violet's blog entry on BB

As for your JTable Example, there are observable Collections, example:

ObservableList<Employee> employees =
    ObservableCollections.observableList(
    new ArrayList<Employee>());

Taken from an article Beans Binding: A Java Data-Binding Solution with a Serious Problem.

Rekin
  • 9,731
  • 2
  • 24
  • 38
1

If your question is about java gui in general, Java FX 2 supports binding natively.
If it is about Swing in particular (your JTable example), there seems to be solutions too but I'm not familiar with them.

Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
  • 1
    yeah, that's really a huge disadvantage of java, if you write a swing desktop application. The best lib for data binding is BBB (BetterBeanBinding), but there are no full examples of how can we use it. I'm really frustrated and disappointed in java. – mr.nothing May 05 '12 at 12:37
  • actually, I'm not tied to it, but I'm not familiar with JavaFX at all and I think building UI on JavaFX is not as simple as on Swing. If I'm wrong, please, correct me. And btw, is there any JavaFX UI builders? – mr.nothing May 05 '12 at 16:28
  • Java fx 2, which has almost nothing to do with java fx 1.3, is supposed to become the main ui platform in java. Building a gui is very similar to what you would do with swing but (i think) better looking. I think the latest version of netbeans has a builder but i haven't used it. Itis worth spending an hour or two to try to create a simple app from their tutorial to decide for yourself. Cons: less used so less support and smaller community. – assylias May 05 '12 at 19:35
0

well after investigation I found out that there is nothing better than using a custom models. In this blog you can find just a perfect table models.

mr.nothing
  • 5,141
  • 10
  • 53
  • 77
0

(I'm answering this as a full answer and not a comment since there's some code included) Maybe I'm late, but you asked for a little example using BBB so here's a little piece of code that you might find useful:

public static final Property<PatientModel, Long> PATIENT_MODEL_PATIENT_ID_PROPERTY_OBJECT =
        BeanProperty.create(Patient.PATIENT_ID_PROPERTY);
public static final Property<PatientModel, String> PATIENT_MODEL_FIRST_NAME_PROPERTY_OBJECT =
        BeanProperty.create(Patient.FIRST_NAME_PROPERTY);
public static final Property<PatientModel, String> PATIENT_MODEL_MIDDLE_NAME_PROPERTY_OBJECT =
        BeanProperty.create(Patient.MIDDLE_NAME_PROPERTY);
public static final Property<PatientModel, String> PATIENT_MODEL_SURNAME_PROPERTY_OBJECT =
        BeanProperty.create(Patient.SURNAME_PROPERTY);
public static final Property<PatientModel, String> PATIENT_MODEL_SECOND_SURNAME_PROPERTY_OBJECT =
        BeanProperty.create(Patient.SECOND_SURNAME_PROPERTY);
public static final Property<PatientModel, Sex> PATIENT_MODEL_SEX_PROPERTY_OBJECT =
        BeanProperty.create(Patient.SEX_PROPERTY);
public static final Property<PatientModel, String> PATIENT_MODEL_BIRTH_PLACE_PROPERTY_OBJECT =
        BeanProperty.create(Patient.BIRTH_PLACE_PROPERTY);
public static final Property<PatientModel, Calendrical> PATIENT_MODEL_BIRTH_DATE_PROPERTY_OBJECT =
        BeanProperty.create(Patient.BIRTH_DATE_PROPERTY);
public static final Property<PatientModel, Period> PATIENT_MODEL_AGE_PROPERTY_OBJECT =
        BeanProperty.create(Patient.AGE_PROPERTY);

(...)

    private ObservableList<PatientModel> model;
    private JTable selectionTable;

(...)

    JTableBinding<PatientModel, List<PatientModel>, JTable> selectionTableBinding
            = SwingBindings.createJTableBinding(UpdateStrategy.READ_WRITE, this.model, this.selectionTable);
        selectionTableBinding.addColumnBinding(PATIENT_MODEL_PATIENT_ID_PROPERTY_OBJECT);
        selectionTableBinding.addColumnBinding(PATIENT_MODEL_FIRST_NAME_PROPERTY_OBJECT);
        selectionTableBinding.addColumnBinding(PATIENT_MODEL_SURNAME_PROPERTY_OBJECT);
        selectionTableBinding.addColumnBinding(PATIENT_MODEL_SEX_PROPERTY_OBJECT);
        selectionTableBinding.addColumnBinding(PATIENT_MODEL_BIRTH_PLACE_PROPERTY_OBJECT);
        selectionTableBinding.addColumnBinding(PATIENT_MODEL_BIRTH_DATE_PROPERTY_OBJECT);
        selectionTableBinding.addColumnBinding(PATIENT_MODEL_AGE_PROPERTY_OBJECT);

From my experience, learning is proportionally as damn hard as how ridiculously easy becomes implementing it once learned. I know documentation is not great, but having a look at http://www.jarvana.com/jarvana/view/it/tidalwave/betterbeansbinding/betterbeansbinding-swingbinding/1.3.0/betterbeansbinding-swingbinding-1.3.0-javadoc.jar!/org/jdesktop/swingbinding/JTableBinding.html and doing some self teaching examples has saved me from tons of wasted hours. Apart from BBB there's also JGoodies Bindings which I personally haven't tried.