Thursday, April 8, 2010

JTable Header

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListCellRenderer;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/**
*
* @author Monirul
*/
public class JTableRowHeaders1 extends JFrame {

private JTable table;
private JPanel panel;
private JScrollPane scroll;
private JList rowHeaders;

public JTableRowHeaders1() {

initializeInventory();
}

private void initializeInventory() {
panel = new JPanel();
panel.setLayout(new GridLayout(1,0));
final String[] columnNames = {"Name", "Surname", "Age"};
final Object[][] data = {{"Jhon", "Java", "23"}, {"Stupid", "Stupido", "500"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Max", "Dumbass", "10"}, {"Melanie", "Martin", "500"},
{"Jollibe", "Mcdonalds", "15"}};

table = new JTable(data, columnNames);
Integer[] rowData = new Integer[data.length];
for (int x = 0; x <= rowData.length - 1; x++) {
rowData[x] = x + 1;
}

rowHeaders = new JList (rowData);
rowHeaders.setFixedCellWidth(50);
rowHeaders.setCellRenderer(new RowHeaderRenderer(table));
scroll = new JScrollPane(table);
scroll.setBounds(0, 100, 900, 500);
scroll.setRowHeaderView(rowHeaders);
panel.add(scroll);
getContentPane().add(panel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(900, 700);
setLocationRelativeTo(null);
setVisible(true);
}

// class use for rendering the properties of the row headers
private class RowHeaderRenderer extends JLabel implements ListCellRenderer {
RowHeaderRenderer(JTable table) {
setOpaque(true);
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
setHorizontalAlignment(CENTER);
setForeground(new Color(100, 100, 100));
setFont(new Font("Monospaced", Font.BOLD, 11));
}
public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
setText((value == null) ? "" : value.toString());
return this;
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JTableRowHeaders1();
}
});
}
}

No comments:

Post a Comment