• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can i make the words i want readonly in JTextPane not the whole text?  RSS feed

 
ozlem bal
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to make word or a group of words in JTextPane readonly while other text can be editable.

I suppose if i add JLabel on JTextPane, only the JLabel text will be readonly and the remain text will be editable but that doesn't work. When I backspace the whole label is erased instead of character by character.

How can i do that? If you know any easy way, please let me know.

Thanks
 
Campbell Ritchie
Marshal
Posts: 55770
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, ozlem.

I don't think you can do what you suggest.
You can either have the whole of a text pane editable or not editable, not half of it.
Because a JTextPane is also a java.awt.Component, it supports an add() method, but as you say, you delete the label when you delete the text.

I think you will have to add your label to something else. Can you use GridBagLayout to add the label on top of the TextPane? You might get problems with the label obscuring and hiding part of the pane's text.

CR
[ May 16, 2006: Message edited by: Campbell Ritchie ]
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add a KeyboardListener of some kind that detects delete and backspace and decides to either allow the action or cancel it.
 
Jared Cope
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Originally posted by ozlem bal:

How can i do that? If you know any easy way, please let me know.



I've done something similar to this. It is possible, but gets fiddly depending on what you exactly want to do. I extend a JTextPane and write my own document model that extends DefaultStyledDocument. in the model, I over-ride the insertString and remove methods from DefaultStyledDocument to take into account our business rules.

In the remove method, you will need to look for the text that you don't want removed. If/when you find it, you simply return from that method. If you don't find it, then you can call super.remove() which will actually carry out the deletion. It gets fiddly because you also have to look for cases when the user has selected a portion of text with the mouse (not just a single delete key-stroke).

Hope this can put you on the right track. Let me know if you want more details and I can probably show you some code.

Using JLabels and such is the wrong approach I think.

Cheers, Jared.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!