Hello,
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()
{
super();
}
P(BufferedImage bi)
{
super();
this.b = bi;
}
public void paint(Graphics g)
{
super.paint(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)
{
super();
setTitle(title);
panelMain = new P();
panelMain.setBackground(Color.gray);
panelMain.setLayout(new GridLayout(1, 1));
bi = new BufferedImage(1000, 1000, BufferedImage.TYPE_4BYTE_ABGR);
panelLeft = new P(bi);
panelLeft.setBackground(Color.gray);
panelLeft.setLayout(new BorderLayout());
panelLeft.setBorder(BorderFactory.createEmptyBorder(250,250,250,250));
//populate right panel with boxes of 1 pixel high text for each file
panelRight = new P();
panelRight.setBackground(Color.white);
panelRight.setLayout(new GridLayout(2, 1));
doc = new DefaultStyledDocument();
edkit = new StyledEditorKit();
doc = null;
doc = (DefaultStyledDocument)edkit.createDefaultDocument();
jt = new JTextPane(doc);
jt.setStyledDocument(doc);
//get listing of only .java files in directory for display
String[] dir = new java.io.File("/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());
panelMain.add(panelLeft);
//populate panel with textPanes
for(int j=0; j<files.size(); j++)
{
JTextPane jt = new JTextPane();
jt.setText((String)files.get(Integer.toString(j)));
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);
panelMain.add(jt);
}
getContentPane().add(panelMain);
//setContentPane(frame);
resize(new Dimension(width, height));
backgroundColour = bgColour;
resize(1000,1000);
}
public void drawString(String text, int x, int y)
{
bi.getGraphics().drawString(text, x, y);
repaint();
}
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
getTextPane(k).setSelectionStart(0);
getTextPane(k).setSelectionEnd(500);
getTextPane(k).setSelectedTextColor(Color.red);
//***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));
getTextPane(k).repaint();
}
}
}
//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.setVisible(true);
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.