gpt4 book ai didi

java - 具有 JTable 多行列的自定义渲染器

转载 作者:行者123 更新时间:2023-12-02 01:09:44 25 4
gpt4 key购买 nike

我使用的是通过 NetBeans GUI 编辑器创建的 JTable,我仅更改了列数和名称。

当我使用 SQL 查询填充表时,包含换行符的字符串会显示在一行上。

如何解决?

这是我通过单击表格和 Netbeans 中的“自定义代码”看到的内容:

queryTableStatoPackage = new javax.swing.JTable();

queryTableStatoPackage.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {

},
new String [] {
"Nome Package", "ID Package", "Nome Progetto", "Codice Progetto", "Versione", "Stato", "Nota"
}
));

jScrollPane10.setViewportView(queryTableStatoPackage);

enter image description here

“Nota”为涉及的列

编辑

变化:

我创建了这个类

package db_operation_package;

import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;


public class NewLineSupportRenderer extends DefaultTableCellRenderer {

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
Object val = value;
if (val instanceof String) {
val = convertNewLinesToHTML(val.toString());
}
return super.getTableCellRendererComponent(table, val, isSelected, hasFocus, row, column);
}

private String convertNewLinesToHTML(String s) {
if (s.contains("\n")) {
return "<html>" + s.replace("\n", "<br>") + "</html>";
}
return s;
}

public void updateRowHeight(JTable table, int margin) {
final int rowCount = table.getRowCount();
final int colCount = table.getColumnCount();
for (int i = 0; i < rowCount; i++) {
int maxHeight = 0;
for (int j = 0; j < colCount; j++) {
final TableCellRenderer renderer = table.getCellRenderer(i, j);
maxHeight = Math.max(maxHeight, table.prepareRenderer(renderer, i, j).getPreferredSize().height);
}
table.setRowHeight(i, maxHeight + margin);
}
}
}

这是netbeans为表生成的代码

queryTableStatoClasse = new javax.swing.JTable();

queryTableStatoClasse.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {

},
new String [] {
"Nome Classe", "Codice Classe", "Nome Progetto", "Codice Progetto", "Versione", "Stato", "Nota"
}
));

jScrollPane9.setViewportView(queryTableStatoClasse);

我选择了自定义代码并添加了以下内容:

queryTableStatoClasse = new javax.swing.JTable();

queryTableStatoClasse.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {

},
new String [] {
"Nome Classe", "Codice Classe", "Nome Progetto", "Codice Progetto", "Versione", "Stato", "Nota"
}
));

jScrollPane9.setViewportView(queryTableStatoClasse);

NewLineSupportRenderer customRenderer = new NewLineSupportRenderer();
queryTableStatoClasse.getColumnModel().getColumn(1).setCellRenderer(customRenderer);
customRenderer.updateRowHeight(queryTableStatoClasse, 0);

它似乎不起作用,我修改你的代码时做错了什么?

编辑2

它有效,但发生了这种情况 enter image description here

最佳答案

您应该将带有换行符的文本转换为 Swing 支持的基本 HTML 表示形式。最好的方法:use renderers 。您还需要更新行高,因为表格不会自动更新。示例:

import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

/**
* <code>TableExample</code>.
*/
public class TableExample {

private static final String[][] TABLE_DATA = new String[][] {{"One", "String with one \nnew line"},
{"Two", "String \nwith two \nnew lines"}, {"Three", "String \nwith \nmany \nnew \nlines"}};

private class NewLineSupportRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
Object val = value;
if (val instanceof String) {
val = convertNewLinesToHTML(val.toString());
}
return super.getTableCellRendererComponent(table, val, isSelected, hasFocus, row, column);
}
}

private String convertNewLinesToHTML(String s) {
if (s.contains("\n")) {
return "<html>" + s.replace("\n", "<br>") + "</html>";
}
return s;
}

private void initUI() {
JTable table = new JTable(new DefaultTableModel(TABLE_DATA, new String[] {"Numbers", "Texts"}));
table.getColumnModel().getColumn(1).setCellRenderer(new NewLineSupportRenderer());
updateRowHeight(table, 0);
JFrame frm = new JFrame("Table with new lines");
frm.add(new JScrollPane(table));
frm.pack();
frm.setLocationRelativeTo(null);
frm.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frm.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new TableExample()::initUI);
}

private static void updateRowHeight(JTable table, int margin) {
final int rowCount = table.getRowCount();
final int colCount = table.getColumnCount();
for (int i = 0; i < rowCount; i++) {
int maxHeight = 0;
for (int j = 0; j < colCount; j++) {
final TableCellRenderer renderer = table.getCellRenderer(i, j);
maxHeight = Math.max(maxHeight, table.prepareRenderer(renderer, i, j).getPreferredSize().height);
}
table.setRowHeight(i, maxHeight + margin);
}
}

}

关于java - 具有 JTable 多行列的自定义渲染器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59566122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com