1

我想将 aJTable放入 aJScrollPane并希望它:

  • 可水平和垂直滚动(仅当它的内容使其大于父容器时)
  • 如果需要,自动增长以填充父容器(双向)
  • 仅在需要时具有一个/两个滚动条

顺便说一句,JList行为完全一样。但是我发现没有简单的方法可以同时完成所有目标JTable。完成第一个的简单方法是禁用自动调整大小,但这会阻止第二个发生。我想出了一个非常 hacky 的解决方案,它将一个组件侦听器添加到JScrollPane并在调整该容器的大小时调整表格的大小。但是,我不得不在 resize 方法中添加一些“软糖因素”(见 4x 评论),因为我猜想存在一些固有的不准确性,所以我通过小像素量改变了我的高度/宽度检查。如果没有这些软糖因素,滚动条要么出现得太早(在需要它们之前),要么即使存在滚动条,长表格单元格的最后也会被截断。关于这些 hacky fudge 因素的最糟糕的部分是,如果您改变 GUI 的外观和感觉,那么整个解决方案将停止如此干净地工作(即 fudge 因素需要改变)。这显然与理想相去甚远。

必须有一种更清洁的方法来实现这一点。有人可以帮忙吗?运行附加的代码并拖动整个框架的大小以查看表格更新。这段代码使用了金属外观和感觉,这似乎可以在我的 Windows 7 机器上运行,并选择了软糖因素,但如果它在另一个操作系统上运行不那么干净,我不会感到惊讶。

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

public class JTableScrollTest {

    private JFrame frame;
    private JPanel panel;
    private DefaultTableModel tableModel;
    private JTable table;
    private JScrollPane scrollPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        } catch (Throwable e) {
            e.printStackTrace();
        }

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JTableScrollTest window = new JTableScrollTest();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public JTableScrollTest() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel = new JPanel();
        frame.getContentPane().add(panel, BorderLayout.CENTER);
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        scrollPane = new JScrollPane();
        panel.add(scrollPane);

        tableModel = new DefaultTableModel(new Object[]{"Stuff"},0);
        tableModel.addRow(new Object[]{"reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally long string"});
        tableModel.addRow(new Object[]{"a"});
        tableModel.addRow(new Object[]{"a"});
        tableModel.addRow(new Object[]{"a"});
        tableModel.addRow(new Object[]{"a"});
        tableModel.addRow(new Object[]{"a"});
        tableModel.addRow(new Object[]{"a"});
        tableModel.addRow(new Object[]{"a"});
        tableModel.addRow(new Object[]{"a"});
        tableModel.addRow(new Object[]{"a"});
        tableModel.addRow(new Object[]{"a"});
        tableModel.addRow(new Object[]{"a"});
        tableModel.addRow(new Object[]{"LAST ITEM"});
        table = new JTable(tableModel);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        scrollPane.setViewportView(table);

        resizeTable();
        panel.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent evt) {
                resizeTable();
            }
        });
    }

    public void resizeTable() {
        int width = 0;
        int height = 0;
        for (int row = 0; row < table.getRowCount(); row++) {
            TableCellRenderer renderer = table.getCellRenderer(row, 0);
            Component comp = table.prepareRenderer(renderer, row, 0);
            width = Math.max (comp.getPreferredSize().width, width);
            height += comp.getPreferredSize().height;
        }
        if (width > panel.getWidth() - 2) { // fudge factor
            width += 4; // fudge factor
            scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        } else {
            width = panel.getWidth();
            scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        }
        if (height > panel.getHeight() + 4) { // fudge factor
            height -= 26; // fudge factor
            scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        } else {
            height = panel.getHeight();
            scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        }
        table.setPreferredSize(new Dimension(width, height));
    }
}
4

1 回答 1

2

我遇到过同样的问题。该表只是使列更大或更小,而不是显示滚动条。通过反复试验,我想出的 hack 是ComponentListener您附加到ScrollPane.

诀窍是在滚动窗格小于表格的首选大小时关闭表格的自动调整大小。请注意,您不应直接设置表格的首选大小。不用管它自动计算。如果您需要更大或更小的某些列,请通过列模型(例如table.getColumnModel().getColumn(0).setMaxWidth(25))设置它们的宽度。

此类还包括另一个TableModelListener在将列添加到表时执行相同逻辑的类。

public final class ScrollingTableFix implements ComponentListener {
  private final JTable table;

  public ScrollingTableFix(JTable table, JScrollPane scrollPane) {
    assert table != null;

    this.table = table;

    table.getModel().addTableModelListener(new ColumnAddFix(table, scrollPane));
  }

  public void componentHidden(final ComponentEvent event) {}

  public void componentMoved(final ComponentEvent event) {}

  public void componentResized(final ComponentEvent event) {
    // turn off resize and let the scroll bar appear once the component is smaller than the table
    if (event.getComponent().getWidth() < table.getPreferredSize().getWidth()) {
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    }
    // otherwise resize new columns in the table
    else {
        table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    }
  }

  public void componentShown(final ComponentEvent event) {}

  // similar behavior is needed when columns are added to the table
  private static final class ColumnAddFix implements TableModelListener {
    private final JTable table;
    private final JScrollPane scrollPane;

    ColumnAddFix(JTable table, JScrollPane scrollPane) {
      this.table = table;
      this.scrollPane = scrollPane;
     }

    @Override
    public void tableChanged(TableModelEvent e) {
      if (e.getFirstRow() == TableModelEvent.HEADER_ROW) {
        if (scrollPane.getViewport().getWidth() < table.getPreferredSize().getWidth()) {
          table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        }
        else {
          table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        }
      }
     }
   }
}

要使用它,只需像这样连接所有东西:

JTable table = new JTable();
JScrollPane scroller = new JScrollPane(table);
scroller.addComponentListener(new ScrollingTableFix(table, scroller));
于 2013-01-18T00:23:30.933 回答