加载中…
个人资料
jdmaverick
jdmaverick
  • 博客等级:
  • 博客积分:0
  • 博客访问:490
  • 关注人气:0
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

How to interact with the background using JavaFX

(2009-06-23 20:42:12)
标签:

javafx

杂谈

I am a college student in China, now I am engaged in developing a multimedia system for remote education. I am responsible for rich internet application development. And I will share some experience about how to use JavaFX to interact with background.

 

In order to demonstrate how to interact with background using get and post, I written a servelet as follows

How <wbr>to <wbr>interact <wbr>with <wbr>the <wbr>background <wbr>using <wbr>JavaFX

 

then we begin to write JavaFX files,fisrtly, I use get method. It is very simple because only one method needed to be written.

JavaFX code is as follows:

 


package javafxapplication1;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.Font;

 

import javafx.io.http.*;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.ext.swing.SwingButton;
import java.io.DataInputStream;
import javafx.scene.layout.HBox;
import javafx.ext.swing.SwingTextField;


def label:SwingTextField = SwingTextField {
    columns: 12
    text: "JavaFX"
    editable: true
}

function sendHttp(){
        HttpRequest {
        location:bind "http://localhost:8080/JavaScriptWeb/test?name={label.text}";

        onInput: function(is: java.io.InputStream) {
            try {
                def data:DataInputStream = new DataInputStream(is);
                label.text = data.readLine();
            } finally {
                is.close();
            }
        }
    }.enqueue();
}

 


Stage {
    title : "HttpTest"
    scene: Scene {
        width: 250
        height: 200
        content: [HBox{
            content:[label,
            SwingButton {
            text: "Click Me"
            action: function() {
                sendHttp();
            }
        }
        ]
        }
         ]
    }
}

 

The GUI is kind of like AJAX,

How <wbr>to <wbr>interact <wbr>with <wbr>the <wbr>background <wbr>using <wbr>JavaFX

 

There are two points worth our attention.

The first one is that we use ‘bind’ to associate location and the text field of lable, which enable location to keep the same with the TextField.

The second one is that we define to a method which creates a HttpRequest instance and transmits required data.

 

Now we talk about the post method, which is a little tricky. I sepnt a lot of time to solve this problem. The code is illustrated as follows

 

JavaFX Code

package javafxapplication2;

import javafx.io.http.*;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.ext.swing.SwingButton;
import java.io.DataInputStream;
import javafx.scene.layout.HBox;
import javafx.ext.swing.SwingTextField;


def label:SwingTextField = SwingTextField {
    columns: 10
    text: "IJavaFX"
    editable: true
}

var t:String= bind "name={label.text}";

function sendHttp(){
    HttpRequest {

        method:HttpRequest.POST;
        location:"http://localhost:8080/JavaScriptWeb/test";

       onOutput: function(os: java.io.OutputStream) {
            try {
                os.write(t.getBytes());
                os.flush();
            } finally {
                os.close();
            }
        }

        onInput: function(is: java.io.InputStream) {
            try {
                def data:DataInputStream = new DataInputStream(is);
                label.text = data.readLine();
            } finally {
                is.close();
            }
        }
    }.enqueue();
}

Stage {
    title : "Http"
    scene: Scene {
        width: 250
        height: 200
        content: [HBox{
            content:[label,
            SwingButton {
            text: "Click Me"
            action: function() {
                sendHttp();
            }
        }
        ]
        }
         ]
    }

 

Though it seems simple, the post methond really cost me a lot of time to write and finally i found where the problem lies.In API samples, there are two headers, when we use post method,

setHeader("Content-Type", "somecontent/type") should be deleted or change it to setHeader("Content-Type", "application/x-www-form-urlencoded"). if we do not make changes mentioned above, the background getParameter method cannot get the right value.

There are are three more changes to be made.

1. location needs no arguments

2. method:HttpRequest.POST

3. add an output method.

 

In sum, using JavaFX to interact with the background seems simple. We also find some points that need to be improved. In handling with request and response like AJAX, JavaFX is not very easy to use. But i found that JavaFX supports XML and Json, which may makes this problem easier.

0

阅读 评论 收藏 转载 喜欢 打印举报/Report
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有