JTable自动调整为JScrollPane的高度和宽度 [英] JTable auto resizing to JScrollPane height and width

查看:106
本文介绍了JTable自动调整为JScrollPane的高度和宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在互联网上阅读以尝试找到解决我的问题的方法.谁能帮我.当没有足够的行显示滚动条时,如何自动调整Jtable的高度以适合父容器.谢谢

I have been reading all over the internet to try to find a solution to my problem. Can anyone help me. How can I auto resize Jtable's height to fit parent container when there aren't enough rows for scrollbar to show. thanks

更新:显示我的问题的代码,以及建议的解决方案如何无济于事.

UPDATE: code that shows my problem, and how suggested solution is not helping.thanks

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTable;


public class t extends JFrame {

    private JPanel contentPane;
    private JTable table;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    t frame = new t();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public t() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout(0, 0));

        JScrollPane scrollPane = new JScrollPane();
        contentPane.add(scrollPane, BorderLayout.CENTER);

        table = new JTable(10,10);
        table. setFillsViewportHeight( true );
        scrollPane.setViewportView(table);
    }

}

推荐答案

当没有足够的行显示滚动条时,如何自动调整Jtable的高度以适合父容器.

How can I auto resize Jtable's height to fit parent container when there aren't enough rows for scrollbar to show.

我认为您正在寻找:

table. setFillsViewportHeight( true );

但似乎不起作用...

but doesn't seem to work...

在您的示例中添加以下代码行:

Add the following lines of code to your example:

contentPane.setBackground(Color.RED);
scrollPane.getViewport().setBackground(Color.BLUE);

,您将看到一个红色边框.

and you will see a red border.

然后删除:

table. setFillsViewportHeight( true );

,您将看到红色和蓝色,表明桌子的高度最初是增加的.

and you will see red and blue, indicating that the table height was originally increased.

如果您期望看到涂有空白单元格,则它将无法正常工作.该表只能绘制模型中的内容.如果要绘制更多的数据行,则需要向模型添加更多的数据行.如果高度降低,则需要从模型中删除数据.

If you are expecting to see empty cells painted then it won't work. The table can only paint what is in the model. If you want more rows of data painted, then you need to add more rows of data to the model. And if the height decreases you need to remove data from the model.

当然,现在您需要跟踪哪些是有效"数据行,哪些是填充"数据行.因此,您将需要创建一个自定义TableModel来管理它.然后,您将ComponentListener添加到表中以处理表中的高度变化,然后对TableModel进行操作.

Of course now you need to track which is "valid" rows of data and which is "filler" rows of data. So you would need to create a custom TableModel to manage this. You would then add a ComponentListener to the table to handle the height changes in the table and then do your manipulation of the TableModel.

这篇关于JTable自动调整为JScrollPane的高度和宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆