简体   繁体   English

JScrollPane(用于JTable)在MigLayout中填充并增长

[英]JScrollPane (for JTable) fill and grow in MigLayout

It is difficult to describe what happens so here is a SSCCE: 很难描述会发生什么,所以这里是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. 我希望当用户调整框架大小时,scrollPane使用所有可用空间。 But additionally I want another panel (placeHolder) to grow, too. 但是我还希望另一个面板(placeHolder)也能增长。 For example the heigth of the frame divided by 3, the placeHolder gets 1 part and the scrollPane gets 2 parts. 例如,框架的高度除以3,则placeHolder获得1的一部分,而scrollPane获得2的一部分。 The GridLayout is kinda doing what I need, but I'm looking for a solution with MigLayout. GridLayout可以满足我的需求,但是我正在寻找MigLayout的解决方案。

If you uncomment panel.add(placeHolder2, ""); 如果取消注释panel.add(placeHolder2, ""); and comment the line above it works. 并评论其上方的行。


EDIT1: EDIT1:
I tried using the sizegroup contraint. 我尝试使用sizegroup约束。 Now the placeHolder and the scrollPane have the same size, but I want the scrollPane take twice as much space. 现在placeHolder和scrollPane具有相同的大小,但是我希望scrollPane占用两倍的空间。 (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) (此外,在我的程序中,我没有这样的占位符,但是有一些标签,复选框和文本字段。因此,sizegroup可能不是我所需要的,除非有更多使用方式)

I found the solution here: 我在这里找到解决方案:
MigLayout confused by JTable objects contained in JScrollBar objects MigLayout与JScrollBar对象中包含的JTable对象混淆

and here: 和这里:
How to set JScrollPane to show only a sepecific amount of rows 如何将JScrollPane设置为仅显示单独的行

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. 我不知道这段代码片段之间有什么区别,但是它们都能满足我的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM