Created
June 10, 2015 20:16
-
-
Save jspdown/608d8d89e49fd786e917 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CONTACT.JAVA | |
import javafx.beans.property.SimpleStringProperty; | |
import javafx.beans.property.StringProperty; | |
public class Contact { | |
private StringProperty name = new SimpleStringProperty(); | |
private StringProperty contactNumber = new SimpleStringProperty(); | |
public Contact(String name, String contactNumber) { | |
this.name.set(name); | |
this.contactNumber.set(contactNumber); | |
} | |
@Override | |
public String toString() { return String.format("(%s, %s)", name.getValue(), contactNumber.getValue()); } | |
public String getName() { return name.get(); } | |
public StringProperty nameProperty() { return name; } | |
public String getContactNumber() { return contactNumber.get(); } | |
public StringProperty contactNumberProperty() { return contactNumber; } | |
} | |
// CONTROLLER.JAVA | |
import javafx.beans.property.IntegerProperty; | |
import javafx.beans.property.Property; | |
import javafx.beans.property.SimpleIntegerProperty; | |
import javafx.beans.property.SimpleObjectProperty; | |
import javafx.scene.Scene; | |
import javafx.scene.control.*; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.TextField; | |
import java.util.ArrayList; | |
public class Controller { | |
private ArrayList<Contact> contacts = new ArrayList<Contact>(); | |
private IntegerProperty currentIndex = new SimpleIntegerProperty(-1); | |
private IntegerProperty currentSize = new SimpleIntegerProperty(0); | |
private Property<Contact> currentContact = new SimpleObjectProperty<Contact>(); | |
public int getCurrentIndex() { return currentIndex.get(); } | |
public IntegerProperty currentIndexProperty() { return currentIndex; } | |
public Contact getCurrentContact() { return currentContact.getValue(); } | |
public Property<Contact> currentContactProperty() { return currentContact; } | |
public void first() { | |
if (contacts.size() > 0) currentIndex.set(0); | |
} | |
public void last() { | |
if (contacts.size() > 0) currentIndex.set(contacts.size() - 1); | |
} | |
public void next() { | |
if (currentIndex.get() < (contacts.size() - 1)) currentIndex.set(currentIndex.get() + 1); | |
} | |
public void previous() { | |
if (currentIndex.get() > 0) currentIndex.set(currentIndex.get() - 1); | |
} | |
public void newContact() { | |
Contact contact = new Contact("?", "?"); | |
contacts.add(contact); | |
currentSize.set(contacts.size()); | |
last(); | |
} | |
private void setupContacts() { | |
contacts.add(new Contact("Billy Bob", "081-556-1234")); | |
contacts.add(new Contact("Jane Doe", "082-689-2546")); | |
contacts.add(new Contact("John Doe", "084-253-1254")); | |
} | |
private void setupProperties() { | |
currentIndex.addListener((observable, oldValue, newValue) -> { | |
if ((newValue.intValue() >= 0) && (newValue.intValue() < contacts.size())) { | |
System.out.printf("Current index changed, old = %s, new = %s\n", oldValue.intValue(), newValue.intValue()); | |
currentContact.setValue(contacts.get(newValue.intValue())); | |
} | |
}); | |
currentContact.addListener((observable, oldValue, newValue) -> { | |
System.out.printf("Contact changed, old = %s, new = %s\n", oldValue, newValue); | |
rebindFields(oldValue, newValue); | |
}); | |
} | |
public Controller() { | |
setupContacts(); | |
setupProperties(); | |
} | |
private TextField txtName; | |
private TextField txtContactNumber; | |
private Label lblInfo; | |
private Button btnPrev; | |
private Button btnNew; | |
private Button btnNext; | |
private void rebindFields(Contact oldContact, Contact newContact) { | |
if (oldContact != null) { | |
txtName.textProperty().unbindBidirectional(oldContact.nameProperty()); | |
txtContactNumber.textProperty().unbindBidirectional(oldContact.contactNumberProperty()); | |
} | |
if (newContact != null) { | |
txtName.textProperty().bindBidirectional(newContact.nameProperty()); | |
txtContactNumber.textProperty().bindBidirectional(newContact.contactNumberProperty()); | |
} | |
} | |
public void connectToUI(Scene scene) { | |
txtName = (TextField) scene.lookup("#name"); | |
txtContactNumber = (TextField) scene.lookup("#number"); | |
lblInfo = (Label) scene.lookup("#info"); | |
btnPrev = (Button) scene.lookup("#prev"); | |
btnNew = (Button) scene.lookup("#new"); | |
btnNext = (Button) scene.lookup("#next"); | |
lblInfo.textProperty().bind(currentIndex.add(1).asString().concat(" of ").concat(currentSize)); | |
// Fluent binding | |
currentSize.set(contacts.size()); | |
btnPrev.setOnAction(event -> previous()); | |
btnNew.setOnAction(event -> newContact()); | |
btnNext.setOnAction(event -> next()); | |
} | |
} | |
// MAIN | |
import javafx.application.Application; | |
import javafx.geometry.Insets; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.TextField; | |
import javafx.scene.layout.HBox; | |
import javafx.scene.layout.VBox; | |
import javafx.stage.Stage; | |
public class Example extends Application { | |
public static void main(String[] args) { | |
launch(args); | |
} | |
protected Scene createScene() { | |
VBox root = new VBox(); | |
root.setSpacing(10); | |
root.setOpaqueInsets(new Insets(10, 10, 10, 10)); | |
root.setFillWidth(true); | |
Label lblName = new Label("Name"); | |
TextField txtName = new TextField(); | |
txtName.setId("name"); | |
Label lblNumber = new Label("Contact Number"); | |
TextField txtNumber = new TextField(); | |
txtNumber.setId("number"); | |
Label lblInfo = new Label("X of Y"); | |
lblInfo.setId("info"); | |
Button btnPrev = new Button("<"); | |
btnPrev.setId("prev"); | |
Button btnNew = new Button("+"); | |
btnNew.setId("new"); | |
Button btnNext = new Button(">"); | |
btnNext.setId("next"); | |
HBox buttons = new HBox(); | |
buttons.getChildren().addAll(btnPrev, btnNew, btnNext); | |
buttons.setSpacing(5); | |
buttons.setFillHeight(true); | |
root.getChildren().addAll(lblName, txtName, lblNumber, txtNumber, lblInfo, buttons); | |
return new Scene(root); | |
} | |
@Override | |
public void start(Stage primaryStage) throws Exception { | |
Scene scene = createScene(); | |
Controller controller = new Controller(); | |
controller.connectToUI(scene); | |
controller.first(); | |
primaryStage.setTitle("Example 4"); | |
primaryStage.setWidth(300); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment