Example of table headers with icons
/*
* Created on 2005-12-20
*
* Todo to change the template for this generated file go
* Window-preferences-Java-code style-code templates
*/
Import java. AWT. image;
Import java. AWT. rectangle;
Import java. AWT. event. windowadapter;
Import java. AWT. event. javaswevent;
Import java. AWT. image. imageobserver;
Import javax. Swing. imageicon;
Import javax. Swing. jframe;
Import javax. Swing. jlabel;
Import javax. Swing. jscrollpane;
Import javax. Swing. jtable;
Import javax. Swing. Table. defaulttablemodel;
Import javax. Swing. Table. jtableheader;
Import javax. Swing. Table. tablecolumn;
/**
* @ Author Nobuo tamemasa
* @ Version 1.0 06/19/99
*/
Public class animatediconheaderexample extends jframe {
Private Static object [] [] DATA = NULL;
Private Static string [] column = NULL;
Public static object [] [] getdata (){
If (Data = NULL)
Data = new object [] [] {"leopard", "Lycaon "},
{"Jaguar", "Jackal" },{ "Cheetah", "Coyote "},
{"Puma", "Dingo" },{ "Lynx", "Fox" },{ "Tom", "hot "}};
Return data;
}
Public static string [] getcolumn (){
If (column = NULL ){
Column = new string [] {"cat", "dog "};
}
Return column;
}
Public animatediconheaderexample (){
Super ("animatediconheader example ");
Imageicon [] icons = {New imageicon ("images/3-119.gif "),
New imageicon ("images/3-6.gif ")};
Defaulttablemodel model = new defaulttablemodel (){
Public int getcolumncount (){
Return getcolumn (). length;
}
Public int getrowcount (){
Return getdata (). length;
}
Public String getcolumnname (INT col ){
Return getcolumn () [col];
}
Public object getvalueat (INT row, int col ){
Return getdata () [row] [col];
}
};
Jtable table = new jtable (model ){
Public tablecolumn getcolumn (object identifier ){
System. Out. println ("give me a Rander ");
(New mytablecolumn (). creatheaderrenderer (Super
. Getcolumn (identifier ));
Return super. getcolumn (identifier );
}
Class mytablecolumn extends tablecolumn {
Private mytablecolumn (){
Super ();
}
/**
* @ Param Column
*/
Public void creatheaderrenderer (tablecolumn supercolumn ){
If (supercolumn. getheaderrenderer () = NULL ){
Supercolumn. setheaderrenderer (Super
. Createdefaultheaderrenderer ());
System. Out. println ("have a Rander ");
}
}
}
};
Jtableheader header = table. gettableheader ();
Jlabel Renderer;
For (INT I = 0; I <model. getcolumncount (); I ++ ){
Renderer = (jlabel) Table. getcolumn (model. getcolumnname (I ))
. Getheaderrenderer ();
If (Renderer! = NULL)
Renderer. seticon (icons [I]);
Else
System. Out. println ("nullrander ");
// If you have only one column.
// Icons [I]. setimageobserver (header );
Icons [I]. setimageobserver (New headerimageobserver (header, I ));
}
Jscrollpane pane = new jscrollpane (table );
Getcontentpane (). Add (PANE );
}
Class headerimageobserver implements imageobserver {
Jtableheader header;
Int Col;
Headerimageobserver (jtableheader header, int col ){
This. header = header;
This. Col = Col;
}
Public Boolean imageupdate (image IMG, int flags, int X, int y, int W,
Int h ){
If (flags & (framebits | allbits ))! = 0 ){
Rectangle rect = header. getheaderrect (COL );
Header. repaint (rect );
}
Return (flags & (allbits | abort) = 0;
}
}
Public static void main (string [] ARGs ){
Animatediconheaderexample frame = new animatediconheaderexample ();
Frame. addwindowlistener (New windowadapter (){
Public void windowclosing (windowevent e ){
System. Exit (0 );
}
});
Frame. setsize (300,140 );
Frame. setvisible (true );
}
}
Many examples of tame used on the official website are unavailable. Some methods in the bag are also discarded. I sorted it out.