Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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.

 

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:

 

                         

» View all related tutorials
Related Tags: java c ide class graphics constructor object io struct classes size 2d graph const ip vi trac awt int this

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.