Jump to content


Jscrollpane Problem


  • Please log in to reply
2 replies to this topic

#1 greenturtle

greenturtle

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 22 July 2008 - 01:52 PM

CODE
import javax.swing.*;
import java.io.*;
import java.util.Scanner;
import java.awt.*;

public class Help extends JFrame
{
    private static final int HEIGHT = 39;
    private static final int WIDTH = 20;
    public Help()
    {
        JPanel p = new JPanel();
        JTextArea area = new JTextArea(WIDTH,HEIGHT);        
        JScrollPane sp = new JScrollPane(area);
        sp.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        sp.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);        
        Scanner scan;
        PrintWriter output = null;
        String s = "";
        try
        {
            scan = new Scanner( new FileInputStream( "Help.txt"));
            scan.useDelimiter("finito");
            s = scan.next();
        }
        catch(FileNotFoundException e)
        {
            JOptionPane.showMessageDialog( null, "The Help File not found!!");
            s = "The text is not available!!!";
        }        
        area.setText( s);
        p.add(sp);
        getContentPane().add( p);
        pack();
        setVisible( true);        
    }
}


When I executed this frame using a main method the scroll bar created at the end of the text area like this:



But I used BlueJ to execute this class and I did not create a main method. I just create a new Help object and the scroll bar was created from the top.
WHY!!! excl.gif
How can this error be fixed.Please help me...

#2 greenturtle

greenturtle

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 09 April 2009 - 10:12 PM

there is no one to know the solution of this problem unsure.gif

#3 Kraicheck

Kraicheck

    Advanced Member

  • Members
  • PipPipPip
  • 884 posts
  • Gender:Male
  • Location:Belgium

Posted 10 April 2009 - 09:31 AM

After you added the text try
CODE
sp.getVerticalScrollBar().setValue(0);
.