Activity stopping after mouse is clicked on JTextPane

43 Views Asked by At

After running the project if I click on the JTextPane the actual activity which I have written to perform on the text pane is stopped or terminated. How can I overcome this problem even after I have clicked on the text pane with the cursor?

public class textpanetest extends DefaultCaret implements ActionListener,KeyListener {

private static final long serialVersionUID = 1L;
private JFrame frame;
private static JTextPane textPane;
private static SimpleAttributeSet attributeSet,normalattributeSet,center;
private static String s="This is my country.";
static int i=0;
private static StyledDocument doc;
/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                textpanetest window = new textpanetest();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public textpanetest() {
    initialize();
}
public textpanetest(JTextComponent textComponent)
{
    setBlinkRate( textComponent.getCaret().getBlinkRate() );
    textComponent.setHighlighter( null );
}

@Override
public int getMark()
{
    return getDot();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setFocusable(true);
    frame.requestFocusInWindow();
    frame.addKeyListener(this);
    frame.setBounds(100, 100, 998, 650);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
    attributeSet = new SimpleAttributeSet();
    textPane = new JTextPane();
    textPane.setCaret( new textpanetest( textPane ) );
    textPane.setEditable(false);
    textPane.setBackground(Color.BLACK);
    attributeSet=new SimpleAttributeSet();
    center=new SimpleAttributeSet();
    normalattributeSet=new SimpleAttributeSet();
    doc=textPane.getStyledDocument();
    
    StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
    StyleConstants.setItalic(attributeSet, true);  
    StyleConstants.setForeground(attributeSet, Color.red);  
    StyleConstants.setItalic(normalattributeSet, true);  
    StyleConstants.setBold(attributeSet, true);
    StyleConstants.setForeground(normalattributeSet, Color.green);
    doc.setParagraphAttributes(0, doc.getLength(), center, false);
    //StyleConstants.setBackground(attributeSet, Color.blue);
    try {
        
        doc.insertString(doc.getLength(), s.substring(i,i+1), attributeSet);
        doc.insertString(doc.getLength(), s.substring(i+1,s.length()), normalattributeSet);
        
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
      
    textPane.setBounds(334, 90, 215, 189);
    frame.getContentPane().add(textPane);
}

@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub
    
}

@Override
public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
    try {
        textPane.setText("");
        i++;
        doc=textPane.getStyledDocument();
        doc.insertString(doc.getLength(), s.substring(i,i+1), attributeSet);
        doc.insertString(doc.getLength(), s.substring(i+1,s.length()), normalattributeSet);
        
        
    } catch (Exception exception) {
        // TODO Auto-generated catch block
        //exception.printStackTrace();
    }
    
}

@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub
    
}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    
}
}
0

There are 0 best solutions below