import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Test extends JFrame {
String longTitle = "Last Name / Maiden Name (if divorced)";
MultilineHeaderRenderer multilineRenderer =
new MultilineHeaderRenderer(longTitle);JTable table = new JTable(
new Object[][] {
{ "Lynn", "M.", "Seckinger" },
{ "Carol", "R.", "Seckinger" },
{ "Roy", "D.", "Martin" },
{ "Richard", "A.", "Tattersall" },
{ "Philip", "B.", "Edwards" },
{ "Moore", "T.", "Moore" },// shorten scrollbar grip with these ...{ "Lynn", "M.", "Seckinger" },
{ "Carol", "R.", "Seckinger" },
{ "Roy", "D.", "Martin" },
{ "Richard", "A.", "Tattersall" },
{ "Philip", "B.", "Edwards" },
{ "Moore", "T.", "Moore" },
},
new Object[] {"First Name", "MI", longTitle});public Test() {
TableColumn middleColumn = table.getColumn("MI"),
lastColumn = table.getColumn(longTitle);lastColumn.setHeaderRenderer(multilineRenderer);TableCellRenderer hdrRenderer =
middleColumn.getHeaderRenderer();Component hdrComponent =
hdrRenderer.getTableCellRendererComponent(table,
"MI", false, false, 0, 0);if(hdrComponent instanceof JComponent) {
JComponent c = (JComponent)hdrComponent;
c.setToolTipText("Middle Initial");
}table.getTableHeader().setToolTipText("Table Header!");getContentPane().add(
new JScrollPane(table), BorderLayout.CENTER);
}
public static void main(String args[]) {
GJApp.launch(new Test(),
"Multi-Line Column Headers",300,300,300,250);
}
}
class MultilineHeaderRenderer implements TableCellRenderer {
MultilineHeader mll;
JScrollPane scrollPane;public MultilineHeaderRenderer(String title) {
mll = new MultilineHeader(title);
scrollPane = new JScrollPane(mll);scrollPane.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);scrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_NEVER);scrollPane.setBorder(null);
}
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row, int col) {
mll.setText((String)value);
return scrollPane;
}
}
class MultilineHeader extends JTextArea {
public MultilineHeader(String s) {
super(s);
}
public void updateUI() {
super.updateUI();// turn on wrapping and disable editing and highlightingsetLineWrap(true);
setWrapStyleWord(true);
setHighlighter(null);
setEditable(false);// make the text area look like a table headerLookAndFeel.installColorsAndFont(this,
"TableHeader.background",
"TableHeader.foreground",
"TableHeader.font");LookAndFeel.installBorder(this, "TableHeader.cellBorder");
}
}
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.setText(s);
}
static Object getResource(String key) {
if(resources != null) {
return resources.getString(key);
}
return null;
}
}Exception in thread "main" java.lang.NullPointerException
at Test.<init>(Test.java:42)
at Test.main(Test.java:56)
import javax.swing.border.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Test extends JFrame {
String longTitle = "Last Name / Maiden Name (if divorced)";
MultilineHeaderRenderer multilineRenderer =
new MultilineHeaderRenderer(longTitle);JTable table = new JTable(
new Object[][] {
{ "Lynn", "M.", "Seckinger" },
{ "Carol", "R.", "Seckinger" },
{ "Roy", "D.", "Martin" },
{ "Richard", "A.", "Tattersall" },
{ "Philip", "B.", "Edwards" },
{ "Moore", "T.", "Moore" },// shorten scrollbar grip with these ...{ "Lynn", "M.", "Seckinger" },
{ "Carol", "R.", "Seckinger" },
{ "Roy", "D.", "Martin" },
{ "Richard", "A.", "Tattersall" },
{ "Philip", "B.", "Edwards" },
{ "Moore", "T.", "Moore" },
},
new Object[] {"First Name", "MI", longTitle});public Test() {
TableColumn middleColumn = table.getColumn("MI"),
lastColumn = table.getColumn(longTitle);lastColumn.setHeaderRenderer(multilineRenderer);TableCellRenderer hdrRenderer =
middleColumn.getHeaderRenderer();Component hdrComponent =
hdrRenderer.getTableCellRendererComponent(table,
"MI", false, false, 0, 0);if(hdrComponent instanceof JComponent) {
JComponent c = (JComponent)hdrComponent;
c.setToolTipText("Middle Initial");
}table.getTableHeader().setToolTipText("Table Header!");getContentPane().add(
new JScrollPane(table), BorderLayout.CENTER);
}
public static void main(String args[]) {
GJApp.launch(new Test(),
"Multi-Line Column Headers",300,300,300,250);
}
}
class MultilineHeaderRenderer implements TableCellRenderer {
MultilineHeader mll;
JScrollPane scrollPane;public MultilineHeaderRenderer(String title) {
mll = new MultilineHeader(title);
scrollPane = new JScrollPane(mll);scrollPane.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);scrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_NEVER);scrollPane.setBorder(null);
}
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row, int col) {
mll.setText((String)value);
return scrollPane;
}
}
class MultilineHeader extends JTextArea {
public MultilineHeader(String s) {
super(s);
}
public void updateUI() {
super.updateUI();// turn on wrapping and disable editing and highlightingsetLineWrap(true);
setWrapStyleWord(true);
setHighlighter(null);
setEditable(false);// make the text area look like a table headerLookAndFeel.installColorsAndFont(this,
"TableHeader.background",
"TableHeader.foreground",
"TableHeader.font");LookAndFeel.installBorder(this, "TableHeader.cellBorder");
}
}
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.setText(s);
}
static Object getResource(String key) {
if(resources != null) {
return resources.getString(key);
}
return null;
}
}Exception in thread "main" java.lang.NullPointerException
at Test.<init>(Test.java:42)
at Test.main(Test.java:56)
解决方案 »
- 不能打开数据库<未知的>。应用程序可能无法识别该数据库
- 求助:使用tomcat6时,项目中的目录/admin下的文件无法正常使用,/admin开始的链接404
- servlet 无法找到
- oracle触发器问题,请高手指教。
- 用Dreamweaver如何做翻页和直接跳转啊?急!!!!!!!!!!!!
- 有关 JSP 开发中共同模块的问题
- servlet的简单问题!(急!在线等)
- (100分)谁能帮我解决这个问题阿,连接oracle数据库
- java怎麼生成eps文件(freehand文件)
- 一个访问量不高的网站用哪种语言写比较好?
- 新手,javaweb运行错误
- 计算机专业的用Java做什么样的毕业设计?
或者直接。在42行。 System.out.println() 你觉得可能出现空指针的值。
好好学习,天天向上!下班。see you!
TableColumn middleColumn = table.getColumn("MI"), lastColumn = table
.getColumn(longTitle);
lastColumn.setHeaderRenderer(multilineRenderer);
//此处换成middleColumn.setHeaderRenderer(multilineRenderer);下面的就可以得到了
TableCellRenderer hdrRenderer = middleColumn.getHeaderRenderer();
//是lastColumn set HeaderRenderer了,middleColumn 没有set,那么它得到的HeaderRenderer就是null,所以下面调用它的方法就会是空指针异常,可以换成
Component hdrComponent = hdrRenderer.getTableCellRendererComponent(
table, "MI", false, false, 0, 0);