Planet JFX

Ajax-style calls

118pages on
this wiki
Add New Page
Talk1 Share

Summary Edit

Source: OpenJFX Users mailing list.

The following JavaFX code shows how AJAX-like behavior can be performed. The GUI remains responsive while the URL-fetch is performed.

Code Edit

import javafx.ui.*;
import javafx.ui.canvas.*;
import java.lang.StringBuffer;
import java.lang.Math;

// read a text file from an http server;

operation getResourceAsString(urlStr: String) {
    var buf = new StringBuffer();
    do {
        var url = new URL(urlStr);
        var reader = new BufferedReader(new InputStreamReader
        while (true) {
            var line = reader.readLine();
            if (line == null) {
    return buf.toString();

class Model {
    attribute location: String;
    attribute content: String;
    operation update();

operation Model.update() {
    content = "Loading {location}...";
    try {
        content = getResourceAsString(location);
    } catch (e) {
        content = "{e}";


BorderPanel {
    var model = Model {
        location: ""
    top: GroupPanel {
        var row = Row {alignment: BASELINE}
        var col1 = Column {alignment: TRAILING}
        var col2 = Column {resizable: true}
        var col3 = Column {}
        rows: [row]
        columns: [col1, col2, col3]
        [SimpleLabel {
            row: row
            column: col1
            text: "Location: "
        TextField {
            row: row
            column: col2
            columns: 50
            value: bind model.location
            action: operation() {model.update();}
        Button {
            row: row
            column: col3
            text: "Load"
            mnemonic: L
            action: operation() {model.update();}
    center: Label {
        text: bind model.content

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Also on Fandom

Random Wiki