neufocusmanager.java

来自「JTable复杂表头的实现代码 JTable复杂表头的实现代码」· Java 代码 · 共 81 行

JAVA
81
字号
package cn.com.table;

import javax.swing.DefaultFocusManager;
import java.awt.Component;
import java.awt.event.KeyEvent;
import javax.swing.JComponent;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2001</p>
 * <p>Company: </p>
 * @author
 * @version 1.0
 */

public class NeuFocusManager extends DefaultFocusManager {
  private STable table;
  public NeuFocusManager(STable table) {
    this.table = table;
  }
  public void processKeyEvent(Component focusedComponent,KeyEvent anEvent) {
//    if (focusedComponent instanceof JTextField) {
//      JTextField jt = (JTextField)focusedComponent;
//      String name = jt.getName();
//      if (name != null && name.indexOf("JTable") >= 0) {
//        anEvent.consume();
//        return;
//      }
//    }
//    super.processKeyEvent(focusedComponent, anEvent);

      if(anEvent.getKeyCode() == KeyEvent.VK_TAB || anEvent.getKeyChar() == '\t') {
          /** If the focused component manages focus, let it do so
           *  if control is not pressed
           */
          if(focusedComponent instanceof JComponent) {
              JComponent fc = (JComponent) focusedComponent;
              if(fc.isManagingFocus()) {
                   int isctrl = (anEvent.getModifiers() &
                                 ActionEvent.CTRL_MASK);
                   if ((isctrl != ActionEvent.CTRL_MASK) ||
                       (anEvent.getKeyCode() == KeyEvent.VK_I))
                      return;
              }
          }

          /** If this is not a key press, consume and return **/
          if(anEvent.getID() != KeyEvent.KEY_PRESSED){
              anEvent.consume();
              return;
          }
          //如果focus在table中
          if (focusedComponent instanceof JTextField) {
            JTextField jt = (JTextField) focusedComponent;
            String name = jt.getName();
            if (name != null && name.indexOf("JTable") >= 0) {
              if((anEvent.getModifiers() & ActionEvent.SHIFT_MASK) == ActionEvent.SHIFT_MASK) {
                int[] rowcolumn = table.findLastCell(table.getSelectedRow(), table.getSelectedColumn(), true);
                table.changeSelection(rowcolumn[0], rowcolumn[1], false, false);
              } else {
                int[] rowcolumn = table.findLastCell(table.getSelectedRow(), table.getSelectedColumn(), false);
                table.changeSelection(rowcolumn[0], rowcolumn[1], false, false);
              }
              anEvent.consume();
              return;
            }
          }

          //System.out.println("\n\n******TAB pressed in processKeyEvent");
          if((anEvent.getModifiers() & ActionEvent.SHIFT_MASK) == ActionEvent.SHIFT_MASK) {
            focusPreviousComponent(focusedComponent);
          } else { focusNextComponent(focusedComponent); }
          anEvent.consume();
      }
  }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?