Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: JEditorPane in a JScrollPane

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default JEditorPane in a JScrollPane

    Hi,

    I'm trying to use a JEditorPane, in a JScrollPane, in a JSplitPane. It kind of works, but it doesn't scroll in the way I'd like it to; I'd like it to behave like Notepad, without "Word Wrap" on (although eventually I'd like to make this configurable - is that possible?).

    I'm having several problems with this:

    1. The JEditorPane permenantly expands when I expand the JSplitPane that contains it. This means that if I expand the split pane and then shrink it again, the JEditorPane is suddenly too big to be displayed, and a horizontal scrollbar becomes active at the bottom. Ideally I'd like it to always shrink to fit the size of the split pane that contains it.
    2. If I type a full line in the editor pane and continue typing, it wraps onto the next line automatically. Ideally in this situation I'd like it to expand to fit the extra text, requiring horizontal scrolling.


    Any help would be appreciated.

    Thanks,

    - Danjb


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: JEditorPane in a JScrollPane

    Every so often, you'll stumble upon something really stupid in Java; this seems to be one of those times.

    My immediate suggestion would be to use a JTextArea instead, but since you're using a JEditorPane, I would assume you are trying to do things beyond what JTextArea is capable of.

    So, it would seem we have to be creative. I found link that could be useful: "Forced line wrap" and "No wrap" in the JEditorPane
    Read the section that says: "No wrap" implementation and tell me if that works.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Danjb (June 1st, 2011)

  4. #3
    Junior Member
    Join Date
    May 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JEditorPane in a JScrollPane

    Thanks for your reply - I'm glad it's not just me being stupid.

    I tried following those instructions, though, and got stuck at the first hurdle; "We override ParagraphView class with own implementation of layout()". I don't know what it means by this, as I can find no "layout" method to override.

  5. #4
    Junior Member
    Join Date
    May 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JEditorPane in a JScrollPane

    Does anyone have any insight on this?

  6. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JEditorPane in a JScrollPane

    Quote Originally Posted by Danjb View Post
    Does anyone have any insight on this?
    Any chance of seeing an SSCCE that demonstrates what you're talking about?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #6
    Junior Member
    Join Date
    May 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JEditorPane in a JScrollPane

    Quote Originally Posted by KevinWorkman View Post
    Any chance of seeing an SSCCE that demonstrates what you're talking about?
    Of course, and thanks for looking into it:

    HTML Code:
    package gui;
    
    import java.awt.Dimension;
    
    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JSplitPane;
    import javax.swing.ScrollPaneConstants;
    
    public class SplitScrollJEditor extends JFrame {
    
    	public SplitScrollJEditor() {
    		
    		// Right Component
    		JEditorPane textArea = new JEditorPane();
            JScrollPane textAreaScrollPane = new JScrollPane(textArea,
            		ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
            		ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    		
    		// Left Component
    		JPanel panel = new JPanel();
            JScrollPane panelScrollPane = new JScrollPane(panel);
            panelScrollPane.setMinimumSize(new Dimension(100,100));
            
    		// Split Pane
    		JSplitPane splitPane = new JSplitPane();
    		splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
    		splitPane.setDividerLocation(200);
    		splitPane.setContinuousLayout(true);
    		splitPane.setLeftComponent(panelScrollPane);
    		splitPane.setRightComponent(textAreaScrollPane);
    		
    		setSize(800,600);
    		add(splitPane);
    		setVisible(true);
    		
    	}
    	
    	public static void main(String[] args) {
    		SplitScrollJEditor frame = new SplitScrollJEditor();
    	}
    	
    }
    Notice if you drag the divider to the right (making the JEditorPane smaller), the JEdiorPane retains its original size and therefore requires horizontal scrolling.

    Also, if you type a whole line of text, it will automatically wrap to the next line. I want to make this word wrap entirely optional.

    Thanks again,

    - Danjb

  8. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JEditorPane in a JScrollPane

    Alright, I wrestled with this a little, and I'm afraid I don't have a good answer for you. Is there a reason you have to use a JEditorPane instead of a JTextArea? JTextArea seems to work exactly as you describe.

    Hopefully somebody smarter than me can figure this out. Here's an even more condensed SSCCE:

     
    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
     
    public class SplitScrollJEditor{
     
    	public static void main(String[] args) {
     
    		JFrame frame = new JFrame("Text Test");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     
    		//this is the desired behavior
    //		JTextArea text = new JTextArea();
     
    		//why doesn't it work with JEditorPane?
    		JEditorPane text = new JEditorPane();
     
     
    		frame.add(new JScrollPane(text));
    		frame.setSize(500, 500);
    		frame.setVisible(true);
    	}
    }

    Make the JFrame wider, then smaller, and the horizontal scroll bars appear. However, if you use the JTextArea instead, it seems to work fine. Argh! I can see why you'd be frustrated. I'm sure there's a reason for this happening, I just don't know it.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Danjb (June 7th, 2011)

  10. #8
    Junior Member
    Join Date
    May 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JEditorPane in a JScrollPane

    Hehe, thanks again for looking into it anyway. I might just use a JTextArea, at least for now, but eventually I'd like to be able to display slightly more elaborate documents...

    If anyone can suss this out, that would be much appreciated!

  11. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: JEditorPane in a JScrollPane

    Do something for debug purposes. Get the dimensions of the JEditorPane before and after the divider is moved. If they are the same, it means the viewing window changed but the JEditorPane itself has not. To fix it, maybe you could just resize the JEditorPane.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  12. #10
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: JEditorPane in a JScrollPane

    Quote Originally Posted by KevinWorkman View Post
    Hopefully somebody smarter than me can figure this out.
    I don't think this makes me smarter than you , but in your SSCCE the scrollbar appears whenever you decrease the viewport width because JEditorPane overrides getPreferredSize() to do this. The API docs say:

    "The preferred size for JEditorPane is slightly altered from the preferred size of the superclass. If the size of the viewport has become smaller than the minimum size of the component, the scrollable definition for tracking width or height will turn to false. The default viewport layout will give the preferred size, and that is not desired in the case where the scrollable is tracking. In that case the normal preferred size is adjusted to the minimum size. This allows things like HTML tables to shrink down to their minimum size and then be laid out at their minimum size, refusing to shrink any further."

    The JEditorPane minimum width seems to track the current viewport width as it increases (I'm not sure why), so any decrease in viewport width always makes it smaller than the minimum width of the JEditorPane, thus triggering this behaviour.

    You can stop this behaviour by overriding JEditorPane.getScrollableTracksViewportWidth() to always return 'true', but this will obviously interfere with the intended behaviour of the JEditorPane in certain situations.

  13. #11
    Junior Member
    Join Date
    May 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JEditorPane in a JScrollPane

    Hey,

    Thanks for all the info guys - for now I'm using a JTextArea until I can get some other problems sorted out but this will come in very handy later!

    - Danjb
    Last edited by Danjb; June 13th, 2011 at 11:34 AM.

Similar Threads

  1. getting text from JEditorPane
    By nasi in forum AWT / Java Swing
    Replies: 1
    Last Post: May 18th, 2010, 07:38 AM
  2. refreshing JEditorPane
    By nasi in forum AWT / Java Swing
    Replies: 9
    Last Post: April 9th, 2010, 04:01 AM
  3. writing to JEditorPane
    By nasi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 8th, 2010, 09:23 PM
  4. writting to jeditorpane
    By nasi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 5th, 2010, 05:49 AM
  5. create buttons in a JEditorPane
    By seeker in forum AWT / Java Swing
    Replies: 4
    Last Post: December 5th, 2009, 09:01 AM