我有一个grid
( JTable
) 看起来像MS Excel
' 网格。我想允许用户调整行和列的大小。对于我简单地使用的列:
grid.getTableHeader().setResizingAllowed(true);
对于我从这里TableRowResizer
上课的行,我正在使用这样的课程:
new TableRowResizer(grid);
这工作正常。但是,我有一个问题:调整行大小时,行标题也没有调整大小。
这是我制作行标题的方法:
AbstractListModel lm = null;
lm = new TableListModel(grid);
final JList list = new JList(lm);
list.setFixedCellWidth(60);
list.setFixedCellHeight(grid.getRowHeight());
list.setCellRenderer(new TableRowHeaderRenderer(grid));
list.setBackground(grid.getTableHeader().getBackground());
scrollPane.setRowHeaderView(list);
这是TableRowHeaderRenderer
课程:
class TableRowHeaderRenderer extends JLabel implements ListCellRenderer {
private JTable table;
public TableRowHeaderRenderer(JTable table)
{
this.table = table;
JTableHeader header = table.getTableHeader();
setOpaque(true);
setBorder(BorderFactory.createEtchedBorder());
setHorizontalAlignment(CENTER);
setForeground(header.getForeground());
setBackground(header.getBackground());
setFont(header.getFont());
}
public Component getListCellRendererComponent(JList list,
Object value, int index, boolean isSelected, boolean cellHasFocus)
{
Color bg = UIManager.getColor("TableHeader.background");
int selectedrow = table.getSelectedRow();
if (selectedrow==index) bg = new Color(107, 142, 35);
setBackground(bg);
setText("" + Grid.getRowName(index));
return this;
}
}
这是TableListModel
课程:
class TableListModel extends AbstractListModel{
private JTable mytable;
public TableListModel(JTable table) {
super();
mytable = table;
}
public int getSize() {
return mytable.getRowCount();
}
public Object getElementAt(int index) {
return "" + index;
}
}