Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JEditorPane and scrollbars

843806Nov 14 2008 — edited Nov 14 2008
Hi there,

I'm using the JEditorPane to display some simple html and css - in fact all it is a div of a fixed width containing an abitary amount of text. What I want to do is calculate the height of the div in pixels. In a browser this is easy enough, you just use javascript to look up the div's clientHeight property, but for the life of me I can't workout how to do this using the Swing api in the JEditorPane. It must know how high the whole rendered html is as it knows when to add the vertical scroll bar

As an alternative approach i though i could just set the size of the containing JFrame and then check to see if a vertical scroll bar had appeared, if it had then I would know that the height of the text in the div exceeded the hight of the frame, but the
scrollPane.getVerticalScrollBar().isVisible()
method always seems to return false...

If anyone has any idea how to do this (or rather, what i'm doing wrong) it would be appreciated.

Thanks
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.UIManager;

public class TextPane extends JFrame {
	  private  JEditorPane pane;
	  private   JScrollPane scrollPane; 	  
	public TextPane(){
	   
	    pane = new JEditorPane();
	    pane.setEditable(true); // Read-only
	    
	   
	    try{
	    	pane.setPage("html-file-that-contains-enough-text-to-cause-the-scroll-bar-to-appear.html");
	    }
	    catch(IOException e){
	    	throw new RuntimeException(e);
	    }	   
	    scrollPane = new JScrollPane(pane,
	    		JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
	    getContentPane().add(scrollPane);


	}

	  public static void main(String[] args) {
		    try {
		        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		    } catch (Exception evt) {}
		  
		    TextPane f = new TextPane();
		    f.addWindowListener(new WindowAdapter() {
		      public void windowClosing(WindowEvent evt) {
		        System.exit(0);
		      }
		    });
		    f.setSize(400, 300);
		    f.setVisible(true);

		    if(f.scrollPane.getVerticalScrollBar().isVisible()){
	            System.out.println("Scroll Bar is showing");
	        }
	        else{
	            System.out.println("Scroll Bar is not showing");
	        }


		    
		    
		   
		  }

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 12 2008
Added on Nov 14 2008
1 comment
134 views