简体   繁体   English

如果表大小小于设置值,则Java将JDialog适配到JTable

[英]Java fit JDialog to JTable if table size is less than a set value

I have a JDialog with two panels: a JTable and a button. 我有一个带有两个面板的JDialog:一个JTable和一个按钮。 I want to set JDialog so that ( maxHeight= 600 pixels ): 我想将JDialog设置为( maxHeight= 600 pixels ):

(1) If table height is less than maxLength it shows table fully with no blank space, but all rows fit in. (1)如果表格高度小于maxLength它将完全显示表格,没有空格,但是所有行都适合。

(2) If the height is greater than maxHeight it restricts the height to maxHeight and shows scrollbar on the side. (2)如果高度大于maxHeight ,则将高度限制为maxHeight并在侧面显示滚动条。

So far I have most of the JDialog working, but I can't figure out how to control the height of the JTable. 到目前为止,我已经完成了大多数JDialog的工作,但是我不知道如何控制JTable的高度。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class TestGUI {

    public static void main(String[] args) {        
        int a=10;        

        Object[][] data = new Object[a][2];       //Fill table with some data 
        for (int i = 0; i < a; i++) {data[i][0]=i; data[i][1]=i*i; }        

        JDialog dialog = new JDialog();
        dialog.setLayout(new BorderLayout());

        String[] header = {"Number", "Square"};        
        DefaultTableModel model = new DefaultTableModel(data, header);
        JTable table = new JTable(model);

        table.setPreferredScrollableViewportSize(new Dimension(450, 600));        

        table.setFillsViewportHeight(true);
        JScrollPane js = new JScrollPane(table);
        js.setVisible(true);

        dialog.add(js, BorderLayout.CENTER);

        JButton okButton = new JButton(new AbstractAction("OK") {
            private static final long serialVersionUID = 1L;
            public void actionPerformed(ActionEvent e) {
                dialog.setVisible(false);
            } 
        });
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(okButton);
        dialog.add(buttonPanel, BorderLayout.SOUTH);
        //dialog.setSize(new Dimension(300, 600));
        dialog.setMinimumSize(new Dimension(255, 175));
        dialog.setVisible(true);
    }
}

Hard-coded pixel sizes are almost never a good idea. 硬编码像素大小几乎从来都不是一个好主意。 Other users won't have the same fonts as you, and even those who do may be using different font sizes. 其他用户将不会使用与您相同的字体,即使那些用户也可能使用不同的字体大小。 (A 12 point font is a font whose lines of text are 1272 inch high, so even if the user has the same font, the dot pitch of the user's monitor may affect how many pixels each character uses.) (A 12点的字体是一种字体其文本的行72分之12英寸高,因此,即使用户具有相同的字体,用户的显示器的点距可能影响每个字符多少像素使用。)

Instead of maxHeight, define a maxVisibleRows variable. 而不是maxHeight,定义一个maxVisibleRows变量。 Your table should use getRowHeight to compute its size based on the desired number of rows: 您的表应使用getRowHeight根据所需的行数来计算其大小:

int visibleRows = model.getRowCount();
visibleRows = Math.max(1, visibleRows);
visibleRows = Math.min(maxVisibleRows, visibleRows);

Dimension size = table.getPreferredScrollableViewportSize();
size.height = table.getRowHeight() * visibleRows;
table.setPreferredScrollableViewportSize(size);

Now your JTable, and its enclosing JScrollPane, will have a preferred size consistent with what I think you're seeking, which means you can (and should) use pack(). 现在,您的JTable及其封闭的JScrollPane将具有与我认为要查找的一致的首选大小,这意味着您可以(并且应该)使用pack()。

dialog.pack();
dialog.setResizable(false);

If your table may potentially have a lot of rows, you should consider avoiding any restrictions on the size of the JDialog. 如果表中可能有很多行,则应考虑避免对JDialog的大小进行任何限制。 If there are hundreds of rows, users will not appreciate being forced to look at only eight at a time. 如果有数百行,用户将不会欣赏被迫一次只查看八行。

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

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