当我调整 JFrame 大小时,如何防止 JTable 的列调整大小?

发布于 2024-09-11 14:12:51 字数 620 浏览 1 评论 0原文

此代码将 JTable 放入 JFrame(整个 UI 的唯一组件)中:

    JFrame frame = new JFrame( "Title");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTable table = appender.createTable();
    JScrollPane scrollPane = new JScrollPane(table);
    table.setFillsViewportHeight(true);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);

    frame.pack();
    frame.setVisible(true);

我还有一些代码来设置列的首选大小。

没什么花哨的。当用户界面打开时,表格占据了整个视图,并且没有滚动条。柱子的组合首选宽度需要比窗户宽更多的水平空间。尽管如此,没有水平滚动条,而且列太窄。

我该怎么办 列

  1. 仍然可以由用户调整大小
  2. 当前宽度受到尊重?即,当我更改一列的宽度时,其他列的宽度不应更改。

This code puts a JTable into a JFrame (sole component of the whole UI):

    JFrame frame = new JFrame( "Title");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTable table = appender.createTable();
    JScrollPane scrollPane = new JScrollPane(table);
    table.setFillsViewportHeight(true);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);

    frame.pack();
    frame.setVisible(true);

I also have some code to set the preferred size of the columns.

Nothing fancy. When the UI opens, the table takes up the whole view and I have no scroll bars. The combined preferred widths of the columns need more horizontal space than the windows is wide. Despite of that, there is no horizontal scroll bar and the columns are too narrow.

What do I have to do that

  1. The columns are still resizable by the user
  2. The current width is respected? I.e. when I change the width of one column, the width of the other columns should not change.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

调妓 2024-09-18 14:12:51
table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );

您还可以将包含表格的滚动窗格添加到 JPanel,然后将该面板添加到框架。这样面板的大小就会改变,而不是滚动窗格的大小。

table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );

You can also add the scrollpane containing the table to a JPanel and then add the panel to the frame. That way the panel will change in size, not the scrollpane.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文