KidsLovePC Contact Us
  Home   PHP  Graphics ASP  JavaScript   J2EE  HTML  JAVA   Swing   Webtip  Forums  Lyric  Free sheet music  Ringtones
java,jtable,jtree,swing Return to JAVA table list

Best books about JAVA programming:

Icon on JTable row header

The following code will add icon to row header in JTable. It used extended JLabel class to change the row header's look and feel. Or maybe you want to download the 2 images used by the following program.

//All copyright reserved by www.kidslovepc.com
//No redistribution is allowed without author persion and a back link to www.kidslovepc.com
// Example from http://www.kidslovepc.com
//www.kidslovepc.com is not responsible for your data loss or any damage.

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.*;

class RowHeaderRenderer extends JLabel implements ListCellRenderer {
//customize row header.
RowHeaderRenderer(JTable table) {
JTableHeader header = table.getTableHeader();
setOpaque(true);
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
setHorizontalAlignment(CENTER);
setForeground(header.getForeground());
setBackground(header.getBackground());
setFont(header.getFont());
setIcon(null);
}

public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
setText((value == null) ? "" : value.toString());
switch (index)
{
case 1:
setIcon(new ImageIcon("dragonfly.gif"));
break;
case 3:
setIcon(new ImageIcon("dot.gif"));
break;
default:
setIcon(null);
break;
}
return this;
}
}
class MyTableModel extends DefaultTableModel
{
int whichColumn;
public MyTableModel(int rowcount,int columncount,int whichColumn)
{
super(rowcount,columncount);
this.whichColumn =whichColumn;
}
public boolean isCellEditable(int row, int column)
{
if(column==whichColumn) return true;
return false;
}
}
public class IconRow extends JList{

public IconRow(String[] headers,JTable table) {
super(headers);

setFixedCellWidth(50);
setFixedCellHeight(table.getRowHeight());
setCellRenderer(new RowHeaderRenderer(table));
}

//set certain column editable

//Row header example
public static void main(String[] args) {
JFrame frame=new JFrame("Row Header Example");
frame.setSize(300, 150);

frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

//create a table
String headers[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };

MyTableModel dm = new MyTableModel(headers.length , 9,2);
JTable table = new JTable(dm);

//table header
IconRow rowHeader=new IconRow (headers,table);
JScrollPane scroll = new JScrollPane(table);
scroll.setRowHeaderView(rowHeader);
String[] corner={"Order"};

//set corder
scroll.setCorner(JScrollPane.UPPER_LEFT_CORNER ,new RowHeaderAll(corner,table));
frame.getContentPane().add(scroll, BorderLayout.CENTER);
frame.setVisible(true);
}
}

Sponsored link
Java JTabbedPane
JTabbedPane Preview List
Java JComboBox
JComboBox Preview List
Java JTooltip
JTooltip Preview List
Java JList
JList Preview List
Java JTree
JTree Preview List 1
JTree Preview List 2
JAVA JTable
JTable Preview List 1
JTable Preview List 2
JTable Preview List 3
JTable Preview List 4
JTable Preview List 5
JTable Preview List 6
JTable Preview List 7
JAVA Jslide
JSlide Preview List

 

 

Best books about JAVA programming from Amazon
Professional Java, JDK 5 Edition (Paperback Simply Java Programming: An Application-Driven Tutorial Approach (Paperback) Effective Java Programming Language Guide (Paperback) Head First Java, 2nd Edition (Paperback) Java : An Introduction to Problem Solving and Programming (4th Edition)

 

Printer Inkjet A Z lyricsLearn ChineseHome|PHP|Swing|HTML|JavaScript|ASP|JAVA|Forum|WebTip|Contact Us
Copyright © kidslovepc.com All rights reserved(new). Privacy|Copyright
designed by info@kidslovepc.com

Please help us cover the web site maintenance fee.
barcode scanner Thermal Printer credit card machine bar code printer