我JTable
附加了JScrollPane
JScrollPane.setRowHeaderView(JTable)
。
看起来像这样:
但我想删除行号表周围的边框(3d效果):
很厚。有任何想法如何删除它?
public class SimpleJTableHeader extends JFrame {
public JTable table;
public SimpleJTableHeader() {
// CREATE A JTABLE
Object rowData[][] = { { "a" }, { "b" }, { "c" }, { "d" } };
Object columnNames[] = { "1" };
table = new JTable(rowData, columnNames);
table.setRowHeight(20);
JScrollPane scrollPane = new JScrollPane(table);
// ADD ROW NUMBERS TO THE TABLE
scrollPane.setRowHeaderView(getHeaderTable());
// SHOW TABLE
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
public JTable getHeaderTable() {
final AbstractTableModel model = new AbstractTableModel() {
private static final long serialVersionUID = 1L;
@Override
public int getColumnCount() {
return 1;
}
@Override
public Object getValueAt(int row, int column) {
return table.convertRowIndexToModel(row);
}
@Override
public int getRowCount() {
return table.getRowCount();
}
};
JTable headerTable = new JTable(model);
headerTable.setPreferredScrollableViewportSize(new Dimension(40, 0));
headerTable.setRowHeight(20);
headerTable.getColumnModel().getColumn(0).setCellRenderer(new TableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable x, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component component = table.getTableHeader().getDefaultRenderer().getTableCellRendererComponent(table, value, false, false, -1, -2);
component.setBackground(new Color(250, 250, 250));
return component;
}
});
return headerTable;
}
public static void main(String args[]) {
new SimpleJTableHeader();
}
}
答案 0 :(得分:3)
您可以将getTableCellRendererComponent()调用返回的组件转换为JComponent,然后根据需要设置边框:
headerTable.getColumnModel().getColumn(0).setCellRenderer(new TableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable x, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
JComponent component = (JComponent)table.getTableHeader().getDefaultRenderer().getTableCellRendererComponent(table, value, false, false, -1, -2);
component.setBackground(new Color(250, 250, 250));
component.setBorder(BorderFactory.createEmptyBorder());
return component;
}
});
无论如何,你可能想看看Rob Camick的RowNumberTable很好的例子。