Why use JavaFX properties?

Forgive my question if this may seem silly, but I'm curious. I am making a Java program that will have a GUI, and I am interested in the whole idea of ​​properties. Why use them when we can just add data to the class? For instance:

class myButton extends Button {

   private boolean booleanProperty = false;

   myButton(args...) {
      // Do something with the property
   }

   public void setProperty(boolean value) {
      this.booleanProperty = value;
   }

   public boolean getProperty() {
      return this.booleanProperty;
   }
}

Everything seems to work just fine for storing additional information about the custom button implementation. But what about:

class myButton extends Button {

   private SimpleBooleanProperty booleanProperty = new SimpleBooleanProperty(false);

   myButton(args...) {
      // Do something with the property
   }

   public void setProperty(boolean value) {
      this.booleanProperty.set(value);
   }

   public boolean getProperty() {
      return this.booleanProperty.get();
   }
}

The only real difference that I see (correct me if I'm wrong) is that you can attach listeners to property values, but I feel that there should be more than just that. Ideas?

+4
source share
5 answers

JavaFX , , .

, , textField :

TextField tf = ...
Node container = ...
container.visibleProperty.bind(tf.textProperty.isNotEmpty());

, tf, , container .

+4

, , UI. :

public class UndoManager {
    BooleanProperty canUndo = ...;
    BooleanProperty canRedo = ...;

    ...
}

3 , /.

MenuButton menuUndo;
Button toolbarUndo;
MenuButton contextMenuUndo;

:

menuUndo.disabledProperty().bind(undoManager.undoProperty()):
toolbarUndo.disabledProperty().bind(undoManager.undoProperty());
contextMenuUndo.disabledProperty().bind(undoManager.undoProperty());

. , , .

+3

, . .

.

, Bindings .

BooleanProperty booleanProperty = new SimpleBooleanProperty();
booleanProperty.addListener(new ChangeListener<Boolean>() {

    @Override
    public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
        System.out.println("property changed form "+oldValue +" to "+newValue);
    }
});
booleanProperty.set(true);
booleanProperty.set(true);
booleanProperty.set(false);
booleanProperty.set(false);
booleanProperty.set(false);
booleanProperty.set(true);
booleanProperty.set(false);
booleanProperty.set(true);
booleanProperty.set(false);

, , . . , / booleanProperty, myButton / ; .

TableView - , . , TableView, TableView TableView, . , , - . .

+3

javafx . , - ,

0

, -. - , , - .

, Concurrent. 1 , Sequentially . , Concurent , Concurent programming Sequence programming. Concurent.

, gui ( ), .

, - , 1 , , . , ( ), ( - ANYBODY), ( - , ), ( -). - , .

, . . , . ( 1 ).

, . , . , , . , - ? . , , . mutli task vs just solo-tasking.

, - , .

0

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


All Articles