简体   繁体   English

如何使JTable同时AutoResize和horizo​​ntall可滚动?

[英]How to make JTable both AutoResize and horizontall scrollable?

I am putting a JTable into a JScrollPane 我将JTable放入JScrollPane

But When I set JTable Auto Resizeable, then it won't have horizontal scroll bar. 但是当我设置JTable Auto Resizeable时,它将没有水平滚动条。

if I set AUTO_RESIZE_OFF, then the Jtable won't fill the width of its container when the column width is not big enough. 如果我设置AUTO_RESIZE_OFF,那么当列宽不够大时,Jtable将不会填充其容器的宽度。

So how can I do this: 那我该怎么做呢:

  1. when the table is not wide enough, expand to fill its container width 当桌子不够宽时,展开以填充其容器宽度
  2. when the table is wide enough, make it scrollable. 当表足够宽时,使其可滚动。

Thanks 谢谢

You need to customize the behaviour of the Scrollable interface. 您需要自定义Scrollable接口的行为。

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

public class TableHorizontal extends JFrame
{
    public TableHorizontal()
    {
        final JTable table = new JTable(10, 5)
        {
            public boolean getScrollableTracksViewportWidth()
            {
                return getPreferredSize().width < getParent().getWidth();
            }
        };
        table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
        final JScrollPane scrollPane = new JScrollPane( table );
        getContentPane().add( scrollPane );
    }

    public static void main(String[] args)
    {
        TableHorizontal frame = new TableHorizontal();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setSize(400, 300);
        frame.setVisible(true);
    }
}

The above code basically sizes the component at its preferred size or the viewport size, whichever is greater. 上面的代码基本上将组件的大小设置为其首选大小或视口大小,以较大者为准。

If for some reason customising JTable is not an option (eg it might be created in third-party code), you can achieve the same result by setting it to toggle between two different JTable AUTO_RESIZE modes whenever the containing viewport is resized, eg: 如果由于某种原因自定义JTable不是一个选项(例如,它可能是在第三方代码中创建的),则可以通过将其设置为在调整包含视口大小时在两个不同的JTable AUTO_RESIZE模式之间切换来实现相同的结果,例如:

jTable.getParent().addComponentListener(new ComponentAdapter() {
    @Override
    public void componentResized(final ComponentEvent e) {
        if (jTable.getPreferredSize().width < jTable.getParent().getWidth()) {
            jTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        } else {
            jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        }
    }
});

I found that all that is needed is to include 我发现所需要的只是包含

  table = new JTable(model);
  // this enables horizontal scroll bar
  table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );    

and then when the required viewport width and height have been calculated, include 然后在计算所需的视口宽度和高度时,包括

  frame.getContentPane().add(new JScrollPane(table))
  table.setPreferredScrollableViewportSize(new Dimension(width,height));

If you set the Layout of its container to BorderLayout with a BorderLayout.CENTER layout constraint, then the JTable will auto resize to fit its container. 如果使用BorderLayout.CENTER布局约束将其容器的布局设置为BorderLayout ,则JTable将自动调整大小以适合其容器。

If you want to make a component scrollable, you can wrap the JTable with a JScrollPane . 如果要使组件可滚动,可以使用JScrollPane包装JTable。

setLayout(new BorderLayout());
add(new JScrollPane(new JTable()), BorderLayout.CENTER);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM