Resizing issue with multiple JTextAreas inside a JScrollPane


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Resizing issue with multiple JTextAreas inside a JScrollPane

  1. #1
    Join Date
    Nov 2008
    Posts
    3

    Resizing issue with multiple JTextAreas inside a JScrollPane

    Hey all, I have an issue that I'm hoping someone may help me resolve:

    I am creating an application that parses an XML file and puts several elements into JTextAreas, one area per element. So far so good. The client wants to display them in a column, maximum width, inside a scrollable area. So my current setup is as follows:

    JPanel(Gridlayout(1,1))[
    JScrollPane[
    JPanel(BoxLayout, Y_AXIS)[
    JPanel(BorderLayout)[ JTextArea(BorderLayout.CENTER) ]
    JPanel(BorderLayout)[ JTextArea(BorderLayout.CENTER) ]
    JPanel(BorderLayout)[ JTextArea(BorderLayout.CENTER) ]
    ...
    ]
    ]
    ]


    The idea is to satisfy the following conditions:
    • Each JTextArea is expected to wrap its text when wider than the parent.
    • Each JTextArea is as wide as the parent, even if there is less text (which is why they're wrapped in a panel with BorderLayout).
    • The JTextAreas are arranged in a column (parent JPanel with BoxLayout).
    • Each JTextArea should only be as high as necessary (read - each text area may have different height, hence no GridLayout(0,1)).
    • The column of JTextAreas is within a scrollable pane.
    • The JScrollPane is within another panel due to API reasons - the method is expected to return a JPanel.


    Now, when I first populate the UI it all looks great, same when I make the root panel wider - the JTextAreas re-wrap their text to maximize their width. However, when I make the root panel narrower, the height of the JTextAreas stays the same, which translates into no wrapping, which translates into no resizing - So I end up with a column of JTextAreas with 1 row within the JScrollPane, and I have to enable the horizontal scrollbar again (which I'd rather not do). Could anyone give me a pointer as to how I could coerce the JTextAreas to re-wrap their content? I'm open to suggestions for alternative construction, just as long as the look&feel is more or less preserved (no JTrees, I'm afraid). Below are the relevant excerpts from my code:

    Code:
    JPanel master = new JPanel( new BorderLayout( ) );
    JPanel panel  = new JPanel( );
    panel.setLayout( new BoxLayout( panel, BoxLayout.Y_AXIS ) );
    
    ArrayList<MasterDocumentElement> elements = doc.getElements( );
    Iterator<MasterDocumentElement> it = doc.getElements( ).iterator( );
    while( it.hasNext( ) ){
    	MasterDocumentElement elem = it.next( );
    	JPanel nest = new JPanel( new BorderLayout( ) );
    	JTextArea t = new JTextArea( "Name: " + elem.getName( ) + "\n" + "Attributes: " + elem.getAttributes( ).toString( ) );
    	t.setLineWrap( true );
    	t.setWrapStyleWord( true );
    	nest.add( t, BorderLayout.CENTER );
    	panel.add( nest );
    }
    JScrollPane scroll = new JScrollPane( );
    scroll.getViewport( ).add( panel );
    scroll.setHorizontalScrollBarPolicy( ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
    master.add( scroll, BorderLayout.CENTER );
    return master;
    Thanks in advance.
    Last edited by Clarox; 11-13-2008 at 08:47 PM.

  2. #2
    Join Date
    Nov 2008
    Posts
    3
    Update: solved by creating a VerticalScrollablePanel extending JPanel implementing Scrollable, and setting
    Code:
    public boolean getScrollableTracksViewportWidth( ){ return true; }
    I then use this class instead of JPanel for the panel using the BoxLayout.

  3. #3
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Thanks for coming back and sharing your solution. It should help someone else with the same or similiar problem.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

Similar Threads

  1. Replies: 2
    Last Post: 11-11-2008, 03:59 AM
  2. Replies: 2
    Last Post: 08-06-2006, 01:49 PM
  3. Finding & resizing multiple images
    By apprentice in forum VB Classic
    Replies: 1
    Last Post: 09-13-2005, 03:38 PM
  4. JPanel paintComponent inside JScrollPane
    By vu_tech in forum Java
    Replies: 1
    Last Post: 04-08-2005, 10:36 AM
  5. Replies: 0
    Last Post: 08-27-2001, 08:58 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center