Java JTable检测列由用户重新调整大小 [英] Java JTable detect Column re-sized by user

查看:172
本文介绍了Java JTable检测列由用户重新调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 TableColumnModelListener()的JTable来检测列何时重新调整大小并且我想要在<$中执行一些代码c $ c> columnMarginChanged()方法。

I have a JTable that is using a TableColumnModelListener() to detect when the column has been re-sized and I have some code I want to execute in the columnMarginChanged() method.

如何确定列是由用户重新调整大小还是由于其他代码?

How do I determine whether the column was re-sized by the user or as a result of other code?

我想我必须从 ChangeEvent.getSource()开始,但我不知道从那里去哪里。

I am thinking I have to start with ChangeEvent.getSource() but I don't know where to go from there.

谢谢。

推荐答案

我可以给你一个可能的方法。我试图解决同样的问题,因为我想将有关列宽的信息序列化到磁盘,以便下次在我的应用程序中打开表时,我可以适当地恢复列宽。接下来是:

I can give you one possible approach. I was trying to solve the same problem, because I wanted to serialize information about column widths to disk, so that the next time the table opened up in my application, I could restore the column widths appropriately. Here goes:

第1步 - 覆盖你的JTable并为其添加一个布尔属性

class MyTable extends JTable {

    private boolean isColumnWidthChanged;
    public boolean getColumnWidthChanged() {
        return isColumnWidthChanged;
    }

    public void setColumnWidthChanged(boolean widthChanged) {
        isColumnWidthChanged = widthChanged;
    }

}

第2步 - 添加表格的TableColumnModelListener()

private class TableColumnWidthListener implements TableColumnModelListener
{
    @Override
    public void columnMarginChanged(ChangeEvent e)
    {
        /* columnMarginChanged is called continuously as the column width is changed
           by dragging. Therefore, execute code below ONLY if we are not already
           aware of the column width having changed */
        if(!tableObj.hasColumnWidthChanged())
        {
            /* the condition  below will NOT be true if
               the column width is being changed by code. */
            if(tableObj.getTableHeader.getResizingColumn() != null)
            {
                // User must have dragged column and changed width
                tableObj.setColumnWidthChanged(true);
            }
        }
    }

    @Override
    public void columnMoved(TableColumnModelEvent e) { }

    @Override
    public void columnAdded(TableColumnModelEvent e) { }

    @Override
    public void columnRemoved(TableColumnModelEvent e) { }

    @Override
    public void columnSelectionChanged(ListSelectionEvent e) { }
}

步骤3 - 添加鼠标表头的监听器

private class TableHeaderMouseListener extends MouseAdapter
{
    @Override
    public void mouseReleased(MouseEvent e)
    {
        /* On mouse release, check if column width has changed */
        if(tableObj.getColumnWidthChanged())
        {
            // Do whatever you need to do here

            // Reset the flag on the table.
            tableObj.setColumnWidthChanged(false);
        }
    }
}

注意:在我的应用程序中,TableHeaderMouseListener和TableColumnWidthListener类是我的主应用程序类的私有内部类。我的主要应用程序类保留了被观察表的参考。因此,这些内部类可以访问表实例。显然,根据您的设置,您需要做适当的事情以使表实例可用于这些其他类。希望这有帮助!

NOTE: In my application, the TableHeaderMouseListener and TableColumnWidthListener classes were private inner classes of my main application class. My main application class held on to a reference of the table being observed. Therefore, these inner classes had access to the table instance. Obviously, depending on your setup, you need to do the appropriate thing to make the table instance available to these other classes. Hope this helps!

这篇关于Java JTable检测列由用户重新调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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