当容器大小更改时,JTable仅调整选定列的大小 [英] JTable resize only selected column when container size changes

查看:101
本文介绍了当容器大小更改时,JTable仅调整选定列的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于面板内部的JTable,如果面板变大,我怎样才能将额外空间分配给某些列(在我的情况下,到最后一列,尽管可能很好提供第3列, 4和8将获得额外的空间。

For a JTable that's inside a panel, if the panel is made larger, how can I distribute the extra space to only certain columns (in my case, to the last column, though it may be nice to offer "columns 3,4, and 8 will get the extra space).

我想允许用户手动更改所有列的列大小。

I want to allow the user to change column sizes for all the columns manually.

我尝试了table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
将额外空间分配给调整JTable大小时可调整大小的所有列。在JavaDoc for JTable中,用于doLayout( ),说:

I tried table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); That distributes the extra space to all the columns that are resizeable when the JTable is resized. In the the JavaDoc for JTable, for doLayout(), says:


当调整封闭窗口大小调整方法时,resizingColumn为null。这意味着调整大小已发生在JTable的外部,并且无论此JTable的自动调整大小模式如何,更改 - 或delta - 都应分发到所有列。

When the method is called as a result of the resizing of an enclosing window, the resizingColumn is null. This means that resizing has taken place "outside" the JTable and the change - or "delta" - should be distributed to all of the columns regardless of this JTable's automatic resize mode.

可能有办法拦截JTable是resiz 事件,并做一些自定义的事情,虽然我无法弄清楚。

There may be a way to intercept a "JTable is resizing" event, and do something custom, though I haven't been able to figure it out.

另一个解决方案是在
如何在JTable中排除自动调整大小的列
虽然它并没有真正告诉我如何在摇摆中这样做。

Another solution is presented in How to exclude columns from auto-resizing in a JTable Though it doesn't really tell me how to do it in swing.

推荐答案

覆盖 doLayout()拦截布局的方法。这给出了基础知识。当最后一列宽度即将消极时,您需要确定要执行的操作。

Override the doLayout() method to intercept the layout. This gives the basics. You will need to determine what you want to do when the last column width is about to go negative.

import javax.swing.*;
import javax.swing.table.*;

public class Test2Table
{
    private static Object[][] data = new Object[][]
    {
            { "a", "b", "c" },
            { "d", "e", "f" }
    };
    private static Object[] colNames = new Object[] { "1", "2", "3" };

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run() {
                JTable table = new JTable(data, colNames)
                {
                    @Override
                    public void doLayout()
                    {
                        //  Viewport size changed. Change last column width

                        if (tableHeader != null
                        &&  tableHeader.getResizingColumn() == null)
                        {
                            TableColumnModel tcm = getColumnModel();
                            int delta = getParent().getWidth() - tcm.getTotalColumnWidth();
                            TableColumn last = tcm.getColumn(tcm.getColumnCount() - 1);
                            last.setPreferredWidth(last.getPreferredWidth() + delta);
                            last.setWidth(last.getPreferredWidth());
                        }
                        else
                        {
                            super.doLayout();
                        }
                    }

                };

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

这篇关于当容器大小更改时,JTable仅调整选定列的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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