Search code examples
javaresizejscrollpanejtextpane

How to resize a JTextPane component binded in a JScrollPane


In a JPanel with a GridBagLayout I've created a list of JTextPane components that are binded with JscrollPane components. Unfortunately, the size of the components seem to be fixed: when I decrease the JFrame size, I want to force the component to dynamically fit their sizeto the JFrame. For example when the main windows size is decreased to 20pixels, I want that the compents change their size to 20 pixels, too.

Here is my code:

import java.awt.*;
import javax.swing.*;

public class ScrollbarTest {

private JFrame frame;
private JPanel panel;
private JScrollPane [] scrollpane = new JScrollPane[4];
private JTextArea [] textPane = new JTextArea[4];
private JScrollPane scrollbar;


ScrollbarTest() {
    //initialize jtextarea
    for(int i=0;i<textPane.length;i++) {            
        textPane[i]=new JTextArea();            
        textPane[i].setText("xxxx xxxx xxxx xxxx xxxx xxxx xxxx | ");
        scrollpane[i] = new JScrollPane(textPane[i]); 
        scrollpane[i].setMinimumSize(new Dimension(100,100));
    }

    panel     = new JPanel();
    frame     = new JFrame();

    createList(panel);
    scrollbar = new JScrollPane(panel);

    frame.add(scrollbar);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setSize(new Dimension(400,400));          
} //constructor

//method to easily add components to GridBags
static void addComponent(JPanel panel, 
                         GridBagLayout gbl, 
                         Component c,
                         int x, int y,
                         int width, int height,
                         double weightx, double weighty) {

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = x; gbc.gridy = y;
    gbc.gridwidth = width; gbc.gridheight = height;
    gbc.weightx = weightx; gbc.weighty = weighty;
    gbl.setConstraints( c, gbc );
    panel.add( c ); 
}//addComponent

public void createList(JPanel panel) {
    //setting layout: "GridBagLayout"
    GridBagLayout gbl = new GridBagLayout();        
    panel.setLayout(gbl);                        

    //put created components to the gridbags of gridbaglayout
    //                                            x  y  w  h  wx wy
    addComponent( panel, gbl, scrollpane[0]     , 0, 1, 1, 1, 1 ,1  );
    addComponent( panel, gbl, scrollpane[1]     , 0, 2, 1, 1, 1 ,1  );
    addComponent( panel, gbl, scrollpane[2]     , 1, 1, 1, 1, 1 ,1  );
    addComponent( panel, gbl, scrollpane[3]     , 1, 2, 1, 1, 1 ,1  );
} //createList

public static void main (String[] args) {
    new ScrollbarTest();        
} //main

} //end of class

How Can I dynamically fit the size my components?

Many Thanks!


Solution

  • When working with GridBagLayout, you always have to keep an eye on preferredSize of the child components. Add something like

    textPane[i].setPreferredSize(new Dimension(10, 15));
    

    to the for-loop in your constructor.

    Also, you're mixing up AWT with Swing - use more light-weight JComponent instead of Component.