下面列出了怎么用javax.swing.plaf.TableUI的API类实例代码及写法,或者点击链接到github查看源代码。
static boolean isHover( JTable table, int row, int column ) {
TableUI ui = table.getUI();
if( !(ui instanceof TabTableUI) )
return false;
TabTableUI tabUI = (TabTableUI) ui;
return tabUI.hoverRow == row && tabUI.hoverColumn == column;
}
/**
* Maps {@code JTable.getUI()} through queue
*/
public TableUI getUI() {
return (runMapping(new MapAction<TableUI>("getUI") {
@Override
public TableUI map() {
return ((JTable) getSource()).getUI();
}
}));
}
/**
* Maps {@code JTable.setUI(TableUI)} through queue
*/
public void setUI(final TableUI tableUI) {
runMapping(new MapVoidAction("setUI") {
@Override
public void map() {
((JTable) getSource()).setUI(tableUI);
}
});
}
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
this.setForeground(table.getSelectionForeground());
} else {
this.setForeground(table.getForeground());
}
SubstanceStripingUtils.applyStripedBackground(table, row, this);
this.setSelected(((value != null) && ((Boolean) value).booleanValue()));
this.setEnabled(table.isEnabled());
TableUI tableUI = table.getUI();
if (tableUI instanceof SubstanceTableUI) {
SubstanceTableUI ui = (SubstanceTableUI) tableUI;
// Recompute the focus indication to prevent flicker - JTable
// registers a listener on selection changes and repaints the
// relevant cell before our listener (in TableUI) gets the
// chance to start the fade sequence. The result is that the
// first frame uses full opacity, and the next frame starts the
// fade sequence. So, we use the UI delegate to compute the
// focus indication.
hasFocus = ui.isFocusedCell(row, column);
TableCellId cellFocusId = new TableCellId(row, column);
StateTransitionTracker stateTransitionTracker = ui
.getStateTransitionTracker(cellFocusId);
if (hasFocus || (stateTransitionTracker != null)) {
SubstanceTableCellBorder border = new SubstanceTableCellBorder(
new Insets(0, 0, 0, 0), ui, cellFocusId);
if (stateTransitionTracker != null) {
border.setAlpha(stateTransitionTracker.getFocusStrength(hasFocus));
}
this.setBorder(border);
} else {
this.setBorder(BooleanRenderer.noFocusBorder);
}
} else {
if (hasFocus) {
this.setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
} else {
this.setBorder(BooleanRenderer.noFocusBorder);
}
}
this.setOpaque(false);
return this;
}
@Override
public void setUI(TableUI ui) {
super.setUI(ui);
renderer = null;
sheetCellEditor = null;
}
public void setUI(TableUI ui) {
needCalcRowHeight = true;
inSetUI = true;
super.setUI(ui);
inSetUI = false;
}
@Override
public void setUI( TableUI ui ) {
super.setUI( new TabTableUI() );
}
@Override
public void setUI( TableUI ui ) {
super.setUI( new TaskListTableUI() );
setTableHeader( createDefaultTableHeader() );
}
@Override
public void setUI(TableUI ui) {
// Forbid changing UI
}
public void setUI(TableUI ui) {
super.setUI(ui);
// Customize action and input maps
ActionMap actionMap = getActionMap();
setFocusTraversalKeys(
KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
KeyboardFocusManager.getCurrentKeyboardFocusManager().getDefaultFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));
setFocusTraversalKeys(
KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
KeyboardFocusManager.getCurrentKeyboardFocusManager().getDefaultFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS));
InputMap focusedInputMap = getInputMap(JComponent.WHEN_FOCUSED);
InputMap ancestorInputMap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
actionMap.put("selectPreviousRow", new MySelectNextPreviousRowAction(false));
actionMap.put("selectNextRow", new MySelectNextPreviousRowAction(true));
actionMap.put("startEditing", new MyStartEditingAction());
focusedInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0), "startEditing");
ancestorInputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0));
actionMap.put("smartEnter", new MyEnterAction());
focusedInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "smartEnter");
ancestorInputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
focusedInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel");
ancestorInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel");
actionMap.put("restoreDefault", new MyRestoreDefaultAction());
focusedInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "restoreDefault");
ancestorInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "restoreDefault");
focusedInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0), "restoreDefault");
ancestorInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0), "restoreDefault");
actionMap.put("expandCurrent", new MyExpandCurrentAction(true, false));
focusedInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ADD, 0), "expandCurrent");
ancestorInputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_ADD, 0));
actionMap.put("expandCurrentRight", new MyExpandCurrentAction(true, true));
focusedInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "expandCurrentRight");
ancestorInputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0));
focusedInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_KP_RIGHT, 0), "expandCurrentRight");
ancestorInputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_KP_RIGHT, 0));
actionMap.put("collapseCurrent", new MyExpandCurrentAction(false, false));
focusedInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT, 0), "collapseCurrent");
ancestorInputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT, 0));
actionMap.put("collapseCurrentLeft", new MyExpandCurrentAction(false, true));
focusedInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "collapseCurrentLeft");
ancestorInputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0));
focusedInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_KP_LEFT, 0), "collapseCurrentLeft");
ancestorInputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_KP_LEFT, 0));
}