jTable 的问题

hot_baby 2007-12-06 09:53:16
刚用Jswing 有个问题:
当Jtable中的一个单元中的内容比较多的时候,此内容会显示不全,其余的内容用...来代替
怎样做,才能让其显示完全呢?
Excel中也有这种情况,它通过“最合适列宽 和最合适行高”也把所有的内容显示出来
...全文
242 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zg6002 2008-01-28
  • 打赏
  • 举报
回复
public class MultiLineCellRenderer extends JTextArea implements
TableCellRenderer {
public MultiLineCellRenderer() {
setEditable(false);
setLineWrap(true);
setWrapStyleWord(true);
this.setBackground(new Color(164, 227, 243));
this.setBorder(null);
}

public Component getTableCellRendererComponent(JTable tb, Object value,
boolean isSelected,
boolean hasFocus, int row,
int column) {
if (value == null) {

setText("");
}
else {
setText(value.toString());
}
if (isSelected) {
this.setBackground(new Color(181,219,255));
}
else {
this.setBackground(Color.white);
}
return this;
}

}
//////////////////////////
MultiLineCellRenderer mr = new MultiLineCellRenderer();
mYtable.getColumnModel().getColumn(i).setCellRenderer(mr);
huzhenqi2008 2008-01-21
  • 打赏
  • 举报
回复
可以使用JScollPane(滚动条)
IamHades 2008-01-17
  • 打赏
  • 举报
回复
6楼的方法比较接近了,我们项目因为也有这个内容自适应的需求,所以我就自己写了个这样的table,会根据内容变化以及窗体大小改变自动变化表格的长度,前面的都是以内容大小为准,最后一列则比较特殊,如果内容小于放table的容器时则抵拢容器边缘,否则以内容为准.
BatiTan 2008-01-16
  • 打赏
  • 举报
回复
水平滚动条我已经解决了
就是将表格设置为 setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
BatiTan 2008-01-04
  • 打赏
  • 举报
回复
我现在也遇到了问题呢
列宽是通过hfbupt的方法实现了
可是在表格宽度上还是没有实现全部显示表格内容
也就是表格没有出现水平滚动条
不知道这个问题怎么解决啊?
hfbupt 2008-01-02
  • 打赏
  • 举报
回复
public static void adjustColumnPreferredWidths(JTable table) {

TableColumnModel columnModel = table.getColumnModel();
for (int col = 0; col < table.getColumnCount(); col++) {
int maxwidth = 0;
for (int row = 0; row < table.getRowCount(); row++) {
TableCellRenderer rend = table.getCellRenderer(row, col);
Object value = table.getValueAt(row, col);
Component comp =
rend.getTableCellRendererComponent(table,
value,
false,
false,
row,
col);
maxwidth = Math.max(comp.getPreferredSize().width,
maxwidth);

}

TableColumn column = columnModel.getColumn(col);
TableCellRenderer headerRenderer = column.getHeaderRenderer();
if (headerRenderer == null) {
headerRenderer = table.getTableHeader().getDefaultRenderer();
}
Object headerValue = column.getHeaderValue();
Component headerComp =
headerRenderer.getTableCellRendererComponent(table,
headerValue,
false,
false,
0,
col);
maxwidth = Math.max(maxwidth,
headerComp.getPreferredSize().width);
column.setPreferredWidth(maxwidth);
}
}
hengxxh 2007-12-14
  • 打赏
  • 举报
回复
重写 table 渲染器
guoyueshan 2007-12-07
  • 打赏
  • 举报
回复
要自己实现TableCellRenderer
superman0920 2007-12-06
  • 打赏
  • 举报
回复
http://zhidao.baidu.com/question/32334688.html
不知是不是你想要的,希望对你有些帮助
hot_baby 2007-12-06
  • 打赏
  • 举报
回复
可以自动设置行高么
hot_baby 2007-12-06
  • 打赏
  • 举报
回复
自己顶一下
这样能实现么 要是理论上不能实现 我就不费劲了

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧