gpt4 book ai didi

java - 使用 TextAreaRenderer 在 JTable 单元格中居中对齐文本

转载 作者:行者123 更新时间:2023-11-29 05:32:12 24 4
gpt4 key购买 nike

我正在使用 TextAreaRenderer 在我的 JTable 单元格中自动换行文本。但是,我的项目规范要求能够使文本居中(水平和垂直)。我不知道该怎么做。

因为我已经在使用 TextAreaRenderer,它是 extends JTextArea,所以我无法扩展访问 所必需的 DefaultTableCellRenderer setHorizo​​ntalAlignment()setVerticalAlignment() 方法(请参阅此问答:How to center in JTable cell a value?)。我无法实现 DefaultTableCellRenderer,因为它不是接口(interface)。

SSCCE 与我正在使用的 TextAreaRenderer:

import java.awt.Component;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class Test {

public static void main(String args[]) {
JFrame frame = new JFrame();
JTable table = new JTable();
JScrollPane jsp = new JScrollPane(table);
Object[][] data = new Object[2][2];
data[0][0] = 1;
data[0][1] = "Super long text just to illustrate word wrap. Super long text just to illustrate word wrap. Super long text just to illustrate word wrap. Super long text just to illustrate word wrap. Super long text just to illustrate word wrap. Super long text just to illustrate word wrap. Super long text just to illustrate word wrap. Super long text just to illustrate word wrap. ";
data[1][0] = 2;
data[1][1] = "Two Lines / Two Lines";
String[] headers = new String[]{"Digit", "Text"};
table.setModel(new DefaultTableModel(data, headers) {

@Override
public Class<?> getColumnClass(int columnIndex) {
switch(columnIndex){
case 1: return Integer.class;
case 2: return String.class;
default: return Object.class;
}
}
});
table.getColumnModel().getColumn(1).setCellRenderer(new TextAreaRenderer());
frame.add(jsp);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}



static class TextAreaRenderer extends JTextArea implements TableCellRenderer {

private final DefaultTableCellRenderer adaptee = new DefaultTableCellRenderer();
/** map from table to map of rows to map of column heights */
private final Map cellSizes = new HashMap();


public TextAreaRenderer() {
setLineWrap(true);
setWrapStyleWord(true);
}

public Component getTableCellRendererComponent(
JTable table, Object obj, boolean isSelected,
boolean hasFocus, int row, int column) {

// set the colours, etc. using the standard for that platform
adaptee.getTableCellRendererComponent(table, obj,
isSelected, hasFocus, row, column);
setForeground(adaptee.getForeground());
setBackground(adaptee.getBackground());
setBorder(adaptee.getBorder());
setFont(adaptee.getFont());
setText(adaptee.getText());


// This line was very important to get it working with JDK1.4
TableColumnModel columnModel = table.getColumnModel();
setSize(columnModel.getColumn(column).getWidth(), 100000);
int height_wanted = (int) getPreferredSize().getHeight();
addSize(table, row, column, height_wanted);
height_wanted = findTotalMaximumRowSize(table, row);
if (height_wanted != table.getRowHeight(row)) {
table.setRowHeight(row, height_wanted);
}
return this;
}

@SuppressWarnings("unchecked")
private void addSize(JTable table, int row, int column, int height) {
Map rows = (Map) cellSizes.get(table);
if (rows == null) {
cellSizes.put(table, rows = new HashMap());
}
Map rowheights = (Map) rows.get(new Integer(row));
if (rowheights == null) {
rows.put(new Integer(row), rowheights = new HashMap());
}
rowheights.put(new Integer(column), new Integer(height));
}

/**
* Look through all columns and get the renderer. If it is
* also a TextAreaRenderer, we look at the maximum height in
* its hash table for this row.
*/
private int findTotalMaximumRowSize(JTable table, int row) {
int maximum_height = 0;
Enumeration columns = table.getColumnModel().getColumns();
while (columns.hasMoreElements()) {
TableColumn tc = (TableColumn) columns.nextElement();
TableCellRenderer cellRenderer = tc.getCellRenderer();
if (cellRenderer instanceof TextAreaRenderer) {
TextAreaRenderer tar = (TextAreaRenderer) cellRenderer;
maximum_height = Math.max(maximum_height,
tar.findMaximumRowSize(table, row));
}
}
return maximum_height;
}

private int findMaximumRowSize(JTable table, int row) {
Map rows = (Map) cellSizes.get(table);
if (rows == null) {
return 0;
}
Map rowheights = (Map) rows.get(new Integer(row));
if (rowheights == null) {
return 0;
}
int maximum_height = 0;
for (Iterator it = rowheights.entrySet().iterator();
it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
int cellHeight = ((Integer) entry.getValue()).intValue();
maximum_height = Math.max(maximum_height, cellHeight);
}
return maximum_height;
}
}
}

最佳答案

您需要有一个使用 JTextPane 而不是 JTextArea 的呈现器,然后设置文档的样式属性。

即,

static class TextAreaRenderer extends JTextPane implements TableCellRenderer {

private final DefaultTableCellRenderer adaptee = new DefaultTableCellRenderer();
/** map from table to map of rows to map of column heights */
private final Map cellSizes = new HashMap();

public TextAreaRenderer() {
// !! setLineWrap(true);
// setWrapStyleWord(true);

StyledDocument doc = getStyledDocument();
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
doc.setParagraphAttributes(0, doc.getLength(), center, false);
}

有关更多信息,请参阅(并投票)camickr 的回答 here .

关于java - 使用 TextAreaRenderer 在 JTable 单元格中居中对齐文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20721798/

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