【问题标题】:How to set the encoding in a TableView如何在 TableView 中设置编码
【发布时间】:2016-06-08 16:04:11
【问题描述】:

我的 TableView 不像 ä ö ü 那样显示德语 元音变音 我的控制台向我显示了适合杜塞尔多夫、科隆、慕尼黑等地的字符。

我是否必须在 tableView 中设置字符集或类似内容?

这是一个例子

TableColumn<TextData, String> column;
TableView<TextData> table;
ObservableList<TextData> data = FXCollections.observableArrayList();

...
...
//part of main code
Task<ObservableList<TextData>> task = new Task() {

@Override
protected Object call() throws Exception {  

    IOFileOperations io = new IOFileOperations(fileName); 
    data = io.getData(); 
    colNumSize=io.getNumberOfColumns(); 

    Platform.runLater(new Runnable() {

        @Override
            public void run() {

                for(int i=0;i<colNumSize;i++) {
                    final int x=i; 
                    column = new TableColumn<>("["+x+"]");  
                    //populate the columns with data
                    column.setCellValueFactory(cellData -> cellData.getValue().dataProperty(x));                             
                    table.getColumns().add(column);
                }   
            table.setItems(data);   
            }           
        });
        return null;
    }
};
new Thread(task).start();

//method getData in class IOFileOperations
 public ObservableList<TextData> getData() {
    int numRow=results.length; //<-results a string of Array (String [][] results)
    int numCol=results[0].length;
    for(int i=0;i<numRow;i++) list.add(new TextData(i,numCol, results));   
    return list;
}

//class TextData
public class TextData {

    public StringProperty [] dataValue; 

    public TextData(int row, int numCol, String loadedText [][]) {
        this.dataValue = new StringProperty[numCol];
        for(int i=0;i<numCol;i++) dataValue[i] = new SimpleStringProperty(loadedText[row][i]);
    }
}  

【问题讨论】:

  • 通过在集合中定义这些名称对我有用。你是如何填充你的 TableView 的?
  • 对我也很好。请edit您的问题包括minimal reproducible example
  • @James_D 好的,我已经添加了一个示例
  • @Peter 请看一下我的代码

标签: java javafx utf-8 character-encoding tableview


【解决方案1】:

看起来问题在于您在此类 IOFileOperations 中读取数据的方式

如果不包含这些内容,我们将无法帮助您找出实施中的确切问题。
下面我提供了一个重新创建问题的示例,包括如何使用以下文本文件更正它:

杜塞尔多夫
科隆
慕尼黑

这应该可以帮助您调试和更正自己的实现


public class City {
    private int id;
    private String name;

    public City(int id, String name){
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}

读入数据:
请注意,在这一步我们可以提供我们想要使用的字符集。使用 UTF-8 时,一切都会按预期显示,但是如果您在注释行之间切换,将无法识别变音符号

ObservableList<City> data = FXCollections.observableArrayList();
File file = new File("test.txt");
//Charset charset = StandardCharsets.US_ASCII;
Charset charset = StandardCharsets.UTF_8;
int currentId = 0;

try(BufferedReader reader = new BufferedReader(
        new InputStreamReader(new FileInputStream(file), charset))) {
    String line = reader.readLine();

    while (line != null) {
        data.add(new City(currentId, line));
        line = reader.readLine();
        currentId++;
    }
}

并设置 TableView:

TableView<City> tableView = new TableView();
TableColumn idColumn = new TableColumn("Id");
idColumn.setCellValueFactory(
        new PropertyValueFactory<>("id"));
TableColumn cityColumn = new TableColumn("City");
cityColumn.setCellValueFactory(
        new PropertyValueFactory<>("name"));

 tableView.getColumns().setAll(idColumn, cityColumn);
 tableView.getItems().addAll(data);


字符集的区别:

【讨论】:

  • 我得到了正确的变音符号 charset = StandardCharsets.UTF_8;
猜你喜欢
  • 2016-02-04
  • 1970-01-01
  • 2010-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-27
  • 2011-08-29
相关资源
最近更新 更多