Results 1 to 6 of 6
  1. #1
    zyky is offline Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Question Problem with scrolling a JTextPane with a minimum width

    Hello everybody,

    I'm using a JTextPane inside a JScrollPane, and I want the JTextPane to have a minimum width. The problem is that when i try to scroll to the right, the scrollbar is forced back to the left.

    It may be clearer to show with a simple example (try running the code and pasting in the following text, then scroll to the right):

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextPane;
    
    public class Main {
    
    	public static void main(String[] args) {
    		JFrame f = new JFrame("Scroll");
    
    		JTextPane t = new JTextPane() {
    			private static final long serialVersionUID = 1L;
    			public void setBounds(int x, int y, int width, int height) {
    				super.setBounds(x, y, Math.max(width, 5000), height);
    				System.out.println("setBounds + x:"+x+", y:"+y+", w:"+Math.max(width, 5000)+", h:"+height);
    			}
    		};
    //		t.setPreferredSize(new Dimension(5000,2000));		
    //		t.setMinimumSize(new Dimension(5000,2000));
    		
    		JScrollPane s = new JScrollPane(t);
    		JPanel p = new JPanel(new BorderLayout());
    		p.add(s, BorderLayout.CENTER);
    
    		f.getContentPane().add(p);
    		f.pack();
    		f.setBounds(400,400,400,400);
    		f.setVisible(true);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    }
    Java Code:
                        <XXXXXXXXX>XX_XXXXX_XXXXXXXX</XXXXXXXXX>
                        <XXX__XXXX__XXXX/>
                        <XXXXX__XXXK__XXXX>XX-XX-XXXX</XXXXX__XXXK__XXXX>
                        <XXXXXXX__XX__XXXX>XX-XX-XXXX</XXXXXXX__XX__XXXX>
                        <XXXXXXX>X</XXXXXXX>
                        <XXXX__XXXX>XX-XX-XXXX</XXXX__XXXX>
                        <XXXXXXXX>XX</XXXXXXXX>
                        <XXXX__XXX>XX</XXXX__XXX>
                        <XXX__XXX>XXX</XXX__XXX>
                        <XXX__XXXXX>XXXX</XXX__XXXXX>
                        <XXXXXXX__XXXX>XXXXXXXX --XXXXXXXX -</XXXXXXX__XXXX>
                        <XXXX__XXX>XX</XXXX__XXX>
                        <XXX__XXX__XXX>XXXXXXXXXX</XXX__XXX__XXX>
                        <XXXX__XXXX>XXXXXXXX_XXXX_XXXX{XXX_XXX=      ;XXXX_XXX=                                                  ;XXX=X   ;XXXXX=+;XXX=XXXXXX;XXX_XXX=  ;XXXXXXXXX=               ;XXXXXXXXXXX=                                                  ;XXXXXXX=                                                  ;XX_XX_XXXXX=                                                  ;XX_XX_XXXX=                                                  ;XXX_XXXXXXXXX=      ;XXXX_XXX_XXXXXXXXX=                                                  ;XXXX_XXXXXXXX=                         ;XXXXXXXXX=                                   ;XXX_XXX=   ;XXX_XXXXX=    ;XXX_XXXX=  ;XXX_XXXX_XXXXX= ;}</XXXX__XXXX>
                        <XXXX__XXXXXXXXX>X</XXXX__XXXXXXXXX>
                        <XXXX__XXXXXX/>
                        <XXXXX__XXXX>XXXX</XXXXX__XXXX>
                        <XXXX__XXXX>XXX</XXXX__XXXX>
                        <XXX__XX__XXXXXXX>XXXXXXXXXXXXX-XX</XXX__XX__XXXXXXX>
                        <XXXXXXXXXX__XXXX>XXXXXXXXXXXXXXX</XXXXXXXXXX__XXXX>
                        <XXXX__XXX/>
                        <XXXXXXXXXXX__XXX>                 XXXXXXX        XXXXX XXXXXXXX                               +XXXXXXX-XXXXXX X   XXXXXX                                 XXXXXXX        XXXXX XXXXXXXX                               +XXXXXXX-XXXXXX X  XXXXXXX        XXXX.XXX XXXXXXX X'XXXXXX XXXXX.                 XXXXXXX        XXXXX XXXXXXXX                               +XXXXXXX-XXXXXX X  XXXXXXX        XXXX.XXX XXXXXXXXXX X'XXXXXX XXXXX.                 XXXXXXX        XXXXX XXXXXXXX                               +XXXXXXX-XXXXXX X  XXXXXXX        XXXX. XXX XXXXX. XX XXXXXXXXX XXXXX.                 XXXXXXX        XXXXX XXXXXXXX                               +XXXXXXX-XXXXXX X  XXXXXXX        XXX. XXXX. XXX XXXXXXX</XXXXXXXXXXX__XXX>
                        <XXXXXXXXXXX__XXX/>
                        <XXXXXX__XXXXXX__XXX XXXXX:XXX="XXXX://XXX.XX.XXX/XXXX/XXXXXXXXX-XXXXXXXX" XXX:XXX="XXXX"/>
    Has anybody any idea?

    Thank you in advance
    Gabriele

    PS: my original goal was to have a "no line wrapping" JTextPane inside a JScrollPane, but after looking over the internet I couldn't make it work, so I'm trying this workaround.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Problem with scrolling a JTextPane with a minimum width

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Problem with scrolling a JTextPane with a minimum width

    Not sure what your ultimate aim is, but you could try overriding getPreferredSize() rather than setBounds(...). Something like
    Java Code:
    public Dimension getPreferredSize() {
        Dimensions d = supre.getPreferredSize();
        d.width = width > 5000 ? 5000 : width;
        return d;
    }
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default Re: Problem with scrolling a JTextPane with a minimum width

    Why are you using a JTextPane? JTextArea seems to do what you want out of the box.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: Problem with scrolling a JTextPane with a minimum width

    my original goal was to have a "no line wrapping" JTextPane inside a JScrollPane
    See: No Wrap Text Pane « Java Tips Weblog

  6. #6
    zyky is offline Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Default Re: Problem with scrolling a JTextPane with a minimum width

    Thank you a lot DarrylBurke, your solution worked perfectly.

    @KevinWorkman: I'm using JTextPane because it allows to color the text and such things.

    @camickr: I tried that solution but in some scenarios it didn't for me.

    Thank you all
    Have a nice day

Similar Threads

  1. JTextPane problem
    By mitra in forum AWT / Swing
    Replies: 2
    Last Post: 08-04-2011, 07:25 AM
  2. JTextPane Unicode/UTF-8 problem?
    By LeonLanford in forum AWT / Swing
    Replies: 15
    Last Post: 08-16-2010, 02:36 PM
  3. problem in JTextPane
    By jperson in forum New To Java
    Replies: 4
    Last Post: 07-07-2010, 04:57 PM
  4. Scrolling problem
    By lethalwire in forum Eclipse
    Replies: 5
    Last Post: 06-27-2010, 11:37 AM
  5. problem with changes of Dynamic input width box
    By synclenovo in forum New To Java
    Replies: 1
    Last Post: 04-09-2009, 12:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •