Question

Generally when you double click in a text component, the entire word is selected.

I would like to disable selection of this single word but still maintain the ability to click and drag to select it.

Example: "The quick brown fox jumps over the lazy dog"

When I click and drag from the beginning of "The" to the end of "dog," the text is selected. However, when I double click "brown," "brown" is not selected and a different action can be preformed.

Does anyone know how I can achieve this?

Was it helpful?

Solution

Where edit is JTextComponent instance

DefaultCaret c=new DefaultCaret() {
    public void mouseClicked(MouseEvent e) {
        int nclicks = SwingUtilities2.getAdjustedClickCount(getComponent(), e);
        if (! e.isConsumed() &&
                SwingUtilities.isLeftMouseButton(e) &&
                nclicks == 2
                && SwingUtilities2.canEventAccessSystemClipboard(e)) {
            return;
        }

        super.mouseClicked(e);
    }
    public void mousePressed(MouseEvent e) {
        int nclicks = SwingUtilities2.getAdjustedClickCount(getComponent(), e);
        if (! e.isConsumed() &&
                SwingUtilities.isLeftMouseButton(e) &&
                nclicks == 2
                && SwingUtilities2.canEventAccessSystemClipboard(e)) {
            return;
        }
        super.mousePressed(e);
    }
};
c.setBlinkRate(edit.getCaret().getBlinkRate());
edit.setCaret(c);

OTHER TIPS

I suppose you can register your own MouseListener or MouseAdapter and check the click count (i.e. getClickCount()) of the MouseEvent. If it's equal to 2, swallow the event, otherwise delegate the event handling to the superclass (e.g. super.mouseClicked(...)).

Another way to do that is to replace the selectWord action by an action that does nothing:

    textComponent.getActionMap().put(DefaultEditorKit.selectWordAction, 
            new TextAction(DefaultEditorKit.selectWordAction) {
                public void actionPerformed(ActionEvent e) {
                    // DO NOTHING
                }
            });

You can also use this technic to change the behaviour of the selectWord action if your definition of a word is different than the default Swing one.

you could implement the double-click like this:

setCaretPosition(0);

Documentation: This points to the deprecating method select(int,int) which is a delegate call to setCaretPosition and then moveCaretPosition

@Loic Your answer worked for me. However, I still wanted to have the word selection enabled if a certain condition is fulfilled. Here is how I did it:

Action defaultSelectWordAction = textComponent.getActionMap().get(DefaultEditorKit.selectWordAction);

textComponent.getActionMap().put(DefaultEditorKit.selectWordAction, new TextAction(DefaultEditorKit.selectWordAction) {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (conditionFulfilled()) {
                defaultSelectWordAction.actionPerformed(e);
            } else {
                // DO NOTHING
            }
        }
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top