• Post Reply Bookmark Topic Watch Topic
  • New Topic

Table Header Renderer Issue

 
satya sahu
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am trying a sample program to render one of the column of JTable with a image icon and text, but getting NullPointerException when trying to get the default headerRenderer.
"TableCellRenderer headerRenderer = column.getHeaderRenderer();"

Can somebody correct this problem.
Code is attached below.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.TableColumn;
import javax.swing.table.TableCellRenderer;

public class TableHeaderIconDemo extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JTable jTable1 = new JTable(new Object[][]{
{"Apple", "Rs. 80"},
{"Grape", "Rs. 35"},
{"Orange", "Rs. 25"},
{"Mousambi", "Rs. 30"},
{"Banana", "Rs. 20"},
{"Tangerine", "Rs. 40"},
},
new Object[] { "Item", "Price/Lb."});

//Construct the frame
public TableHeaderIconDemo() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
init();
}
catch(Exception e) {
e.printStackTrace();
}
}

private void init() throws Exception {
TableColumn column = jTable1.getColumn("Price/Lb.");
TableCellRenderer headerRenderer = column.getHeaderRenderer();
if(headerRenderer == null)
System.out.println("headerRenderer is "+headerRenderer);
column.setHeaderRenderer(new TableHeaderIconRenderer(headerRenderer));

}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Table Header with Icon");
contentPane.add(jScrollPane1, BorderLayout.CENTER);
jScrollPane1.getViewport().add(jTable1, null);
}

//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public static void main(String args[]){
TableHeaderIconDemo frame = new TableHeaderIconDemo();
frame.setVisible(true);
}

}


class TableHeaderIconRenderer
implements TableCellRenderer {
TableCellRenderer realRenderer;
JPanel panel;
JLabel iconLabel = new JLabel(new ImageIcon(".\\images\\MixedFruit.jpg"));
public TableHeaderIconRenderer(TableCellRenderer r) {
System.out.println("r = "+r);
realRenderer = r;
iconLabel.setBorder(BorderFactory.createEtchedBorder());
}

public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected,
boolean hasFocus,
int row, int col) {
Component c = realRenderer.getTableCellRendererComponent(table, value,
isSelected, hasFocus,
row, col);
constructHeaderIcon(c);
return panel;
}
private void constructHeaderIcon(Component c){
if(panel == null){
panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(iconLabel, BorderLayout.WEST);
panel.add(c, BorderLayout.CENTER);
}
}

}
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by satya sahu:
Hi, I am trying a sample program to render one of the column of JTable with a image icon and text, but getting NullPointerException when trying to get the default headerRenderer.
"TableCellRenderer headerRenderer = column.getHeaderRenderer();"


That doesn't get the default header renderer. It gets the specific header renderer set for that column, or null if one hasn't been set.

If you want the default header renderer you can do
TableCellRenderer headerRenderer = jTable1.getTableHeader().getDefaultRenderer();

That will get rid of your NullPointerException problem but it won't help you all that much because this code

won't work the way you expect. Unless you have specified a non-default renderer, 'c' will be a Component, but you can't actually use it like a Component and add it to a Container because several of its methods (including validate/invalidate/revalidate) have been overridden to do nothing. So'll you'll have to go with another approach.

If you're willing to lose the etched border around the Icon, I would suggest something simple like this:



[edit: You can also lose the enableEvents/processWindowEvent stuff and simply call setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE).]
[ January 25, 2008: Message edited by: Brian Cole ]
 
satya sahu
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. My problem is solved.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!