Hi all,

i am not an experienced Java GUI programmer and its the first time i'm asking for help in programming on a forum.

I have this simple GUI which allows people to mark text with different categories. I use JTextPane and DefaultStyledDocument. Basically, i took most of the code from this Java tutorial.

The input plain text is delimited with say '|'. E.g., instead of "I like apples but not cherries or melons", it is "| I like apples | but not cherries | or melons. |"

Now, when the user works with the text, he has to select a "chunk" (a text between any two delimiters and then mark it for a category from a pop-up menu. The chunk selection automatically for a mouse click event - the user only needs to click anywhere within the chunk to select the whole text inside it. Here is the code:

public void mouseClicked(MouseEvent e) {

	if (e.getButton() == MouseEvent.BUTTON1) {

		int caret = textPane.getCaretPosition();
				ChunkBordersFinder.findBefore(caret, textPane.getText()), 
				ChunkBordersFinder.findAfter(caret, textPane.getText()));

Now, my problems is as follows:

what if the user wants to mark several consecutive chunks in one go?

So far, if they press the left mouse key, hold it and drag it, the normal text selection mode takes care of text selection, where the smallest unit is one character, while what i need is one "chunk". How do i implement/overwrite this?

Please let me know if i should give any further details.
Thank you in advance,

7 Years
Discussion Span
Last Post by red_lynx

ok guys, turns out it was trivial, i just needed to think about it a little longer :)

Here is the code if anybody is interested:

public void mouseReleased(MouseEvent e) {
	if (e.getButton() == MouseEvent.BUTTON1) {

		int start =textPane.getSelectionStart();
		int end = textPane.getSelectionEnd();
				ChunkBordersFinder.findBefore(start, textPane.getText()), 
				ChunkBordersFinder.findAfter(end, textPane.getText()));

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.