我想在一个按键上实现一个事件
把jTable一列排序
怎么做

解决方案 »

  1.   

    1. EventListener
      public class KeyEventDemo ...  implements KeyListener ... {
        ...//where initialization occurs:
            typingArea = new JTextField(20);
            typingArea.addKeyListener(this);        //Uncomment this if you wish to turn off focus
            //traversal.  The focus subsystem consumes
            //focus traversal keys, such as Tab and Shift Tab.
            //If you uncomment the following line of code, this
            //disables focus traversal and the Tab events 
            //become available to the key event listener.
            //typingArea.setFocusTraversalKeysEnabled(false);
        ...
        /** Handle the key typed event from the text field. */
        public void keyTyped(KeyEvent e) {
            displayInfo(e, "KEY TYPED: ");
        }    /** Handle the key-pressed event from the text field. */
        public void keyPressed(KeyEvent e) {
            displayInfo(e, "KEY PRESSED: ");
        }    /** Handle the key-released event from the text field. */
        public void keyReleased(KeyEvent e) {
            displayInfo(e, "KEY RELEASED: ");
        }
        ...
        private void displayInfo(KeyEvent e, String keyStatus){
            
            //You should only rely on the key char if the event
            //is a key typed event.
            int id = e.getID();
            String keyString;
            if (id == KeyEvent.KEY_TYPED) {
                char c = e.getKeyChar();
                keyString = "key character = '" + c + "'";
            } else {
                int keyCode = e.getKeyCode();
                keyString = "key code = " + keyCode
                        + " ("
                        + KeyEvent.getKeyText(keyCode)
                        + ")";
            }
            
            int modifiersEx = e.getModifiersEx();
            String modString = "extended modifiers = " + modifiersEx;
            String tmpString = KeyEvent.getModifiersExText(modifiersEx);
            if (tmpString.length() > 0) {
                modString += " (" + tmpString + ")";
            } else {
                modString += " (no extended modifiers)";
            }
            
            String actionString = "action key? ";
            if (e.isActionKey()) {
                actionString += "YES";
            } else {
                actionString += "NO";
            }
            
            String locationString = "key location: ";
            int location = e.getKeyLocation();
            if (location == KeyEvent.KEY_LOCATION_STANDARD) {
                locationString += "standard";
            } else if (location == KeyEvent.KEY_LOCATION_LEFT) {
                locationString += "left";
            } else if (location == KeyEvent.KEY_LOCATION_RIGHT) {
                locationString += "right";
            } else if (location == KeyEvent.KEY_LOCATION_NUMPAD) {
                locationString += "numpad";
            } else { // (location == KeyEvent.KEY_LOCATION_UNKNOWN)
                locationString += "unknown";
            }
            
            ...//Display information about the KeyEvent...
        }

    2.代码参考 jdk/src.zip/javax/swing/table/TableRowSorter.java在TableRowSorterModelWrapper中 getStringValueAt函数里面,
    把要比较的多列值连接起来,我是用//连接
      Object o = getValueAt(row, column);//这行后面取其它列的值  if(column == 2)
      o = o.toString() + "//" +getValueAt(row, 3);
    在getComparator函数里面, 如果列数是你要排序的列时, 调用自己的 MY_COMPARABLE_COMPARATOR
    我的是第2列 if(column == 2) return MY_COMPARABLE_COMPARATOR;在开始定义
      private static final Comparator MY_COMPARABLE_COMPARATOR =
      new MyComparableComparator();
    在自己的 MyComparableComparator函数里面 ,把连接起来的字符串拆开, 比较.
      private static class MyComparableComparator implements Comparator {
      @SuppressWarnings("unchecked")
        
      //public int compare(Object o1, Object o2) {
      // return ((Comparable)o1).compareTo(o2);
      //}
        
      public int compare(Object obj1,Object obj2){
      String[] s1 = obj1.toString().split("//");
      String[] s2 = obj2.toString().split("//");
      
    int flag=s1[0].compareTo(s2[0]);
    if(flag==0){
    return 0-s1[1].compareTo(s2[1]);
    }else{   
    return flag;
    }
      }
      }