我有一个有4个标签的JTabbedPane。每个选项卡都有一个JTable。我正在尝试为JTabbedPane设计一个自定义的FocusTraversalPolicy,这样当我专注于表格的最后一个单元格时,按Tab键就可以转到下一个选项卡。我已经搜索了很多,但还没有找到我可以使用的特定东西。
tabbedPane.setFocusCycleRoot(true);
tabbedPane.setFocusTraversalPolicyProvider(true);
tabbedPane.setFocusTraversalPolicy(new MyPanelFocusTraversalPolicy(tabbedTables));以下是我的自定义焦点遍历策略。
private class MyPanelFocusTraversalPolicy extends FocusTraversalPolicy{
private Vector<Component> order;
public StateWithholdingPanelFocusTraversalPolicy(List<CopyWorkerSingleTablePanel> table){
this.order = new Vector<Component>(table.size());
this.order.addAll(table);
}
@Override
public Component getComponentAfter(Container aContainer, Component aComponent) {
return order.get(order.indexOf(aComponent)+1);
}
@Override
public Component getComponentBefore(Container aContainer, Component aComponent) {
return order.get(order.indexOf(aComponent)-1);
}
@Override
public Component getFirstComponent(Container aContainer) {
return order.get(1);
}
@Override
public Component getLastComponent(Container aContainer) {
return order.lastElement();
}
@Override
public Component getDefaultComponent(Container aContainer) {
return order.get(0);
}
}此外,这些JTables在其他地方被实例化,当我在最后一个单元格上时,我已经覆盖了Tab键绑定,以关注下一个组件。我这样做是为了给我在其他屏幕上的实用工具。
KeyStroke keyStroke = KeyStroke.getKeyStroke("TAB");
Object actionKey = copyWorkerTable.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).get(keyStroke );
final Action action= table.getActionMap().get(actionKey);
Action wrapper = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if(table.getSelectedRow() >= table.getRowCount()-1 && table.getSelectedColumn() >= table.getColumnCount()-1) {
if(!table.isCellEditable(table.getSelectedRow(),table.getSelectedColumn())){
table.transferFocus();
}else{
table.getCellEditor(table.getSelectedRow(),table.getSelectedColumn()).stopCellEditing();
table.transferFocus();
}
}else{
action.actionPerformed(e);
}
}
};发布于 2017-02-10 02:03:46
如果我没弄错的话,为什么不在操作处理程序中使用类似这样的东西来在选项卡之间导航呢?
if (tabbedPane.getSelectedIndex() == (tabbedPane.getComponentCount() - 1))
{
tabbedPane.setSelectedIndex(0);
} else
{
tabbedPane.setSelectedIndex(tabbedPane.getSelectedIndex() + 1);
}https://stackoverflow.com/questions/42140957
复制相似问题