Question

My Goal is : having a text pane where user can paste text only(with OS not a button!!!) and can't type in to the pane

because: can't have submit button (this is a must requirement - I know that it's not logical)

after pasting: the text-pane becomes not editable for user, and the program will change background at specific chars.

after button "clear filed" pressed - the program clears the pane and returns to initial state

My Problem: How to make text pane accept pastes but block typing

so far I made only JPanel with the text pane itself and all the controls will be in different JPanel

public class textPanel extends JPanel{

private JTextPane text;

public textPanel ()
{
    setLayout(new BorderLayout());
    text = new JTextPane();        //12,81
    text.setBorder(BorderFactory.createLoweredBevelBorder());
    add(text,BorderLayout.CENTER);

    StyledDocument doc = text.getStyledDocument();


    setBackground(Color.LIGHT_GRAY);
    setPreferredSize(new Dimension(1000, 210));




}    

thanks for help

Was it helpful?

Solution

Override public void paste() method of your. By default your JTextPane is not editable (setEditable(false)).

The paste() method's source in JTextComponent is

public void paste() {
    if (isEditable() && isEnabled()) {
        invokeAction("paste", TransferHandler.getPasteAction());
    }
}

So you just make it editable, call super.paste(), and set the editable=false back after the super call.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top