JTextPane: KeyBindings not working on StyledEditorKit

Please view the following code

import java.awt.Color;    
import java.awt.Dimension;    
import java.awt.FlowLayout;    
import java.awt.event.ActionEvent;    
import java.awt.event.ActionListener;    
import java.awt.event.KeyEvent;
import java.util.ArrayList;    
import java.util.List;    
import java.util.logging.Level;    
import java.util.logging.Logger;    
import javax.swing.*;    
import javax.swing.text.*;    

    public class Form1 extends JFrame      
    {      
        private JTextPane textPane;      
        private JPanel south;    
        private JScrollPane scroll;      

        private String  content;      
        public String documentType;                


        private DefaultStyledDocument document;          
        int start, end, offset1,length1;         
        private JButton button;             
        JFrame frame;    


        public Form1()      
        {      

            //Declaring the instance variables      
            textPane = new JTextPane();      
            textPane.setMinimumSize(new Dimension(100,100));      

            button = new JButton("Bold");      
            button.addActionListener(new StyledEditorKit.BoldAction());     
             button.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_B,KeyEvent.CTRL_MASK),"key");
        button.getActionMap().put("key", new StyledEditorKit.BoldAction());

            document = (DefaultStyledDocument) textPane.getDocument();        




            //Creating the main window     
            south = new JPanel();      
            south.setLayout(new FlowLayout());      
            south.add(button);                          
            scroll = new JScrollPane(textPane);      

            getContentPane().add(scroll,"Center");      
            getContentPane().add(south,"South");                  

            setSize(800,600);    
            validate();    
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                          
        }     


        private class Action extends AbstractAction    
        {      
            public void actionPerformed(ActionEvent ae)      
            {              
                new StyledEditorKit.BoldAction();
            }
        }      

       public static void main(String[] args) throws Exception {       
            SwingUtilities.invokeLater(new Runnable()       
            {        
              @Override        
              public void run() {        
               Form1 f = new Form1();  
               f.setVisible(true);
              }        
            });        
          }       
    }  

Here the user can enter any text, and when he selects the text and presses the "Bold" button, the text will be bold. However, I need to do this using CTRL + B. As you can see, my attempt does not give an answer to this key event. I even tried adding it to a separate class that extends AbstractAction, but still nothing good. How can I implement CTRL + B here? Please, help...

+3
source share
1 answer

, , , InputMap - , ? , ? JComponent.WHEN_FOCUSED , , , .

, , , , , , . JComponent.WHEN_IN_FOCUSED_WINDOW .

.,

InputMap inputMap = myComponent.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
+4

All Articles