Java - 在JScrollPanes下面的约束组件,运行时更改

时间:2015-01-12 19:26:50

标签: java swing constraints jscrollpane springlayout

我有一个包含JTable的JScrollPane的JFrame。在JTable下面,我已经使用SpringLayout放置了一个JButton:

l.putConstraint(SpringLayout.NORTH, but, 50, SpringLayout.SOUTH, sP);

JButton向JTable添加一行并更新JScrollPane的大小。由于他们彼此约束,我希望按钮更新他的位置并向下移动。但是,它不会发生。
当我尝试使用普通JTable时,它工作得很好,但是我需要这个JScrollPane以避免JTable超出框架范围。

这是我的完整测试代码:

package tests;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SpringLayout;
import javax.swing.table.DefaultTableModel;

public class StackOverflowQRuntimeAlignment extends JFrame{

    private JScrollPane sP;
    private JTable tab;
    private JButton but;
    private DefaultTableModel dtm;

    public static void main(String[] args){
        StackOverflowQRuntimeAlignment frame = new StackOverflowQRuntimeAlignment("Test");
        frame.setSize(new Dimension(1280, 720));
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    StackOverflowQRuntimeAlignment(String title){
        super(title);
        //DefaultTabelModel to create the Table
        dtm = new DefaultTableModel(new String[][]{{"data1", "data2"}}, new String[]{"Column1", "Column2"});
        tab = new JTable(dtm);
        //set TableHeaderHeight to RowHeight
        tab.getTableHeader().setPreferredSize(new Dimension(tab.getWidth(), tab.getRowHeight()));
        //create ScrollPane containing the Table
        sP = new JScrollPane(tab);
        //set the size of the ScrollPane
        sP.setPreferredSize(new Dimension(500, (dtm.getRowCount()+1)*16+3));//+1 for the Header, +3 to hide ScrollBars if they are not neccesary
        sP.setMaximumSize(new Dimension(tab.getWidth(), 360));

        //create the button to add an row to the table
        but = new JButton("Hit me");
        but.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dtm.addRow(new String[]{"Banana", "Apple"});
                //updating the size of the ScrollPane
                sP.setPreferredSize(new Dimension(500, (dtm.getRowCount()+1)*16+3));
                sP.setSize(sP.getPreferredSize());
            }
        });

        //constraining the Components on the GUI
        SpringLayout l = new SpringLayout();
        this.setLayout(l);
        l.putConstraint(SpringLayout.HORIZONTAL_CENTER, sP, 0, SpringLayout.HORIZONTAL_CENTER, this.getContentPane());
        l.putConstraint(SpringLayout.NORTH, sP, 50, SpringLayout.NORTH, this.getContentPane());

        l.putConstraint(SpringLayout.HORIZONTAL_CENTER, but, 0, SpringLayout.HORIZONTAL_CENTER, sP);
        l.putConstraint(SpringLayout.NORTH, but, 50, SpringLayout.SOUTH, sP);

        //adding the Components to the GUI
        this.add(sP);
        this.add(but);
    }
}

1 个答案:

答案 0 :(得分:0)

使用JScrollPane的关键是你不能不断调整它的大小。随着添加到滚动窗格的组件的首选大小增加,滚动条将出现在滚动窗格上。因此,使用滚动窗格的默认首选大小创建GUI,并让Swing布局管理器工作。

通常,滚动窗格将添加到框架上BorderLayout的CENTER中,以便在用户调整框架大小时将空间分配给滚动窗格。

但是,从可见GUI添加(或删除)组件的一般规则是使用:

panel.add(...);
panel.revalidate(); // to invoke the layout manager
panel.repaint(); // to paint the components