JScrollPane中的JTable:禁用滚动条的最佳高度

时间:2012-12-20 11:22:31

标签: java swing jtable size jscrollpane

我使用以下代码在JTable内创建JScrollPane以显示列标题

JTable won't show column headers

String[] columnNames = {"header1", "header2", "header2", "header3"};
Object[][] data = new Object[num][4];
//feed values into data using for

JTable chart = new JTable(data, columnNames);
chart.setShowVerticalLines(false);
chart.setEnabled(false);
chart.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

JScrollPane sp = new JScrollPane(chart);
sp.setPreferredSize(new Dimension(width, chart.getHeight() + 5));
panel.add(sp);

问题在于我需要为JScrollPane计算高度,以便可以看到整个JTable并且不会显示JScrollBar。我怎么能这样做?

num从2更改为4,如果为4,则会显示滚动条。 width已修复。

3 个答案:

答案 0 :(得分:8)

基本方法是

  • JTable是一个Scrollable,遗憾的是在计算prefScrollable方面效果不佳,所以你必须自己动手
  • 要么使用一个LayoutManager,它在pref(f.i.FlowLayout)中列出所有内容,要么在JTable中实现max(如果你使用像BoxLayout这样的调整大小但最大的管理者)
  • JScrollPane是一个validationRoot,因此重新验证必须在scrollPane的父级上发生

类似的东西:

final JTable table = new JTable(10, 5) {

    @Override
    public Dimension getPreferredScrollableViewportSize() {
        Dimension dim = super.getPreferredScrollableViewportSize();
        // here we return the pref height
        dim.height = getPreferredSize().height;
        return dim;
    }

};
final JComponent content = new JPanel();
content.add(new JScrollPane(table));
Action add = new AbstractAction("add row") {

    @Override
    public void actionPerformed(ActionEvent e) {
        ((DefaultTableModel) table.getModel()).addRow(new Object[]{});
        content.revalidate();
    }
};

答案 1 :(得分:1)

将我的评论转换为答案,疯狂,疯狂,真的很疯狂,一切都可能使简单的事情复杂化,假设每个行都有相同的大小,列模型的长方法,扩展方法,必须添加列渲染器/编辑等。

enter image description here

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableColumn;

public class TablePreferredSize {

    private String[] head = {"One", "Two", "Three", "Four", "Five", "Six"};
    private String[][] data = new String[25][6];
    private JTable table = new JTable(data, head);
    private DefaultTableColumnModel columnModel = new DefaultTableColumnModel();
    private TableColumn column = new TableColumn();
    private int rowHeight = 23;
    private int rowWidth = 0;

    public TablePreferredSize() {
        table.setRowHeight(23);
        table.setIntercellSpacing(new Dimension(1, 1));
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        column = new TableColumn();
        column.setModelIndex(0);
        column.setHeaderValue("One");
        column.setPreferredWidth(250);
        columnModel.addColumn(column);
        rowWidth += column.getPreferredWidth();
        column = new TableColumn();
        column.setModelIndex(1);
        column.setHeaderValue("Two");
        column.setPreferredWidth(120);
        columnModel.addColumn(column);
        rowWidth += column.getPreferredWidth();
        column = new TableColumn();
        column.setModelIndex(2);
        column.setHeaderValue("Three");
        column.setPreferredWidth(80);
        columnModel.addColumn(column);
        rowWidth += column.getPreferredWidth();
        column = new TableColumn();
        column.setModelIndex(3);
        column.setHeaderValue("Four");
        column.setPreferredWidth(120);
        columnModel.addColumn(column);
        column = new TableColumn();
        column.setModelIndex(4);
        column.setHeaderValue("Five");
        column.setPreferredWidth(70);
        columnModel.addColumn(column);
        column = new TableColumn();
        column.setModelIndex(5);
        column.setHeaderValue("Six");
        column.setPreferredWidth(30);
        columnModel.addColumn(column);
        table.setColumnModel(columnModel);
        table.setPreferredScrollableViewportSize(new Dimension(rowWidth, 12 * rowHeight));
        JScrollPane scrollPane = new JScrollPane(table);
        JFrame frame = new JFrame("Table PreferredSize");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(scrollPane);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                TablePreferredSize t = new TablePreferredSize();
            }
        });
    }
}

答案 2 :(得分:0)

如果你打电话怎么办?

sp.getColumnHeader().getHeight()