• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Reading a file in ScrollPane

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I would like to read a file in Java ScrollPane. The problem I face is in setting up the ScrollPane size. I have set up the ScrollPane size initially. I dont have any problems, if the file is small and could be accomodated in the ScrollPane. if the file size is larger and exceeds the scrollpane size I couldnt display few Lines.
so how could i get through this problem. Is there a way that I can set Pane size depending on the file size i am going to display in the pane.
Thanks
Chandhra
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what you're doing, exactly, as far as displaying text directly in a scroll pane. If you're using Swing, the thing to do is to put the text into a JTextArea, then add the JTextArea to a JScrollPane, and the two work together automatically to make sure all the text is visible.
If you're using AWT, then you don't need to use ScrollPane at all; TextArea knows how to scroll itself, and you can simply add whatever text you want to the TextArea, and it will create its own scroll bars as needed.
In both cases it helps, of course, if the components in question are managed by a reasonable LayoutManager that wants to keep the whole component in the visible part of the window -- i.e., no using "null" for a LayoutManager.
 
Chandhrasekar Saravanan
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using awt to display my files. I need to format the lines i need to display, so I am using ScrollPane to display the lines and not the text area.
The code is below. I am setting the dimension of the scrollpane using setDimension, in the PaneWriter class.
As I said before, I could display the files only for the dimension I have set. I need to set the scoll pane dimension according to the file size.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is just that instead of the hardcoded "3000" you want to compute a value? Well, if you know the number of lines of text, you can compute the size like this:
public void paint(Graphics g){
Font f = g.getFont();
FontMetrics fm = g.getFontMetrics(f);
int lineHeight = fm.getHeight();
int panelHeight = numLinesOfText*lineHeight;
But if you're drawing the lines yourself, you must already know how to do this.
Maybe the problem is that you want to change the size of the panel after it's already onscreen? In that case, you want to set a new value for your getPreferredSize() method to return, then call doLayout() on the ScrollPane to actually resize it.
I hope something in here helps.
 
Chandhrasekar Saravanan
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your suggestion. As you said I need to go with doLayout() method of ScrollPane. I added doLayout() method to my ScrollPane, but i couldnt find any difference. I did not make any other modifications to my previous code. The dimension remains the same.
I am adding only the modified code below.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand how you got from what I wrote to what you did, but let me try again.
First, in your PaneWriter.getPreferredSize() method, I suggested that you compute the Y dimension using FontMetrics, instead of hard-coding the "3000".
Second, I suggested that whenever you have to change the value that getPreferredSize() should return -- ie., because the amount of text grew --
that you need to call scrollpane.doLayout(). In the example you've shown here, the doLayout() call does nothing, because the getPreferredSize() method returns a constant dimension.
So anyway, if you compute the correct dimension to return, can you see all the text then?
 
reply
    Bookmark Topic Watch Topic
  • New Topic