I'm trying to figure out why 开发者_如何学Csome JTables
in a large application have the focus indicator and some don't. To debug this issue, I added code:
UIManager.put("Table.focusCellHighlightBorder",new BorderUIResource(
new LineBorder(new Color(255,0,0)));
And those JTables
with focus indicators changed to red but I still don't see the focus indicator on all JTables
. Any idea why the cells in a JTable
wouldn't show the focus indicator?
You need to set the UI property "before" creating the table.
If you still have a problem then post your SSCCE that demonstrates the problem becuase we can't guess what you are doing.
maybe s/he means (add to your example something ...)
public JavaGUI() {
CustomModel model = new CustomModel();
JTable table = new JTable(model) {
private static final long serialVersionUID = 1L;
private Border outside = new MatteBorder(1, 0, 1, 0, Color.red);
private Border inside = new EmptyBorder(0, 1, 0, 1);
private Border highlight = new CompoundBorder(outside, inside);
@Override
public Component prepareRenderer(
TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
JComponent jc = (JComponent) c;
if (isRowSelected(row)) {
jc.setBackground(Color.orange);
jc.setBorder(highlight);
} else {
jc.setBackground(Color.white);
}
return c;
}
};
for (int i = 1; i <= 16; i++) {
model.addRow(newRow(i));
}
this.add(table);
}
Both JTables used a cell renderer that subclassed DefaultTableCellRenderer and overrode the getTableCellRendererComponent method. The overridden getTableCellRendererComponent method for the JTable that showed the focus indicator, called the super.getTableCellRendererComponent method but the overridden getTableCellRendererComponent method for the JTable that did not show the focus indicators did NOT call the super.getTableCellRendererComponent method.
JTable with focus indicator:
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int col) {
Component comp = super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, col);
....
JTable with no focus indicator:
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected,
boolean hasFocus, int row,
int col) {
for (int i = 0; i < ids.length; i++) {
....
精彩评论