problem in scrolling a JTextArea
hi,
I am using a JTextArea to display files. I could scroll smoothly without interruptions when I tested with a 1 kb file. But when I loaded a 300 kb file, the scrolling beacame halted and slow. It was not continuous. Can this be fixed?
Also I am thinking of displaying files of larger sizes( 1Mb- 5 Mb). In such cases, I thought it would be better to display only parts of the document as i scroll. Could someone please help me with this?
Thanks,
ramya
We may need a description of your application level requirement, or use-case, and a small example code that could demonsterate the problem.
Post a small demo code that is generally compilable, runnable and could reproduce your problem. See: http://homepage1.nifty.com/algafield/sscce.html and [url=http://riters.com/JINX/index.cgi/Suggestions_20for_20Asking_20Questions_20on_20Newsgroups]this wiki.[/url]
hiwaa at 2007-7-14 20:34:15 >

String display seems very slow in this sort of situation - I've seen the same issue with very long JLabels.
The way I solved this problem (many years ago) was to override the paintComponent() method of the JLabel to work out the scroll offset and only draw enough characters to fill the currently visible portion of the field.
This may be a bit more complicated with a JTextField that needs to be updatable, but it may still be possible to do it in a similar fashion?
also try increasing these for the scrollbar (e.g. try 100)setUnitIncrement(int)setBlockIncrement(int)
> We may need a description of your application level
> requirement, or use-case, and a small example code
> that could demonsterate the problem.
>
> Post a small demo code that is generally compilable,
> runnable and could reproduce your problem. See:
> http://homepage1.nifty.com/algafield/sscce.html and
> [url=http://riters.com/JINX/index.cgi/Suggestions_20f
> r_20Asking_20Questions_20on_20Newsgroups]this
> wiki.[/url]
hello,
I have posted my code below. I have one more request to make.
Please test my program with the 2 files posted at
http://students.uta.edu/RX/rxr4470/java/test1.txt
and
http://students.uta.edu/RX/rxr4470/java/test2.txt
The scrolling looks worse when I move the knob on the track. When I perfrom a unit increment or block increment , it does not look that bad.
Also this program was run using jdk 1.4
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.awt.*;
import java.io.*;
public class scrolltext extends JFrame
{
private String line;
private static String filename;
private Document doc;
private BufferedReader bf;
public scrolltext()
{
super("JScrollBar Demo");
setSize(600,600);
Textarea textarea = new Textarea();
doc = textarea.getDocument();
JScrollPane scroll = new JScrollPane(textarea);
scroll.setPreferredSize(new Dimension(300,250));
JPanel panel = new JPanel();
panel.add(scroll);
getContentPane().add(panel);
loadFile();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void loadFile() // loads the file to textarea
{
try
{
bf = new BufferedReader(new FileReader(filename));
}catch(FileNotFoundException fnf){}
try
{
while((line = bf.readLine())!=null)
{
if(line.charAt(0)=='>')
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),line,null);
}
}catch(IOException ie){} catch(BadLocationException ble){}
}
public static void main(String[] args)
{
filename = args[0]; // gets the file name as a command line parameter
new scrolltext();
}
}
class Textarea extends JTextArea implements Scrollable
{
Textarea()
{
super();
}
public Dimension getPreferredScrollableViewportSize()
{
return getPreferredSize();
}
public int getScrollableBlockIncrement(Rectangle r,
int orietation, int direction)
{
return 10;
}
public boolean getScrollableTracksViewportHeight()
{
return true;
}
public boolean getScrollableTracksViewportWidth()
{
return false;
}
public int getScrollableUnitIncrement(Rectangle r,
int orientation, int direction)
{
return 100;
}
}
Thanks for your time,
ramya
> also try increasing these for the scrollbar (e.g. try
> 100)
> setUnitIncrement(int)
> setBlockIncrement(int)
hello,
Thanks for your suggestion. I tried what you said. I found the unit increment and block increment scrolling better than moving the knob on the track.
When I move the knob for a large file , scrolling looks halted.
When I have small files, I would not be able to set a large value for the increment units.
If you have time, please test my code with the files I have posted. Any suggestion would be of great help.
Thanks for your time,
ramya
> String display seems very slow in this sort of
> situation - I've seen the same issue with very long
> JLabels.
>
> The way I solved this problem (many years ago) was to
> override the paintComponent() method of the JLabel to
> work out the scroll offset and only draw enough
> characters to fill the currently visible portion of
> the field.
>
> This may be a bit more complicated with a JTextField
> that needs to be updatable, but it may still be
> possible to do it in a similar fashion?
hi,
Do you still have the program you did for the String display? It could prove to be a useful pointer for my application.
Thanks for your time,
ramya
I think I find no problem for the test2.txt file.
But it may be that I don't understand your requirement precisely.
Anyway, try this basic version:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.io.*;
public class ScrollText2 extends JFrame{
private String line;
private static String filename;
private BufferedReader bf;
JTextArea textArea;
public ScrollText2(){
super("JScrollBar Demo");
setSize(600,600);
textArea = new JTextArea();
JScrollPane scroll = new JScrollPane(textArea);
scroll.setPreferredSize(new Dimension(300, 250));
JPanel panel = new JPanel();
panel.add(scroll);
getContentPane().add(panel);
loadFile();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void loadFile(){ // loads the file to textarea
StringBuilder sb = new StringBuilder(); // use StringBuffer for 1.4
int count = 0;
try{
bf = new BufferedReader(new FileReader(filename));
while((line = bf.readLine())!=null){
++count;
if (count != 1 && line.charAt(0) == '>'){
line = "\n" + line;
}
sb.append(line);
}
}
catch(IOException ie){
}
textArea.setText(new String(sb));
}
public static void main(String[] args){
filename = "test2.txt";
if (args.length > 0){
filename = args[0];
}
new ScrollText2();
}
}
hiwaa at 2007-7-14 20:34:15 >

Not sure why you wrote your own "load" method. All text components support a read(...) method.
hi,
To both hiwa & Camickr
Thanks a lot for the suggestions.
Please let me try to explain my application, so that you could get an idea of how I am structuring my display.
If you could please take a look at the files I had already posted at
http://students.uta.edu/RX/rxr4470/java/test1.txt
and
http://students.uta.edu/RX/rxr4470/java/test2.txt, you would find the symbol ">" followed by a list of letters , either an "a", "c", "g" or "t". As you would have guessed, they are gene sequences.
Each sequence begins with the ">" symbol and I am trying to display an entire sequence in a single line and hence am inserting a "\n" whenever I encounter a ">".
As I load a huge file(1Mb), scrolling becomes difficult, i.e. it is not smooth.
Has the scrolling got anything to do with the version of the JDK ? I ran my prgram using 1.4 and 1.5; I found a better scrolling in 1.5 than in 1.4, for large files.
To hiwa,
I ran your code , it definitely improved the scrolling, but not like how it scrolls for a 1 Kb file. Please test the code I have pasted in the next section to see the kind of scrolling I am thinking about.
To Camickr,
I had already tried the read() method; it enables perfect and smooth scrolling but I will not be able to display a sequence in a single line.
Below is the code with the read() method. Please compare the scrolling achieved by this code, with the one I posted before. Also please test my program with the files I had already posted.(this may help to explain the scrolling I am trying to get)
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
public class ScrollText1 extends JFrame{
private String line;
private static String filename;
private BufferedReader bf;
JTextArea textArea;private StringBuffer sb; PipedWriter pw;PipedReader pr;
public ScrollText1()
{
super("JScrollBar Demo");
setSize(600,600);
textArea = new JTextArea();
JScrollPane scroll = new JScrollPane(textArea);
scroll.setPreferredSize(new Dimension(300, 250));
JPanel panel = new JPanel();
panel.add(scroll);
getContentPane().add(panel);
loadFile();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void loadFile() // This time using the read() method of JTextComponent
{
try
{
BufferedReader reader = new BufferedReader(new FileReader(filename));
textArea.read(reader,null);
}catch(Exception e){}
}
public static void main(String[] args){
if (args.length > 0){
filename = args[0];
}
new ScrollText1();
}
}
I also tried using Piped streams to read a file ,then structure it the way I want and then display it using the read() method of the JTextComponent ;but that also did not help me get smooth scrolling :(
I could not think of any other way to combine the read() method and the display of each sequence in a line. If you could think of some way out, please let me know.
--
I am wondering if I could display just a portion of the text as I scroll.
Ex:
As I scroll each time, I would like to extract only 100 letters of all the sequences from the document and display it in the TextArea. In this way I could effect nice scrolling.
I am looking forward to more suggestions. Please help me.
Thank you all for your time and help,
ramya
hi,
To both hiwa & Camickr
Thanks a lot for the suggestions.
Please let me try to explain my application, so that you could get an idea of how I am structuring my display.
If you could please take a look at the files I had already posted at
http://students.uta.edu/RX/rxr4470/java/test1.txt
and
http://students.uta.edu/RX/rxr4470/java/test2.txt, you would find the symbol ">" followed by a list of letters , either an "a", "c", "g" or "t". As you would have guessed, they are gene sequences.
Each sequence begins with the ">" symbol and I am trying to display an entire sequence in a single line and hence am inserting a "\n" whenever I encounter a ">".
As I load a huge file(1Mb), scrolling becomes difficult, i.e. it is not smooth.
Has the scrolling got anything to do with the version of the JDK ? I ran my prgram using 1.4 and 1.5; I found a better scrolling in 1.5 than in 1.4, for large files.
To hiwa,
I ran your code , it definitely improved the scrolling, but not like how it scrolls for a 1 Kb file. Please test the code I have pasted in the next section to see the kind of scrolling I am thinking about.
To Camickr,
I had already tried the read() method; it enables perfect and smooth scrolling but I will not be able to display a sequence in a single line.
Below is the code with the read() method. Please compare the scrolling achieved by this code, with the one I posted before. Also please test my program with the files I had already posted.(this may help to explain the scrolling I am trying to get)
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
public class ScrollText1 extends JFrame{
private String line;
private static String filename;
private BufferedReader bf;
JTextArea textArea;private StringBuffer sb; PipedWriter pw;PipedReader pr;
public ScrollText1()
{
super("JScrollBar Demo");
setSize(600,600);
textArea = new JTextArea();
JScrollPane scroll = new JScrollPane(textArea);
scroll.setPreferredSize(new Dimension(300, 250));
JPanel panel = new JPanel();
panel.add(scroll);
getContentPane().add(panel);
loadFile();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void loadFile() // This time using the read() method of JTextComponent
{
try
{
BufferedReader reader = new BufferedReader(new FileReader(filename));
textArea.read(reader,null);
}catch(Exception e){}
}
public static void main(String[] args){
if (args.length > 0){
filename = args[0];
}
new ScrollText1();
}
}
I also tried using Piped streams to read a file ,then structure it the way I want and then display it using the read() method of the JTextComponent ;but that also did not help me get smooth scrolling :(
I could not think of any other way to combine the read() method and the display of each sequence in a line. If you could think of some way out, please let me know.
--
I am wondering if I could display just a portion of the text as I scroll.
Ex:
As I scroll each time, I would like to extract only 100 letters of all the sequences from the document and display it in the TextArea. In this way I could effect nice scrolling.
I am looking forward to more suggestions. Please help me.
Thank you all for your time and help,
ramya
I tried your code and yes its a little jerky when scrolling the text, but thats to be expected for a huge file, but I don't see it as unreasonable.
I tried displaying each line of text in a JTextField and then adding each text field to a panel using a GridLayout. This did not seem to help with the scrolling.
I then tried using a JLabel instead of a JTextField. This seemed to work until I realized that the JLabel stopped renderering the text at a certain length and just started displaying "...".
Finally I tried loading only a single line into the text area. The scrolling worked well. So I would suggest that you change your interface to edit only a single line at a time. Each line seems to be identified with a header record. So maybe you use a combo box to display the header record. When a record is selected then you populate a JTextField with the line. Thats all I can think of.
> thats to be expected for a huge file, but I don't see it as unreasonableI second that. If it's me, I would use my simplest code.
hiwaa at 2007-7-14 20:34:15 >

Hello,
I hope you would be able to recall my problem - smooth scrolling of loads of text.(Please see my problem -right at the top, as the first post)
I worked out a way to display only the required text as i scroll. I could not go in for a scrollpane because I would need to deal with MBs of textual data. :(
Now, I am trying to perform a word search on the text displayed. I do not know how to do this.
Please help with your suggestions and pointers.
I have posted the code below.
import javax.swing.*;
import java.awt.*;
import javax.swing.text.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.text.*;
public class paint_frame extends JFrame implements AdjustmentListener
{
JTextArea text; PlainDocument doc = new PlainDocument();Font f;FontMetrics fm; int ivisible,ivalue;JScrollBar hscroll;
JViewport view; int width; columnpanel cp; JScrollBar vscroll;
paintpanel pp; int ivisible_v; int ivalue_v; VPanel vp; JPanel mp;
JPanel mmp=new JPanel(new BorderLayout());
public static void main(String[] args)
{
paint_frame pf = new paint_frame();
pf.setSize(new Dimension(900,900));
pf.pack();
pf.setVisible(true);
pf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public paint_frame()
{
try
{
doc.insertString(doc.getLength(),"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"67890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"89012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"02345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"67890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"89012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"00345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
}catch(Exception e){}
hscroll = new JScrollBar(JScrollBar.HORIZONTAL,0,0,0,0); // set the max value to be the length of the longest seq. in the alignment.
vscroll = new JScrollBar(JScrollBar.VERTICAL,0,0,0,0);
pp = new paintpanel();
view = new JViewport();
view.setPreferredSize(new Dimension(600,600));
view.setMaximumSize(new Dimension(600,600));
vp = new VPanel();
text = new JTextArea(); view.setView(text);
f = new Font("Monospaced",Font.PLAIN,20);
text.setFont(f);fm = text.getFontMetrics(f); //text.setEditable(false);
mp = new JPanel(new BorderLayout());
cp = new columnpanel();
hscroll.addAdjustmentListener(this);
vscroll.addAdjustmentListener(this);
pp.add(view,BorderLayout.CENTER);
pp.add(hscroll,BorderLayout.SOUTH);
pp.add(cp, BorderLayout.NORTH);
view.setView(text);
JMenuBar bar = new JMenuBar();
JMenu format = new JMenu("format");
JMenuItem font = new JMenuItem("font");
JMenu search = new JMenu("search");
JMenuItem find = new JMenuItem("find");
font.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
f = new Font("Monospaced",Font.PLAIN,18);
text.setFont(f);
fm=text.getFontMetrics(f);
//paintframe3.this.revalidate();
paint_frame.this.repaint();// causes the panel to be repainted
}
});
format.add(font);
bar.add(format);
mp.add(pp,BorderLayout.CENTER);
mp.add(vp,BorderLayout.EAST);
mmp.add(mp,BorderLayout.CENTER);
//mmp.add(headerview,BorderLayout.WEST);
getContentPane().add(mmp,BorderLayout.CENTER);
setJMenuBar(bar);
System.out.println("\n jpanel added");
}
public void adjustmentValueChanged(AdjustmentEvent ae)
{
try
{
paint_frame.this.repaint();
}catch(Exception e){}
}
class columnpanel extends JPanel
{
int alignment_width;
int window_width;
int hvalue;int j;String s=""; int v1,v;
columnpanel()
{
setOpaque(true);
setPreferredSize(new Dimension(view.getSize().width,50));
}
public void doLayout()
{
}
public void paintComponent(Graphics g)
{
window_width = width;
g.setColor(Color.white);
System.out.println(" COLUMN PANEL COLUMN PANEL/*//*" + getSize());System.out.println(" preferred column panel" + getPreferredSize());
g.fillRect(0,0,getSize().width,getSize().height);
g.setColor(Color.black);
g.setFont(f);
j = fm.charWidth('M');
v = hscroll.getValue()/fm.charWidth('M');
for(int i=1;i<=15;i=i+1)
{
if(v==0)
{
s=new Integer((i)*10).toString();
//g.drawString("|", i*j*10-(j+j*v),0+10);
g.drawString(s, i*j*10-(j+j*v),40);
}
else
if(v!=0)
{
s=new Integer((i)*10).toString();
//g.drawString("|", i*j*10-(j+j*v)+view.getSize().width%j,0+10);
g.drawString(s, i*j*10-(j+j*v)+view.getSize().width%j,40);
}
}
}
}// end of column panel class
class paintpanel extends JPanel
{
paintpanel()
{
setSize(new Dimension(800,800));
setLayout(new BorderLayout());
}
public void paintComponent(Graphics g)
{
try
{
int testwidth = view.getSize().width/fm.charWidth('M');
int testheight = view.getSize().height/fm.getHeight();
hscroll.setMaximum(150*fm.charWidth('M'));
vscroll.setMaximum(30*fm.getHeight());
hscroll.setVisibleAmount(testwidth*fm.charWidth('M'));
vscroll.setVisibleAmount(testheight*fm.getHeight());
hscroll.setUnitIncrement(fm.charWidth('M'));
vscroll.setUnitIncrement(fm.getHeight());
hscroll.setBlockIncrement(testwidth*fm.charWidth('M'));
//hvisibleamount = hscroll.getVisibleAmount();
text.setText("");
text.setText(doc.getText(0,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText((151),testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(302,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(453,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(604,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(755,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(906,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(1057,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(1208,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(1359,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(1510,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(1661,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(1812,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(1963,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(2114,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(2265,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(2416,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(2567,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(2718,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(2869,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
/*text.append("\n");
text.append(doc.getText(3020,ivalue+ivisible+hscroll.getValue()));
text.append("\n");
text.append(doc.getText(3171,ivalue+ivisible+hscroll.getValue()));
text.append("\n");
text.append(doc.getText(3322,ivalue+ivisible+hscroll.getValue()));
text.append("\n");
text.append(doc.getText(3473,ivalue+ivisible+hscroll.getValue()));
text.append("\n");
text.append(doc.getText(3624,ivalue+ivisible+hscroll.getValue()));
text.append("\n");
text.append(doc.getText(3775,ivalue+ivisible+hscroll.getValue()));
text.append("\n");
text.append(doc.getText(3926,ivalue+ivisible+hscroll.getValue()));
text.append("\n");
text.append(doc.getText(4077,ivalue+ivisible+hscroll.getValue()));
text.append("\n");
text.append(doc.getText(4228,ivalue+ivisible+hscroll.getValue()));
text.append("\n");
text.append(doc.getText(1379,ivalue+ivisible+hscroll.getValue()));*/
}catch(Exception e){}
}
}// end of paint panel class
class VPanel extends JPanel
{
VPanel()
{
//setLayout(null);
setSize(new Dimension(17,pp.getSize().height));
setBackground(Color.white);
add(vscroll);
}
public void doLayout()
{
vscroll.setBounds(0,view.getBounds().y,getSize().width,view.getSize().height);
//vscroll.setPreferredSize(new Dimension(getSize().width,view.getSize().height));
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
System.out.println("\n vertical panel size" + getSize());
}
} // end of VPanel class
}// end of piant_frame class
Thank you very much for the time,
ramya
> Now, I am trying to perform a word search on the text displayed.String.indexOf(....);
Hello,
The problem I am facing is where to include the search code. Since I am painting the paintpanel to reflect the text display, I know I have to include the search in the paint method of paintpanel class.
I am posting the code that I implemented for search.
Please run it and you will be able to see that though I am able to highlight correctly, I am not able to scroll in the middle of a search.
To do the search repeatedly please select the "search" menuitem and click on "find". The number "2" will be highlighted. This will work only for the first line of numbers.
import javax.swing.*;
import java.awt.*;
import javax.swing.text.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.text.*;
public class paint_frame extends JFrame implements AdjustmentListener
{
JTextArea text; PlainDocument doc = new PlainDocument();Font f;FontMetrics fm; int ivisible,ivalue;JScrollBar hscroll;
JViewport view; int width; columnpanel cp; JScrollBar vscroll;
paintpanel pp; int ivisible_v; int ivalue_v; VPanel vp; JPanel mp;
DefaultHighlighter.DefaultHighlightPainter paint=new DefaultHighlighter.DefaultHighlightPainter(Color.red);int index=-1; String str;
JPanel mmp=new JPanel(new BorderLayout());
public static void main(String[] args)
{
paint_frame pf = new paint_frame();
pf.setSize(new Dimension(900,900));
pf.pack();
pf.setVisible(true);
pf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public paint_frame()
{
try
{
doc.insertString(doc.getLength(),"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"67890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"89012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"02345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"67890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"89012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
doc.insertString(doc.getLength(),"\n",null);
doc.insertString(doc.getLength(),"00345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789*",null);
}catch(Exception e){}
hscroll = new JScrollBar(JScrollBar.HORIZONTAL,0,0,0,0); // set the max value to be the length of the longest seq. in the alignment.
vscroll = new JScrollBar(JScrollBar.VERTICAL,0,0,0,0);
pp = new paintpanel();
view = new JViewport();
view.setPreferredSize(new Dimension(600,600));
view.setMaximumSize(new Dimension(600,600));
vp = new VPanel();
text = new JTextArea(); view.setView(text);
f = new Font("Monospaced",Font.PLAIN,20);
text.setFont(f);fm = text.getFontMetrics(f); //text.setEditable(false);
mp = new JPanel(new BorderLayout());
cp = new columnpanel();
hscroll.addAdjustmentListener(this);
vscroll.addAdjustmentListener(this);
pp.add(view,BorderLayout.CENTER);
pp.add(hscroll,BorderLayout.SOUTH);
pp.add(cp, BorderLayout.NORTH);
view.setView(text);
JMenuBar bar = new JMenuBar();
JMenu format = new JMenu("format");
JMenuItem font = new JMenuItem("font");
JMenu search = new JMenu("search");
JMenuItem find = new JMenuItem("find");
find.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
str = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
try
{
index = index + 1;
index=str.indexOf("2",index);
System.out.println("\n index" + index);
if(index!=-1)
{
pp.repaint(); //index = index+1;
}
else
{
//kit.beep();
}
//index=str.indexOf(str1,index);
}catch(Exception ble){System.out.println("bad location");}
}
});
search.add(find);
bar.add(search);
font.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
f = new Font("Monospaced",Font.PLAIN,18);
text.setFont(f);
fm=text.getFontMetrics(f);
//paintframe3.this.revalidate();
paint_frame.this.repaint();// causes the panel to be repainted
}
});
format.add(font);
bar.add(format);
mp.add(pp,BorderLayout.CENTER);
mp.add(vp,BorderLayout.EAST);
mmp.add(mp,BorderLayout.CENTER);
//mmp.add(headerview,BorderLayout.WEST);
getContentPane().add(mmp,BorderLayout.CENTER);
setJMenuBar(bar);
System.out.println("\n jpanel added");
}
public void adjustmentValueChanged(AdjustmentEvent ae)
{
try
{
paint_frame.this.repaint();
}catch(Exception e){}
}
class columnpanel extends JPanel
{
int alignment_width;
int window_width;
int hvalue;int j;String s=""; int v1,v;
columnpanel()
{
setOpaque(true);
setPreferredSize(new Dimension(view.getSize().width,50));
}
public void doLayout()
{
}
public void paintComponent(Graphics g)
{
window_width = width;
g.setColor(Color.white);
System.out.println(" COLUMN PANEL COLUMN PANEL/*//*" + getSize());System.out.println(" preferred column panel" + getPreferredSize());
g.fillRect(0,0,getSize().width,getSize().height);
g.setColor(Color.black);
g.setFont(f);
j = fm.charWidth('M');
v = hscroll.getValue()/fm.charWidth('M');
for(int i=1;i<=15;i=i+1)
{
if(v==0)
{
s=new Integer((i)*10).toString();
//g.drawString("|", i*j*10-(j+j*v),0+10);
g.drawString(s, i*j*10-(j+j*v),40);
}
else
if(v!=0)
{
s=new Integer((i)*10).toString();
//g.drawString("|", i*j*10-(j+j*v)+view.getSize().width%j,0+10);
g.drawString(s, i*j*10-(j+j*v)+view.getSize().width%j,40);
}
}
}
}// end of column panel class
class paintpanel extends JPanel
{
int testwidth ,testheight;
paintpanel()
{
setSize(new Dimension(800,800));
setLayout(new BorderLayout());
}
public void paintComponent(Graphics g)
{
try
{
testwidth = view.getSize().width/fm.charWidth('M');
testheight = view.getSize().height/fm.getHeight();
hscroll.setMaximum(150*fm.charWidth('M'));
vscroll.setMaximum(30*fm.getHeight());
hscroll.setVisibleAmount(testwidth*fm.charWidth('M'));
vscroll.setVisibleAmount(testheight*fm.getHeight());
hscroll.setUnitIncrement(fm.charWidth('M'));
vscroll.setUnitIncrement(fm.getHeight());
hscroll.setBlockIncrement(testwidth*fm.charWidth('M'));
//hvisibleamount = hscroll.getVisibleAmount();
text.setText("");
text.setText(doc.getText(0,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText((151),testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(302,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(453,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(604,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(755,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(906,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(1057,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(1208,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(1359,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(1510,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(1661,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(1812,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(1963,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(2114,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(2265,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(2416,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(2567,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(2718,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
text.append("\n");
text.append(doc.getText(2869,testwidth+(hscroll.getValue()/fm.charWidth('M'))));
/*text.append("\n");
text.append(doc.getText(3020,ivalue+ivisible+hscroll.getValue()));
text.append("\n");
text.append(doc.getText(3171,ivalue+ivisible+hscroll.getValue()));
text.append("\n");
text.append(doc.getText(3322,ivalue+ivisible+hscroll.getValue()));
text.append("\n");
text.append(doc.getText(3473,ivalue+ivisible+hscroll.getValue()));
text.append("\n");
text.append(doc.getText(3624,ivalue+ivisible+hscroll.getValue()));
text.append("\n");
text.append(doc.getText(3775,ivalue+ivisible+hscroll.getValue()));
text.append("\n");
text.append(doc.getText(3926,ivalue+ivisible+hscroll.getValue()));
text.append("\n");
text.append(doc.getText(4077,ivalue+ivisible+hscroll.getValue()));
text.append("\n");
text.append(doc.getText(4228,ivalue+ivisible+hscroll.getValue()));
text.append("\n");
text.append(doc.getText(1379,ivalue+ivisible+hscroll.getValue()));*/
}catch(Exception e){}
// highlighting the search word
try
{
if(index>-1)
{
text.getHighlighter().addHighlight(index,index+1,paint);
/*if(index==-1)
{
text.getHighlighter().addHighlight(pindex,pindex+1,paint);
}*/
if(index>testwidth)
{
/*if(smallestindexset==false)
{
smallestindex = index;
smallestindexset=true;
}*/
hscroll.setValue(((((index-testwidth)/testwidth)+1)*testwidth)*fm.charWidth('M'));
//text.getHighlighter().addHighlight(pindex,pindex+1,paint);
}
}
}catch(Exception e){}
}
}// end of paint panel class
class VPanel extends JPanel
{
VPanel()
{
//setLayout(null);
setSize(new Dimension(17,pp.getSize().height));
setBackground(Color.white);
add(vscroll);
}
public void doLayout()
{
vscroll.setBounds(0,view.getBounds().y,getSize().width,view.getSize().height);
//vscroll.setPreferredSize(new Dimension(getSize().width,view.getSize().height));
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
System.out.println("\n vertical panel size" + getSize());
}
} // end of VPanel class
}// end of piant_frame class
Thanks ,
ramya
