Search code examples
javaswingjtablejscrollpanemiglayout

JScrollPane (for JTable) fill and grow in MigLayout


It is difficult to describe what happens so here is a SSCCE:

public class TableInScrollPaneTest {


    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TableInScrollPaneTest();
            }
        });
    }

    public TableInScrollPaneTest() {

        JFrame frame = new JFrame();
        frame.setSize(300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        LayoutManager layout;

        layout = new MigLayout("wrap 1, fill", "fill, grow", "[fill, grow 1][fill, grow 2]");
        //layout = new GridLayout(2,1);

        JPanel panel = new JPanel(layout);


        JPanel placeHolder = new JPanel();
        JPanel placeHolder2 = new JPanel();
        placeHolder.setBackground(Color.GRAY);
        placeHolder2.setBackground(Color.LIGHT_GRAY);

        JTable table = this.createTable();
        JScrollPane tableScrollPane = new JScrollPane(table, 
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        //table.setPreferredScrollableViewportSize(new Dimension(5000,200));

        panel.add(placeHolder, "");
        panel.add(tableScrollPane, ""); 
        //panel.add(placeHolder2, "");

        frame.setContentPane(panel);
        frame.setVisible(true);
    }

    private JTable createTable() {

        String[] columnNames = "Name 1,Name 2,Name 3,Name 4,Name 5".split(",");

        int rows = 30;
        int cols = columnNames.length;
        String[][] data = new String[rows][cols];

        for(int i=0; i<rows; i++) {
            for(int j=0; j<cols; j++) {
                data[i][j] = "R"+i+" C"+j;
            }
        }
        JTable table = new JTable(data, columnNames);

        return table;
    }


}

I want the scrollPane to use all available space when the frame gets resized by the user. But additionally I want another panel (placeHolder) to grow, too. For example the heigth of the frame divided by 3, the placeHolder gets 1 part and the scrollPane gets 2 parts. The GridLayout is kinda doing what I need, but I'm looking for a solution with MigLayout.

If you uncomment panel.add(placeHolder2, ""); and comment the line above it works.


EDIT1:
I tried using the sizegroup contraint. Now the placeHolder and the scrollPane have the same size, but I want the scrollPane take twice as much space. (Additionally in my programm I dont have a placeholder like this, but rather there are some labels, checkboxes and textfields. So sizegroup probably is not what I need except there are more ways to use it)


Solution

  • I found the solution here:
    MigLayout confused by JTable objects contained in JScrollBar objects

    and here:
    How to set JScrollPane to show only a sepecific amount of rows

    table.setPreferredScrollableViewportSize(null);
    

    or

    table.setPreferredScrollableViewportSize(table.getPreferredSize());
    


    I don't know what's the difference between this code snippets, but they both do what I need.