gpt4 book ai didi

java - JTable 调用自定义单元格渲染器方法...连续

转载 作者:搜寻专家 更新时间:2023-10-31 08:23:32 25 4
gpt4 key购买 nike

可以在以下位置找到可编译的源代码:http://www.splashcd.com/jtable.tar

我是这门语言的新手,所以我不确定这是否是可以接受的行为。

我创建了一个 JTable 来为收到的每条消息显示一行(它接收大约每 20 秒一个)。其中一个表列可以包含大量文本,所以我创建了一个自定义单元格渲染器,它自动换行并设置行相应的高度。

一切都按预期工作,除了一旦表格显示其第一行,它每秒调用单元格渲染器大约十次......直到用户关闭表。

一旦我在那里得到大约 20 行,表格就会变得相当缓慢,需要 2-8调整列的大小、向上或向下滚动或使用选定的背景颜色。

我在渲染器里面插入了一个打印语句,所以我可以看到有多少次正在调用 getTableCellRendererComponent 方法。

我禁用了工具提示,并禁用了所有单元格编辑。我确实有一个听众添加新行或创建表时,将 View 滚动到最后一行调整大小。

getTableCellRendererComponent 方法是否应该每秒调用几次当我只是查看屏幕(不触摸鼠标或键盘)时?

TIA

最佳答案

  • aaaaach

  • 你需要doLayout(),

  • 下一级 :-),然后您也可以为 JTextComponents 设置 Maximum visible row,不费吹灰之力

enter image description here

doLayout()

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.text.*;
//http://tips4java.wordpress.com/2008/10/26/text-utilities/
public class AutoWrapTest {

public JComponent makeUI() {
String[] columnNames = {" Text Area Cell Renderer "};
Object[][] data = {
{"123456789012345678901234567890"},
{"dddddddddddddddddddddddddddddddddddddddddddddddddddddddddx"},
{"----------------------------------------------0"},
{">>>>>>>>>>>>>dddddddddddddddddddddddddddddddddddddddddddddddddd"
+ "dddddddxdddddddddddddddddddddddddddddddddddddddddddddd"
+ "dddddddddddx>>>>>>>>>>>>>>>>>>>>>>>>>|"},
{">>>>>>>>>>>>ddddddddddddddddddddddddddddddddddddddddddddddddddd"
+ "ddddddx>>>>>>>>>>>>>>>>>>>>>>>>>>|"},
{"a|"},
{">>>>>>>>bbbb>>>>>>>>>>>>>>>>>>>|"},
{">>>>>>>>>>>>>>>>>>|"},
{">>>>>>>>>>>>>dddddddddddddddddddddddddddddddddddddddddddddddddd"
+ "dddddddxdddddddddddddd123456789012345678901234567890dddddd"
+ "dddddddddddddddddddddddddddddddddddddx>>>>>>>>>>>>>>>>>>>>"
+ ">>>>>|"},
{">>>>>>>>>>>>>dddddddddddddd123456789012345678901234567890dddddd"
+ "dddddddddddddddddddddddddddddddddddddxdddddddddddddd123456"
+ "789012345678901234567890dddddddddddddddddddddddddddddddddd"
+ "ddddd123456789012345678901234567890ddddx>>>>>>>>>>>>>>>>>>"
+ ">>>>>>>|"},};
TableModel model = new DefaultTableModel(data, columnNames) {

private static final long serialVersionUID = 1L;

@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
JTable table = new JTable(model) {

private static final long serialVersionUID = 1L;

@Override
public void doLayout() {
TableColumn col = getColumnModel().getColumn(0);
for (int row = 0; row < getRowCount(); row++) {
Component c = prepareRenderer(col.getCellRenderer(), row, 0);
if (c instanceof JTextArea) {
JTextArea a = (JTextArea) c;
int h = getPreferredHeight(a) + getIntercellSpacing().height;
if (getRowHeight(row) != h) {
setRowHeight(row, h);
}
}
}
super.doLayout();
}

private int getPreferredHeight(JTextComponent c) {
Insets insets = c.getInsets();
View view = c.getUI().getRootView(c).getView(0);
int preferredHeight = (int) view.getPreferredSpan(View.Y_AXIS);
return preferredHeight + insets.top + insets.bottom;
}
};
table.setEnabled(false);
table.setShowGrid(false);
table.setTableHeader(null);
table.getColumnModel().getColumn(0).setCellRenderer(new TextAreaCellRenderer());
//table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane sp = new JScrollPane(table);
sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
sp.setPreferredSize(new Dimension(250, 533));
JPanel p = new JPanel(new BorderLayout());
p.add(sp);
return p;
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
createAndShowGUI();
}
});
}

public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new AutoWrapTest().makeUI());
f.setLocation(100, 100);
f.pack();
f.setVisible(true);
}
}

class TextAreaCellRenderer extends JTextArea implements TableCellRenderer {

private static final long serialVersionUID = 1L;
private final Color evenColor = new Color(230, 240, 255);

public TextAreaCellRenderer() {
super();
setLineWrap(true);
setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
}

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
setBackground((row % 2 == 0) ? evenColor : getBackground());
}
setFont(table.getFont());
setText((value == null) ? "" : value.toString());
return this;
}
}

关于java - JTable 调用自定义单元格渲染器方法...连续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12864998/

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