Wikia

Planet JFX

Ajax-style calls

118pages on
this wiki
Talk1

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.io.*;
import java.net.URL;
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
(url.openStream()));
        while (true) {
            var line = reader.readLine();
            if (line == null) {
                break;
            }
            buf.append(line);
            buf.append("\n");
        }
    }
    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: "http://www.gnu.org/licenses/gpl.txt"
    }
    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]
        content:
        [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
    }
}

Around Wikia's network

Random Wiki