Clive Mercer

Greenhorn
+ Follow
since Dec 30, 2009
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Clive Mercer

Funnily enough I just did exactly what you said, then I couldn't post on the forum because it was under maintenance.

Still, thank you for the very fast response!
14 years ago
Hi

Can someone please show me how to find out the colour of a character in a StyledDocument?

For example, I have a JTextPane and I wish to know if a certain character's foreground is Green?

Thanks
14 years ago
The DocumentListener receives the insertUpdate() or removeUpdate() events and passes this information to another class. This class then places these words ("TypeDuo" objects) into the ColourQueue. I know that the ColourQueue is getting populated correctly and that no changes are being lost - as evidenced by the println() statement on line 9.

Some of the words get coloured correctly, however many appear to be ignored, although they are definetly retreived from the ColourQueue.

Could there be something blocking setCharacterAttributes() from applying the changes, such as some sort of Document lock?

Regards,
Clive
14 years ago
Brian, I used your examples of ParenMatcher and LiveParenMatcher. Inside LiveParenMatcher's listener I had my parsing algorithm break down and identify individual keywords then send them to a static Vector inside a wrapper class. This vector class is tested to work correctly under concurrency.
ParenMatcher's run() method would then busy-loop (for testing) waiting for the Vector's size to become more than zero, at which point it would enter a loop, get the keyword details and colour them.



Unfortunately, I am back at square one. Although every single word is identified, stored and retrieved correctly, many are not coloured at all. It's almost as if setCharacterAttributes() ignores some of the calls made to it!

Here is the code for the run() method:

CQ is wrapper class containing the static vector, TypeDuo is the class which holds information of each word to be coloured. The println() statement verifies that every word is correctly received and sent to setCharacterAttributes(), the question is, why are some of these words ignored?

Any and all help is very appreciated, this problem is driving me crazy!
14 years ago
Brian, this is great stuff. The code editor is extremely simple, so things like matching parenthesis are a little beyond me (I'm a beginner!). The two samples however are very informative and I shall certainly look into them.

If the DocumentFilter was used to do the colouring, would it still render the document unusable until it is completed, like it would be when colouring from within the DocumentListener?

Thanks again for your input and patience
14 years ago
Thanks for your responses!

Rob Camick/Stanislav Lanitsky/Campbell Ritchie:
The project is a Code Editor with syntax highlighting - the reason I used a separate thread instead of doing the colouring from within the listener was so that a user will not have to wait for the colouring to take place whilst they work with the document. If the colouring code was placed inside the listener via SwingUtilities.invokeLater(), then every modification would force the user to wait for the program to catch up.

In the SSCCE, you are right, some values would be skipped if the updates are happening quicker than the Colourer thread can colour them. This is a fault in my SSCCE, the actual application uses a Vector to "queue" words up waiting to be coloured. However, this colouring problem is present in both applications.
This leads me to believe that I've misunderstood StyleAttributes or some aspect of concurrency which is causing the doc.setCharacterAttributes() to not be able to colour certain characters if called repeatedly in a short amount of time.

Brian Cole:
I did not know about javax.swing.text.Position, I will certainly look it up. The DocumentFilter solutions looks like a great idea, thank you for showing me this, I shall try it tonight!
14 years ago
Bump?

I'm not really expecting a solution (although that would be great), even a pointer in the right direction would be very appreciated.
I genuinely want to learn and develop as a programmer, but it is extremely difficult if I'm stuck and can't find answers anywhere.

My question is - why do StyleAttributes not get applied if called really quickly?

Regards,
Clive
14 years ago
Hi

I have encountered a problem regarding StyleAttributes in a JTextPane. I am working on a Programmer's Editor with live syntax highlighting, and I have a problem where if I put in small sections of text, the colouring is fine. However if I paste in larger chunks, the colours do not apply correctly - to me this seems to be a speed issue. Could it be the overhead of setCharacterAttributes obtaining a writelock?

To demonstrate this, I have made a SSCCE demonstrating the problem. If I paste in the word "hello" once, it colours fine. However if I keep CTRL+V pressed, some "hello"s get coloured but others do not (screenshots are attached).

Can someone please share their expertise as to why this is, and possibly how to fix it?

Thanks!

Clive

P.S: Mods - This is a followup to an older post. I wasn't sure about the rules regarding bumping old threads, so I made a new one. I hope this is not a problem.

Source:
14 years ago
Ireneusz, thank you for your response.

Yes, I used the attributes object to colour text with different colours, I am very impressed you thought of that!

However, I still have the same problem.

Here is the method:


If I paste lots of text in, most of the keywords will get coloured blue and most of the normal words will get coloured red.
But some words do not get coloured, even though they DO show up in the println.

Did I get the attributes object wrong again or could it be something else?

Thanks again,

Clive


EDIT:
Here is a screenshot of the problem:
14 years ago
Hi folks, long time lurker

I've got a problem with Style Attributes not always colouring stuff.
I'm simplifying the code here, my set up is pretty complex, but I think the snippet below is a good representation of the problem.

I have the following method:



This outputs the location of the word and then colours it in blue. Everytime I call it, the values of the "word" value are different, as I want to colour different parts of the document.

When I call the above method several times within a second or so, MOST of the time, it colours the text correctly.
But sometimes, it doesn't.
I know it can find the location of the word just fine, due to the println, I'm confused as to why the changes sometimes dont apply?

Do I need to go back and and look at my code further or is it something to do with StyleConstants/Attributes?

Apologies for not posting exact code - I just have a feeling its not the code but a 'feature' of java doing this.

Thanks in advance,

Clive
14 years ago