Results 1 to 2 of 2
- 08-10-2007, 04:45 AM #1
Member
- Join Date
- Aug 2007
- Posts
- 3
- Rep Power
- 0
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
- 09-22-2007, 04:25 AM #2
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:
Java 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); } } } }
Similar Threads
-
data from the main/GUI thread to another runnin thread...
By cornercuttin in forum Threads and SynchronizationReplies: 2Last Post: 04-23-2008, 10:30 PM -
need help with my 1st multi-method program
By Phobos0001 in forum New To JavaReplies: 6Last Post: 02-08-2008, 05:44 AM -
If JNI thread call the java object in another thread, it will crash.
By skaterxu in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 07:02 AM -
Axiomatic Multi-Platform C 1.6.7
By JavaBean in forum Java SoftwareReplies: 0Last Post: 08-10-2007, 04:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks