File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes  Multiple JTextPane referencing trouble  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark " Multiple JTextPane referencing trouble  " Watch " Multiple JTextPane referencing trouble  " New topic

Multiple JTextPane referencing trouble

Brian Colan

Joined: Jul 03, 2003
Posts: 5
Hi all,
I am having trouble with a Java2D program. I need to calculate the number of JTextPanes needed on the screen at compile time, this part is fine and they are stored in a hashtable. Another hashtable is used to store the contents of the textpanes.
Later in the program I am changing the colour of text for a number of lines in a couple of the textpanes at different times (not together). The textpanes are all in a flowlayout. The problem is that the text will change colour for the the first textpane but not any of the others.
This is strange as it is retrieving the correct hashtable reference (can print out its contents for all of the textpanes). I have tried increasing the font size at runtime for all the textpanes in turn and this works so it seems very strange that it can change the font size and not the colour.
To sum up: the first textpane in the flowlayout will respond and change text color on request but the others will not :/ Just wondering if anyone else has come across this ? If further information is needed please let me know. Thank you for your time
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

"B. C." -

Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it here.

Thanks! and welcome to the JavaRanch!

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Brian Colan

Joined: Jul 03, 2003
Posts: 5
Here is some revelant code snippets that will help identify what I am talking about. Hopefully someone will spot something cause I didnt :/ .Thanks! ..if u want a compilable version I can send it ..I have put "//***NOTE:" where the problem is ..thanks and I look forward to a reply
public class Canvas extends JFrame
class P extends JPanel
BufferedImage b = null;
P(BufferedImage bi)
this.b = bi;
public void paint(Graphics g)
if (b != null)
((Graphics2D) g).drawImage(bi, new AffineTransform(1f,0f,0f,1f,0,0), null);

private Color backgroundColour;
P panelMain, panelLeft, panelRight;
BufferedImage bi;
JTextPane jt;
String text;
String allText;
StringBuffer temp;
private DefaultStyledDocument doc;
private StyledEditorKit edkit;
int[] fileLengths;
Hashtable files = new Hashtable();
Hashtable textpanes = new Hashtable();
Hashtable filenames = new Hashtable();

JTextPane jtp; JTextPane jt1 = new JTextPane();
String[] testString;

public Canvas(String title)
this(title, 300, 300, Color.white);
public Canvas(String title, int width, int height)
this(title, width, height, Color.white);
public Canvas(String title, int width, int height, Color bgColour)
panelMain = new P();
panelMain.setLayout(new GridLayout(1, 1));
bi = new BufferedImage(1000, 1000, BufferedImage.TYPE_4BYTE_ABGR);
panelLeft = new P(bi);
panelLeft.setLayout(new BorderLayout());
//populate right panel with boxes of 1 pixel high text for each file
panelRight = new P();
panelRight.setLayout(new GridLayout(2, 1));
doc = new DefaultStyledDocument();
edkit = new StyledEditorKit();
doc = null;
doc = (DefaultStyledDocument)edkit.createDefaultDocument();
jt = new JTextPane(doc);

//get listing of only .java files in directory for display
String[] dir = new"/home/bluec/DEV/").list(new OnlyJava());
for(int i=0; i<dir.length; i++)
temp = new StringBuffer();
//Store file as a StringBuffer
FileInput fi = new FileInput("/home/bluec/DEV/" + dir);
text = new String(fi.readEntireFile());
//store file contents in hashtable
files.put(Integer.toString(i), text)
//store file names in hashtable
filenames.put(Integer.toString(i), fi.getFileNameOnly());
panelMain.setLayout(new FlowLayout());
//populate panel with textPanes
for(int j=0; j<files.size(); j++)
JTextPane jt = new JTextPane();
jt.setFont(new Font("Sans-Serif", Font.PLAIN, 3));
jt.setPreferredSize(new Dimension(200,200));
jt.setMinimumSize(new Dimension(200,200));
//store textpanes in HashTable for reference later
textpanes.put(Integer.toString(j), jt);
resize(new Dimension(width, height));
backgroundColour = bgColour;
public void drawString(String text, int x, int y)
bi.getGraphics().drawString(text, x, y);
public void changeTextColor(int startChar, int endChar, String filename)
String s;
//need to compare filename to filenames hashtable and find right key for jtextpane hashtable
for(int k=0; k<filenames.size(); k++)
s = new String((String)filenames.get(Integer.toString(k)));
if(s.compareTo(filename) == 0)
//***NOTE: it is getting a reference back from the hastable as this prints out the text for each textpane correctly in the console
System.out.println(":::::::::::\nTEXPANE TEXT::::::::::::::" + getTextPane(k).getText());
//here I am trying to highlight all the characters in the text of each called textpane between 0 and 50
//***NOTE: the following line will work for all textpanes so it is strange that the font will not change in the above lines for all textpanes except the first one
getTextPane(k).setFont(new Font("Sans-Serif", Font.PLAIN, 10));

//return the textpane containing the file with the lines to be highlighted
public JTextPane getTextPane(int i)
return (JTextPane)textpanes.get(Integer.toString(i));

//Example usage
public static void main(String[] args)
Canvas c = new Canvas("System in Execution View :: Objects", 1000, 800);
c.drawCircle(10, 10, 50, 50);
c.drawString("Object1", 10, 10);
c.drawCircle(100, 100, 10, 10);
c.drawString("Object2", 100, 100);


Best Regards,
Brian C.
I agree. Here's the link:
subject: Multiple JTextPane referencing trouble
It's not a secret anymore!