Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 计算JTable的最宽列_Java_Mysql_Swing_Jtable_Autoresize - Fatal编程技术网

Java 计算JTable的最宽列

Java 计算JTable的最宽列,java,mysql,swing,jtable,autoresize,Java,Mysql,Swing,Jtable,Autoresize,我有一个JTable对象,它显示预先选择的MySQL表的所有内容。如您所知,此类提供了列自动调整大小等功能:我想创建一个过程,计算表中包含的所有单元格的宽度之和,如果结果小于其首选宽度,则打开自动调整大小模式。我一直在尝试遵循数百条在线指南,但我的程序仍然不起作用: public void setTableColumnPreferredWidth(javax.swing.JTable aTable) { int width = 0; int TotalWidth = 0;

我有一个
JTable
对象,它显示预先选择的MySQL表的所有内容。如您所知,此类提供了列自动调整大小等功能:我想创建一个过程,计算表中包含的所有单元格的宽度之和,如果结果小于其
首选宽度
,则打开自动调整大小模式。我一直在尝试遵循数百条在线指南,但我的程序仍然不起作用:

public void setTableColumnPreferredWidth(javax.swing.JTable aTable)
{
    int width = 0;
    int TotalWidth = 0;
    for (int row = 0; row < aTable.getRowCount(); row++)
    {
        for (int myColumn=0;myColumn < aTable.getColumnCount();myColumn++)
        {
            TableCellRenderer renderer = aTable.getCellRenderer(row, myColumn);
            Component comp = aTable.prepareRenderer(renderer, row, myColumn);
            width = Math.max (comp.getPreferredSize().width, width);
            aTable.getColumnModel().getColumn(myColumn).setPreferredWidth(width);
        }
    }

    for (int I=0;I<aTable.getColumnModel().getColumnCount();I++)
        TotalWidth += aTable.getColumnModel().getColumn(I).getPreferredWidth();

    int maximum = 0;

    if (MainTable.getAutoResizeMode() == MainTable.AUTO_RESIZE_ALL_COLUMNS)
        maximum = MainTable.getPreferredSize().width;
    else
    {
        int PreviousAutoresize = MainTable.getAutoResizeMode();
        MainTable.setAutoResizeMode(MainTable.AUTO_RESIZE_ALL_COLUMNS);
        maximum = MainTable.getPreferredSize().width;
        MainTable.setAutoResizeMode(PreviousAutoresize);
    }

    if (maximum < TotalWidth) 
        MainTable.setAutoResizeMode(MainTable.AUTO_RESIZE_ALL_COLUMNS);
    else 
        MainTable.setAutoResizeMode(MainTable.AUTO_RESIZE_OFF);
}
public void setTableColumnPreferredWidth(javax.swing.JTable aTable)
{
整数宽度=0;
int TotalWidth=0;
对于(int row=0;row
“自动调整大小”属性应基于显示表格的滚动窗格视口中的可用空间

这需要随着滚动窗格的大小变化而动态完成

您可以将
ComponentListener
添加到滚动窗格中,并侦听
componentResized
事件。然后执行计算:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SSCCE extends JPanel
{
    public SSCCE()
    {
        setLayout( new BorderLayout() );

        JTable table = new JTable(5, 5);
        JScrollPane scrollPane = new JScrollPane( table );
        add( scrollPane );

        scrollPane.addComponentListener( new ComponentAdapter()
        {
            @Override
            public void componentResized(ComponentEvent e)
            {
                JScrollPane scrollPane = (JScrollPane)e.getComponent();
                JTable table = ((JTable)scrollPane.getViewport().getView());

                int tableWidth = table.getPreferredSize().width;
                int viewportWidth = scrollPane.getViewport().getSize().width;

                if (tableWidth < viewportWidth)
                    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
                else
                    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new SSCCE());
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater( () -> createAndShowGUI() );
/*
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类SSCCE扩展了JPanel
{
公共服务
{
setLayout(新的BorderLayout());
JTable table=新的JTable(5,5);
JScrollPane scrollPane=新的JScrollPane(表);
添加(滚动窗格);
scrollPane.addComponentListener(新的ComponentAdapter()
{
@凌驾
公共无效组件已恢复(组件事件e)
{
JScrollPane scrollPane=(JScrollPane)e.getComponent();
JTable table=((JTable)scrollPane.getViewport().getView());
int tableWidth=table.getPreferredSize().width;
int viewportWidth=scrollPane.getViewport().getSize().width;
如果(tableWidthcreateAndShowGUI());
/*
invokeLater(新的Runnable()
{
公开募捐
{
createAndShowGUI();
}
});
*/
}
}

或者另一个选项是覆盖JTable的一些默认布局处理,如所示:

现在,您的代码在确定最大列的宽度之前正在更改列的宽度,我不确定这是否是您想要做的。它用于在不更改的情况下检索表列的宽度。您是指自动调整大小模式处理?此
aTable.getColumnModel().getColumn(myColumn).setPreferredWidth(width);
更改列的宽度,在此之前,您有此
width=Math.max(comp.getPreferredSize().width,width);
我猜它是用来确定最大列的宽度的。如果是这样的话,您需要先确定最大宽度,然后再将所有列的宽度更改为该值。