UTF-8 to JTable

How to add UTF-8 strings to JTable? Lines in the ASCII range look normal, but accented characters are not displayed properly.

+3
source share
2 answers

I read the lines without asking the reader the UTF-8 encoding. After change

BufferedReader br = new BufferedReader(new InputStreamReader(di));

to

BufferedReader br = new BufferedReader(new InputStreamReader(di, "UTF-8"));

The text appeared correctly.

+1
source

alt text

Above is the line in JTable (screenshot) with the usual accents specially introduced into it. JTable is ordinary without any special modifications to accommodate or accept special characters.

Accent characters, as defined in: http://tlt.its.psu.edu/suggestions/international/accents/codealt.html

Source:

JTable table = new javax.swing.JTable();
DefaultTableModel model = null;

public void initTableModel() {
    model = new DefaultTableModel();
    table.setModel(model);
    model.addColumn("col1");
    model.addColumn("col2");
    ListSelectionModel selectionModel = table.getSelectionModel();
    selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    ListSelectionModel rowSM = table.getSelectionModel();
    table.removeAll();
    table.getColumnModel().getColumn((model.getColumnCount() - 1)).setPreferredWidth(200); 
}

public void initTableData() {
    int numrows = model.getRowCount();
    for (int i = numrows - 1; i >= 0; i--) {
        model.removeRow(i);
    }

    String[] row = new String[3];
    if (pass != null) {
            row[0] = "Lü Dongbin呂洞賓Lán Cǎihé";
            row[1] = "《全唐詩》ó, ò, ñ";
            model.addRow(row);
    }
    validate();
    repaint();
}

All Java source code: http://ahb.me/1exq (more accurate and verified)

Are these the codes you need?

+1
source

Source: https://habr.com/ru/post/1781747/


All Articles