Search code examples
javaswingjtextfieldlook-and-feel

Delete inside JTextField does not work with a new BasicLookAndFeel


I was surprise to see that JTextField stopped responding to delete action if you apply a new (Basic)LookAndFeel for the application.

Bellow is the complete code for this:

import javax.swing.*;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.basic.BasicLookAndFeel;
import java.awt.*;
import java.util.logging.Logger;

public class App2 {
     private static final Logger log =  Logger.getLogger(App2.class.getCanonicalName());

     public static void main(String args[]){
         setUI();

         JFrame f = new JFrame();

         JTextField tf = new JTextField();
         f.add(tf);



         f.setSize(300, 300);
         f.setVisible(true);
           f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
     }

  static void setUI(){
       try {
            javax.swing.UIManager.setLookAndFeel(new DummyLookAndFeel());
       } catch ( UnsupportedLookAndFeelException ex) {
             log.warning("Setting UI not ok : " + ex.getMessage());
       }
   }
}

class DummyLookAndFeel extends BasicLookAndFeel {
     @Override
     protected void initComponentDefaults(UIDefaults table) {
         table.put("TextField.background", new ColorUIResource(Color.RED));
     }

     @Override
     public String getName() {
         return "Just a fancy name";
     }

     @Override
     public String getID() {
         return "Something unique";
     }

     @Override
     public String getDescription() {
         return "A very very long description";
     }

     @Override
     public boolean isNativeLookAndFeel() {
         return false;
     }

     @Override
     public boolean isSupportedLookAndFeel() {
         return true;
     }
 }

Any idea of why is happening?


Solution

  • It seems that if I change the initComponentDefaults(...) to

    protected void initComponentDefaults(UIDefaults table) {
      super.initComponentDefaults(table);
    
      Object fim = table.get("TextField.focusInputMap");
    
      Object fieldInputMap = new UIDefaults.LazyInputMap(new Object[] {
                         "ctrl C", DefaultEditorKit.copyAction,
                         "ctrl V", DefaultEditorKit.pasteAction,
                         "ctrl X", DefaultEditorKit.cutAction,
                           "COPY", DefaultEditorKit.copyAction,
                          "PASTE", DefaultEditorKit.pasteAction,
                            "CUT", DefaultEditorKit.cutAction,
                 "control INSERT", DefaultEditorKit.copyAction,
                   "shift INSERT", DefaultEditorKit.pasteAction,
                   "shift DELETE", DefaultEditorKit.cutAction,
                     "shift LEFT", DefaultEditorKit.selectionBackwardAction,
                  "shift KP_LEFT", DefaultEditorKit.selectionBackwardAction,
                    "shift RIGHT", DefaultEditorKit.selectionForwardAction,
                 "shift KP_RIGHT", DefaultEditorKit.selectionForwardAction,
                      "ctrl LEFT", DefaultEditorKit.previousWordAction,
                   "ctrl KP_LEFT", DefaultEditorKit.previousWordAction,
                     "ctrl RIGHT", DefaultEditorKit.nextWordAction,
                  "ctrl KP_RIGHT", DefaultEditorKit.nextWordAction,
                "ctrl shift LEFT", DefaultEditorKit.selectionPreviousWordAction,
             "ctrl shift KP_LEFT", DefaultEditorKit.selectionPreviousWordAction,
               "ctrl shift RIGHT", DefaultEditorKit.selectionNextWordAction,
            "ctrl shift KP_RIGHT", DefaultEditorKit.selectionNextWordAction,
                         "ctrl A", DefaultEditorKit.selectAllAction,
                           "HOME", DefaultEditorKit.beginLineAction,
                            "END", DefaultEditorKit.endLineAction,
                     "shift HOME", DefaultEditorKit.selectionBeginLineAction,
                      "shift END", DefaultEditorKit.selectionEndLineAction,
                     "BACK_SPACE", DefaultEditorKit.deletePrevCharAction,
               "shift BACK_SPACE", DefaultEditorKit.deletePrevCharAction,
                         "ctrl H", DefaultEditorKit.deletePrevCharAction,
                         "DELETE", DefaultEditorKit.deleteNextCharAction,
                    "ctrl DELETE", DefaultEditorKit.deleteNextWordAction,
                "ctrl BACK_SPACE", DefaultEditorKit.deletePrevWordAction,
                          "RIGHT", DefaultEditorKit.forwardAction,
                           "LEFT", DefaultEditorKit.backwardAction,
                       "KP_RIGHT", DefaultEditorKit.forwardAction,
                        "KP_LEFT", DefaultEditorKit.backwardAction,
                          "ENTER", JTextField.notifyAction,
                "ctrl BACK_SLASH", "unselect"/*DefaultEditorKit.unselectAction*/,
                 "control shift O", "toggle-componentOrientation"/*DefaultEditorKit.toggleComponentOrientation*/
      });
    
      table.put("TextField.focusInputMap", fieldInputMap);
    
    
      table.put("TextField.background", new ColorUIResource(Color.RED));
    }
    

    the JTextField seems to be working. It seems that UIDefaults has no entry for "TextField.focusInputMap" (In the example the fim Object is null)