Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JavaFx 2.2 can not get I18N (Chinese) text correctly from any InputField when using FXML

 
dp Hu
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A simple JavaFx code, I try to get Chinese character from TextField and send out by email.

When if using pure java code (see OkI18nInput.java), everything is fine.
Then, I rewrote code using FXML (see FailureI18nInput.java and i18ninput.fxml), I got unknown characters ??.

Why and any solution for second case?

I using system.out.println( my text) instead of mail out for easy test.

----------------------OkI18nInput -----------------------------
package com.nusino.rhccc.batchmailer;

import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class OkI18nInput extends Application {
private final String INITIAL_TEXT = "Language \u4E2D\u6587";

private TextField txtI18n;

@Override
public void start(Stage stage) {
stage.setWidth(250);
stage.setHeight(100);
Scene scene = new Scene(new Group());
HBox root = new HBox();
root.setPadding(new Insets(8, 8, 8, 8));
root.setSpacing(5);
root.setAlignment(Pos.BOTTOM_LEFT);
txtI18n = new TextField();
txtI18n.setText(INITIAL_TEXT);
txtI18n.prefHeight(120);
txtI18n.minHeight(30);

Button prtI18n = new Button("Print");
prtI18n.setOnAction(new Print());

root.getChildren().add(txtI18n);
root.getChildren().add(prtI18n);


scene.setRoot(root);
stage.setScene(scene);
stage.show();
}

public class Print implements EventHandler{
@Override
public void handle(Event event) {
// print i18n Chinese properly
System.out.println(txtI18n.getText());
}
}

public static void main(String[] args) {
launch(args);
}


}

------------------------FailureI18nInput --------------------
public class FailureI18nInput extends Application implements Initializable {
private final String INITIAL_TEXT = "Language \u4E2D\u6587";

@FXML
private TextField txtI18n;

@FXML
private Button prtI18n;

@Override
public void initialize(URL arg0, ResourceBundle arg1) {
txtI18n.setText(INITIAL_TEXT);
}

public void print(Event event){
// print i18n out put Chinese as ? ?
System.out.println(txtI18n.getText());

}

@Override
public void start(Stage stage) {
stage.setWidth(300);
stage.setHeight(100);
Scene scene = new Scene(new Group());
URL fxmlUrl = this.getClass().getClassLoader().getResource("com/nusino/rhccc/batchmailer/i18ninput.fxml");
HBox thisTemplate = (HBox) loadNode(fxmlUrl);
scene.setRoot(thisTemplate);
stage.setScene(scene);
stage.show();

}



public static Node loadNode(URL fxmlUrl) {
Node node = null;
try{
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(fxmlUrl);
fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
InputStream in = fxmlUrl.openStream();
node = (Node) fxmlLoader.load(in);
in.close();
}catch(Exception ex){
ex.printStackTrace();
}

return node;
}
public static void main(String[] args) {
launch(args);
}


}

------------------i18ninput.fxml---------------------
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import java.net.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.collections.*?>
<?import javafx.scene.web.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.layout.StackPane?>
<HBox fx:id="mailTemplate"
fx:controller="com.nusino.rhccc.batchmailer.FailureI18nInput"
prefHeight="50.0" prefWidth="250.0"
xmlns:fx="http://javafx.com/fxml">

<TextField fx:id="txtI18n" prefWidth="200" prefHeight="30">
</TextField>
<Button fx:id="prtI18n" text="Print" onAction="#print"/>

</HBox>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic