Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java GUI 设计 » Swing  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 关于getHeaderRenderer()应用的问题?急急急
sy0300014460





发贴: 11
积分: 0
于 2005-05-06 21:13 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
请各位大侠帮忙看看以下代码,为什么总提示NullPointerException异常,并且columnHeaderWidth(TableColumn col)方法中的renderer总是返回null值,请问各位大侠如何解决,有什么好的方法吗?希望能得到各位大侠的一些宝贵经验,在此我先谢谢大家了!!!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.util.*;
public class Test3 extends JFrame {
Object[] columnNames = {"First Name", "MI", "Last Name"};
Object[][]
names = {{ "Lynn", "我们", "Seckinger" },
{ "Carol", "R.", "Seckinger" },
{ "Roy", "D.", "Martin" },
{ "Bill","O.","Veryveryveryverylonglastnjfdsfjla" },
{ "Richard", "A.", "Tattersall" },
{ "Philip", "B.", "Edwards" },
{ "Moore", "T.", "Moore" },

{ "Lynn", "M.", "Seckinger" },
{ "Carol", "R.", "Seckinger" },
{ "Roy", "D.", "Martin" },
{ "Bill", "O.", "Veryveryveryverylonglastname" },
{ "Richard", "A.", "Tattersall" },
{ "Philip", "B.", "Edwards" },
{ "Moore", "T.", "Moore" }};

JTable table = new JTable(names, columnNames);
public Test3() {
TableColumn mid = table.getColumnModel().getColumn(1);
TableColumn last = table.getColumnModel().getColumn(2);

int midWidth =
getPreferredWidthForColumn(mid),
lastWidth = getPreferredWidthForColumn(last);

table.sizeColumnsToFit(0);
getContentPane().add(new JScrollPane(table),BorderLayout.CENTER);
}
public int getPreferredWidthForColumn(TableColumn col) {
int hw = columnHeaderWidth(col), // hw = header width
cw = widestCellInColumn(col); // cw = column width
return hw > cw ? hw : cw;
}
private int columnHeaderWidth(TableColumn col) {
TableCellRenderer renderer = col.getHeaderRenderer();//?????return null
Component
comp = renderer.getTableCellRendererComponent(
table, col.getHeaderValue(),false, false, 0, 0);
return comp.getPreferredSize().width;
}
private int widestCellInColumn(TableColumn col) {
int c = col.getModelIndex(), width=0, maxw=0;
for(int r=0; r < table.getRowCount(); ++r) {
TableCellRenderer renderer = table.getCellRenderer(r,c);
Component
comp = renderer.getTableCellRendererComponent(
table, table.getValueAt(r,c), false, false, r, c);
width = comp.getPreferredSize().width;
maxw = width > maxw ? width : maxw;
}
return maxw;
}

public static void main(String args[]) {
GJApp.launch(
new Test3(),"Setting Column Widths",100,100,340,140);
}
}

class GJApp extends WindowAdapter {
static private JPanel statusArea = new JPanel();
static private JLabel status = new JLabel(" ");
static private ResourceBundle resources;
public static void launch(
final JFrame f, String title,
final int x, final int y,
final int w, int h) {
launch(f,title,x,y,w,h,null);
}
public static void launch(
final JFrame f, String title,
final int x, final int y,
final int w, int h,
String propertiesFilename) {
f.setTitle(title);
f.setBounds(x,y,w,h);
f.setVisible(true);
statusArea.setBorder(BorderFactory.createEtchedBorder());
statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
statusArea.add(status);
status.setHorizontalAlignment(JLabel.LEFT);
f.setDefaultCloseOperation(
WindowConstants.DISPOSE_ON_CLOSE);
if(propertiesFilename != null) {
resources = ResourceBundle.getBundle(
propertiesFilename, Locale.getDefault());
}
f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
static public JPanel getStatusArea() {
return statusArea;
}
static public void showStatus(String s) {
status.setTextMoon;
}
static Object getResource(String key) {
if(resources != null) {
return resources.getString(key);
}
return null;
}
}




话题树型展开
人气 标题 作者 字数 发贴时间
3111 关于getHeaderRenderer()应用的问题?急急急 sy0300014460 4105 2005-05-06 21:13

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923