Home Java Example Java Swing Setting an Icon with Text in a Column Head of JTable
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Setting an Icon with Text in a Column Head of JTable

Advertisement
In this section, you will learn how to set an icon with text in a column head of JTable component.

Setting an Icon with Text in a Column Head of JTable

     

In this section, you will learn how to set an icon with text in a column head of JTable component. But what is icon?

Icon: This is a graphical user interface (GUI). An image or picture available on the screen that helps you to identify file, folder, directory and window etc.

Description of program:

First of all this program creates a JTable containing some data and column with column header. After that you will set the icons in column header. For this you will need an image or icon that have '.gif' extension. Here you will see the SetIcon method applied for setting the icon with text in column header. This method uses the setHeaderRenderer  and setHeaderValue methods that helps you setting the icon and text in column header. After doing entire process, you will get the first and second column header having  icon with text.

Description of code:

setText():
This is the method that is used to set the given text in specified location.

setIcon():
This method helps you to set the given icon on the specified location.

Here is the code of program:

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;

public class IconColumnHeader{
  JTable table;
  JTableHeader header;
  ImageIcon imageIcon;
  public static void main(String[] args) {
  new IconColumnHeader();
  }

  public IconColumnHeader(){
  JFrame frame = new JFrame("An Icon in a 
Column Head of a JTable!"
);
  JPanel panel = new JPanel();
  String data[][] {{"Vinod","Computer","3"},
   {
"Rahul","History","2"},
   {
"Manoj","Biology","4"},
   {
"Sanjay","PSD","5"}};
  String col [] {"Name","Course","Year"};
  DefaultTableModel model = new DefaultTableModel(data,col);
  table = new JTable(model);
  imageIcon = new ImageIcon("bt_home.gif");
  SetIcon(table, 1, imageIcon,"Home");
  imageIcon = new ImageIcon("icon_evil.gif");
  SetIcon(table, 0, imageIcon,"Name");
  header = table.getTableHeader();
  header.setBackground(Color.yellow);
  JScrollPane pane = new JScrollPane(table);
  panel.add(pane);
  frame.add(panel);
  frame.setSize(500,150);
  frame.setUndecorated(true);
  frame.getRootPane().setWindowDecorationStyle
(
JRootPane.PLAIN_DIALOG);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setVisible(true);
  }

  public void SetIcon(JTable table, int col_index, ImageIcon icon,String name){
  table.getTableHeader().getColumnModel().getColumn(col_index).setHeaderRenderer
(
new iconRenderer());
  table.getColumnModel().getColumn(col_index).setHeaderValue(new txtIcon(name, icon));
  }
  
  public class iconRenderer extends DefaultTableCellRenderer{
  public Component getTableCellRendererComponent(JTable table, 
Object obj,boolean isSelected, 
boolean hasFocus, int row, 
int column) {
  txtIcon i = (txtIcon)obj;
  if (obj == i) {
 setIcon(i.imageIcon);
 setText(i.txt);
  }
  setBorder(UIManager.getBorder("TableHeader.cellBorder"));
  setHorizontalAlignment(JLabel.CENTER);
  return this;
  }
  }

  public class txtIcon {
  String txt;
  ImageIcon imageIcon;
  txtIcon(String text, ImageIcon icon) {
  txt = text;
  imageIcon = icon;
  }
  }
}

Download this example.

Output of program:

Before setting an icon:

After setting an icon with text:

 

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 14, 2007

Related Tutorials

Ask Questions?    Discuss: Setting an Icon with Text in a Column Head of JTable  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com