1

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?

David Kaminsky
  • 89
  • 2
  • 12

5 Answers5

2

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(...)).

mre
  • 43,520
  • 33
  • 120
  • 170
2

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);
StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • great, excelent knowledge, you're King_of_JTextComponents, only +1 – mKorbel Feb 29 '12 at 09:03
  • Thank you. After implementing editor like MS Word based on java/swings plenty of similar problems already have solutions. – StanislavL Feb 29 '12 at 09:24
  • I tried implementing this code but SwingUtilities2 could not be found. I am using jre1.6.0_05. I looked online for SwingUtilities2 and most posts suggest not depend on it. Are there any other solutions or workarounds? Thanks. – David Kaminsky Mar 01 '12 at 15:24
  • I just took the code from DefaultCaret source. You can use another way to get click count. – StanislavL Mar 02 '12 at 05:52
1

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.

Loic
  • 11
  • 1
0

@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
            }
        }
    });
DanD
  • 333
  • 3
  • 15
0

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

mindandmedia
  • 6,800
  • 1
  • 24
  • 33