import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.UIManager.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class JTableRowHeader {
private TableRowSorter<TableModel> sorter;
public JTableRowHeader() {
for (int i = 0; i < table.getRowCount(); i++) {
table.setValueAt(i, i, 0);
}
sorter = new TableRowSorter<TableModel>(table.getModel());
table.setRowSorter(sorter);
private static final long serialVersionUID = 1L;
@Override
public int getColumnCount() {
return 1;
}
@Override
public boolean isCellEditable(int row, int col) {
return false;
}
@Override
public int getRowCount() {
return table.getRowCount();
}
@Override
public Class<?> getColumnClass(int colNum) {
switch (colNum) {
case 0:
default:
return super.getColumnClass(colNum);
}
}
};
headerTable
= new JTable(model
); for (int i = 0; i < table.getRowCount(); i++) {
headerTable.setValueAt("Row " + (i + 1), i, 0);
}
headerTable.setShowGrid(false);
headerTable.
setAutoResizeMode(JTable.
AUTO_RESIZE_OFF); headerTable.
setPreferredScrollableViewportSize(new Dimension(50,
0)); headerTable.getColumnModel().getColumn(0).setPreferredWidth(50);
@Override
public Component getTableCellRendererComponent
(JTable x,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column
) {
boolean selected = table.getSelectionModel().isSelectedIndex(row);
Component component
= table.
getTableHeader().
getDefaultRenderer().
getTableCellRendererComponent(table, value,
false,
false,
-1,
-2); if (selected) {
component.
setFont(component.
getFont().
deriveFont(Font.
BOLD)); component.
setForeground(Color.
red); } else {
component.
setFont(component.
getFont().
deriveFont(Font.
PLAIN)); }
return component;
}
});
table.getRowSorter().addRowSorterListener(new RowSorterListener() {
@Override
public void sorterChanged(RowSorterEvent e) {
model.fireTableDataChanged();
}
});
@Override
model.fireTableRowsUpdated(0, model.getRowCount() - 1);
}
});
scrollPane.setRowHeaderView(headerTable);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
frame.
setDefaultCloseOperation(JFrame.
DISPOSE_ON_CLOSE); frame.add(scrollPane);
private static final long serialVersionUID = 1L;
private RowFilter<TableModel, Object> filter = new RowFilter<TableModel, Object>() {
@Override
public boolean include
(javax.
swing.
RowFilter.
Entry<? extends TableModel,
? extends Object
> entry
) { return ((Number) entry.
getValue(0)).
intValue() % 2 == 0; }
};
@Override
if (sorter.getRowFilter() != null) {
sorter.setRowFilter(null);
} else {
sorter.setRowFilter(filter);
}
}
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
public static void main
(String[] args
) { try {// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
for (LookAndFeelInfo info
: UIManager.
getInstalledLookAndFeels()) { if (info.getName().equals("Nimbus")) {
UIManager.
setLookAndFeel(info.
getClassName()); break;
}
}
//e.printStackTrace();
}
@Override
public void run() {
JTableRowHeader TestTableRowHeader = new JTableRowHeader();
}
});
}
}
:http://stackoverflow.com/questions/8187639/jtable-with-titled-rows-and-columns/8187799#8187799