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:

JAVA JTable Row header

The following code will add row header to JTable.

//All copyright reserved by
//No redistribution in any form is allowed without author permission and a back link to
// Example from

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;

class RowHeaderRenderer extends JLabel implements ListCellRenderer {
//customize row header.
RowHeaderRenderer(JTable table) {
JTableHeader header = table.getTableHeader();

public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
setText((value == null) ? "" : value.toString());
return this;

public class RowHeader extends JList{

public RowHeader(String[] headers,JTable table) {

setCellRenderer(new RowHeaderRenderer(table));

//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) {

//create a table
String headers[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
DefaultTableModel dm = new DefaultTableModel(headers.length , 9);
JTable table = new JTable(dm);

//table header
RowHeader rowHeader=new RowHeader (headers,table);
JScrollPane scroll = new JScrollPane(table);

frame.getContentPane().add(scroll, BorderLayout.CENTER);

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
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 © All rights reserved(new). Privacy|Copyright
designed by

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