Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-10-2007, 05:45 AM
Member
 
Join Date: Aug 2007
Posts: 3
jkhoa is on a distinguished road
How come multi thread don't look like it?
Hi I'm still realtively new to java and really new to threads, so please bear with me.

I'm basically making a an applet that has a jtextpane, and when you type stuff in it changes the color of the numbers from the standard black to red. Im trying to make however a seperate thread that searches and colors the jtextpane. It seems to work right now but it is extremely slow, I just wondering if this setup is really making the system run multi threads.
public class TEV3 extends JApplet implements ActionListener{
public void init() {
final sh_Brackets setsThread = new sh_Brackets();
setsThread.start();
TA.getDocument().addDocumentListener(new DocumentListener(){

// This method is called after an insert into the document
public void insertUpdate(DocumentEvent evt) {
setsThread.displayText = rawString;
}
});
}
}



and then the other class basically is this

public class sh_Brackets extends Thread{
public void run(){
while(true){
//displayText do whatever here to color
}
}
}
thanks in advance for any help
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-22-2007, 05:25 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,147
hardwired is on a distinguished road
DocumentFilter
With a continuously–running loop that is busy styling all of the text in the textPane, called by a DocumentListener, I would say that your app could run very slow.
A lighter–weight way to do this is with a simple DocumentFilter. Here's one way you could put it together:
Code:
// <applet code="TEV3Rx" width="400" height="400"></applet> import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.*; public class TEV3Rx extends JApplet { public void init() { JTextPane textPane = new JTextPane(); Style style = textPane.addStyle("basic", null); StyleConstants.setFontSize(style, 18); StyleConstants.setForeground(style, Color.green.darker()); StyleConstants.setFontFamily(style, "Lucida Sans Unicode"); textPane.setLogicalStyle(style); style = textPane.addStyle("redDigits", null); StyleConstants.setForeground(style, Color.red); AbstractDocument doc = (AbstractDocument)textPane.getDocument(); doc.setDocumentFilter(new DigitFilter(style)); getContentPane().add(textPane); } public static void main(String[] args) { JApplet applet = new TEV3Rx(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setContentPane(applet); f.setSize(400,400); f.setLocation(200,200); applet.init(); f.setVisible(true); } } class DigitFilter extends DocumentFilter { String domain = "0123456789"; Style digitStyle; public DigitFilter(Style style) { digitStyle = style; } public void insertString(DocumentFilter.FilterBypass fb, int offset, String str, AttributeSet attrs) throws BadLocationException { replace(fb, offset, 0, str, attrs); } public void replace(DocumentFilter.FilterBypass fb, int offset, int length, // selected text String str, // incoming string AttributeSet attrs) throws BadLocationException { char[] source = str.toCharArray(); char[] result = new char[source.length + 2]; int k = 0; for(int j = 0; j < source.length; j++) { result[k++] = source[j]; fb.replace(offset, length, new String(result, 0, k), attrs); if(domain.indexOf(source[j]) != -1) { StyledDocument doc = (StyledDocument)fb.getDocument(); doc.setCharacterAttributes(offset, 1, digitStyle, false); } } } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
data from the main/GUI thread to another runnin thread... cornercuttin Threads and Synchronization 2 04-23-2008 11:30 PM
need help with my 1st multi-method program Phobos0001 New To Java 6 02-08-2008 06:44 AM
If JNI thread call the java object in another thread, it will crash. skaterxu Advanced Java 0 01-28-2008 08:02 AM
Creating a Thread (extending Java Thread Class) JavaForums Java Blogs 0 12-19-2007 10:31 AM
Axiomatic Multi-Platform C 1.6.7 JavaBean Java Announcements 0 08-10-2007 05:46 PM


All times are GMT +3. The time now is 12:44 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org