Results 1 to 3 of 3
  1. #1
    random7 is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default Heavyweight Component resizing inside JScrollpane

    Hi,

    in my application i have a heavyweight component (jpanel with a few buttons, jtextfields, jtextareas, etc) that i put in a scrollpane.
    the scrollpane is supposed to only scroll vertically - in the horizontal direction, i want to resize the components in the scrollpane to the appropriate size depending on the main frame of the application.

    when the application starts, everything is fine and the components take up the available size.
    when i increase the application window size hotizonally, the components (mainly the jTF and jTA) increase in size, as it is's supposed to be.
    But once i decrease the horizontal size again, the jTF and jTA stay the size they were before and do not shrink as i want them to.
    If i just add the jpanel without putting it inside a jscrollpane, the resizing works perfect. i need a vertical scroller though, so this is not an option.

    here is a picture that shows the resizing behaviour:


    inside the JPanel that is inside the scrollpane i use a grouplayout.
    here is a portion of my code that shows how one of the components inside the panel is layed out.
    Java Code:
    		// component layout for the LINKS panel
    		// horizontal groups
    		SequentialGroup hGroup = layout.createSequentialGroup();
    		ParallelGroup h2 = layout.createParallelGroup();
    		ParallelGroup h3 = layout.createParallelGroup();
    		ParallelGroup h4 = layout.createParallelGroup();
    
    		h2.addComponent(linkListScrollPane, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE);
    		h3.addComponent(bAddLink);
    		h4.addComponent(bRemoveLink);
    
    		hGroup.addGroup(h2);
    		hGroup.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED);
    		hGroup.addGroup(h3);
    		hGroup.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED);
    		hGroup.addGroup(h4);
    		layout.setHorizontalGroup(hGroup);
    
    		// vertical groups
    		SequentialGroup vGroup = layout.createSequentialGroup();
    		ParallelGroup v1 = layout.createParallelGroup();
    
    		v1.addComponent(linkListScrollPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
    				GroupLayout.PREFERRED_SIZE);
    		v1.addComponent(bAddLink);
    		v1.addComponent(bRemoveLink);
    
    		vGroup.addGroup(v1);
    		layout.setVerticalGroup(vGroup);
    If other code portions are needed, please tell me.
    I hope someone can help me with this one :)

  2. #2
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Default

    Hi,
    1)JScrollPane supports lightWeight component only.
    2)JPanel, JTextField etc r lightWright.
    &
    3)I don't use JScrollPane, but still check this out.
    Java Code:
    JScrollPane jsp = new JScrollPane();
    jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    -regards

  3. #3
    random7 is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    Hi,

    thanks for you answer Prajin. Unfortunately it wont solve the problem since the components still remain too large.



    I've found a solution that works though.

    You need to set the preferredSize of the JPanel inside the JScrollPane.
    This way, the components inside the JPanel try to shrink according to the preferredSize.

    How it looks now:
    Java Code:
    // code part building the gui 
    // scrollpane
        JScrollPane frameEditorScroller = new JScrollPane(new FrameEditor());
        frameEditorScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        frameEditorScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    
    
    // panel class that resides in the jscrollpane
    public class FrameEditor extends JPanel {
    
        public FrameEditor()
    	this.setPreferredSize(new Dimension(500, 400);
            // + other stuff below
    
       }
    }
    Last edited by random7; 07-20-2010 at 02:12 PM.

Similar Threads

  1. Transarent background over heavyweight components
    By cartoon_20 in forum AWT / Swing
    Replies: 3
    Last Post: 05-31-2010, 09:14 PM
  2. using awt inside an swt component
    By Jyo in forum AWT / Swing
    Replies: 0
    Last Post: 05-26-2010, 07:06 PM
  3. panel resizing
    By simo_mon in forum AWT / Swing
    Replies: 1
    Last Post: 08-15-2009, 02:09 PM
  4. JFrame resizing
    By carderne in forum New To Java
    Replies: 3
    Last Post: 07-22-2009, 07:42 PM
  5. how to draw an image inside of jscrollpane
    By paty in forum Java Applets
    Replies: 1
    Last Post: 07-24-2007, 12:44 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
  •