我用下面的代码创建JTable
内JScrollPane
显示的列标题
JTable中不会显示列标题
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
是固定的。
基本方法是
- JTable的是,一个滚动的不幸并没有在计算prefScrollable搞得太清楚,所以你要自己做
- 既可以使用其中规定了所有在其PREF(FI的FlowLayout)一个LayoutManager,或在JTable中实现最大值(如果你使用一个大小调整,但像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();
}
};
在这里将我的意见的回答,疯了,疯了,真的疯了,一切都可能是复杂的简单的事情,通过假定每行已得到了相同的尺寸,长方法columnmodel,拓展方法,必须添加列渲染器/编辑器,等等..
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();
}
});
}
}
如果你叫什么?
sp.getColumnHeader().getHeight()