gpt4 book ai didi

java - 计算 JTable 的最宽列

转载 作者:行者123 更新时间:2023-11-28 23:33:39 24 4
gpt4 key购买 nike

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

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);
}

感谢您的关注。

最佳答案

i would like to create a procedure that calculates the sum of the width of all the cells contained inside the JTable and turns on the AUTO RESIZE mode if the result is minor than its preferredWidth

自动调整大小属性应基于显示表格的滚动 Pane 的视口(viewport)中的可用空间。

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

您可以向滚动 Pane 添加一个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();
}
});
*/
}
}

或者另一种选择是覆盖 JTable 的一些默认布局处理,如以下所示:JTable resize only selected column when container size changes

关于java - 计算 JTable 的最宽列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36475772/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com