Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Stretching background colour across whole JTextPane for one line of text

843806Mar 14 2008 — edited Mar 17 2008
Hey guys,

There's something I'd like to do with my JTextPane, but I don't know if it's possible without too much fooling around.

When a line of text is entered into the JTextPane the foreground and the background of the entered message are coloured. All of that works fine, but if possible I would like to also fill the remaining white space of the line the same colour as the existing background, just to make everything look nicer. I would also want the background colour to stretch if the window is stretched, as the JTextPane stretches

This is how I'm going about it.
SimpleAttributeSet attrs = new SimpleAttributeSet();
StyleConstants.setForeground(attrs, Color.red);
StyleConstants.setBackground(attrs, Color.blue);

SimpleAttributeSet attrs2 = new SimpleAttributeSet();
StyleConstants.setForeground(attrs2, Color.gray);
StyleConstants.setBackground(attrs2, Color.blue);

Document doc = conversion.getDocument();

FileCredential credential = ((ClientService)pixecurFile).getCredential();

conversion.getDocument().insertString(doc.getLength(), credential.userData(FileCredential.USER_NAME) + " Says:  ", attrs);
conversion.getDocument().insertString(doc.getLength(), now("yyyy-MM-dd HH:mm:ss aa")+"\n", attrs2);
conversion.getDocument().insertString(doc.getLength(), "   " + chatMessage.getText()+"\n", attrs);
I'm willing to bet that there is no clean way to go about what I'm looking to do, but I hope I'm wrong.

Also, I don't think this matters, but the JTextPane in question is inside of a JScrollPane.

Thanks in advance,
aqzman

Comments

800382
If you have 1 CPU (or 1 core) (and if multiple, I guess it depends on the Java installation), then you only have 1 running at a time. Same as any other multi-threading things you do.

I'm not sure if multiple SwingWorkers create their own threads (I would assume so), but regardless, there can only be so many threads that can be perceived as running at the same time (even though they may not be). And one thread can only do 1 thing at a time.

Sounds like you're app's biting off more than it can chew.
843806
Perhaps I did not properly describe my question. As an example, take the following small program:
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.SwingWorker;

public class SwingWorkerTest
{
  public static void main(String[] args)
  {
    for (int i = 1; i <= 30; i++)
    {
      new Worker(i).execute();
    }
  }
}

class Worker extends SwingWorker
{
  private int id;
  
  public Worker(int id)
  {
    this.id = id;
  }
  
  @Override
  protected Object doInBackground()
  {
    System.out.println(new SimpleDateFormat("HH-mm-ss:").format(new Date(System.currentTimeMillis())) + 
        " started worker no." + id);
    try
    {
      Thread.sleep(5000);         // Do nothing for 5 seconds
    }
    catch (InterruptedException e)
    {
      ;
    }
    return null;
  }
  
}
This program produces following output:
20-42-01: started worker no.10
20-42-01: started worker no.9
20-42-01: started worker no.5
20-42-01: started worker no.3
20-42-01: started worker no.6
20-42-01: started worker no.7
20-42-01: started worker no.2
20-42-01: started worker no.1
20-42-01: started worker no.8
20-42-01: started worker no.4
20-42-06: started worker no.12
20-42-06: started worker no.11
20-42-06: started worker no.13
20-42-06: started worker no.14
20-42-06: started worker no.15
20-42-06: started worker no.16
20-42-06: started worker no.17
20-42-06: started worker no.18
20-42-06: started worker no.19
20-42-06: started worker no.20
20-42-11: started worker no.21
20-42-11: started worker no.22
20-42-11: started worker no.23
20-42-11: started worker no.24
20-42-11: started worker no.25
20-42-11: started worker no.26
20-42-11: started worker no.27
20-42-11: started worker no.28
20-42-11: started worker no.29
20-42-11: started worker no.30
As can be seen, a total of 30 (Swing)Workers are created and executed. However, only the first 10 are started immediately (at 20-42-01), and after 5 seconds when they have terminated the next 10 Workers are started (at 20-42-06), and the same goes for the third bunch of Workers.

So, obviously only ten SwingWorkers are executed simultaneously at any given time. I couldn't find anything about this restriction in the javadocs.

Anyone here with more insight? ;-)
843806
I came to the conclusion that my program needs some redesign since using so many threads (via SwingWorkers) is not a good idea anyway. I think I will be able to manage with two or three SwingWorkers in the end.

However, I'd still be interested to know whether there is an intentional restriction on the number of concurrently running SwingWorkers?
kirillcool
And all it would take is for you to look at the source code of SwingWorker and see how many worker threads are created.
1 - 4
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Apr 14 2008
Added on Mar 14 2008
3 comments
691 views