Creates row header for JTable with row number (starting with 1) displayed - Java Swing

Java examples for Swing:JTable Row

Description

Creates row header for JTable with row number (starting with 1) displayed

Demo Code


import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.Rectangle;
import javax.swing.AbstractListModel;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.ListCellRenderer;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.table.JTableHeader;

public class Main{
    /**//from   w  w w.  jav a2  s  . co m
     * * Creates row header for table with row number (starting with 1)
     * displayed
     */
    public static void removeRowHeader(JTable table) {
        Container p = table.getParent();
        if (p instanceof JViewport) {
            Container gp = p.getParent();
            if (gp instanceof JScrollPane) {
                JScrollPane scrollPane = (JScrollPane) gp;
                scrollPane.setRowHeader(null);
            }
        }
    }
    /**
     * * Creates row header for table with row number (starting with 1)
     * displayed
     */
    public static void setRowHeader(JTable table) {
        Container p = table.getParent();
        if (p instanceof JViewport) {
            Container gp = p.getParent();
            if (gp instanceof JScrollPane) {
                JScrollPane scrollPane = (JScrollPane) gp;
                scrollPane.setRowHeaderView(new TableRowHeader(table));
            }
        }
    }
}

Related Tutorials