如何让 JTable 水平扩展到 JPanel 中的视口之外
How do you get JTable to extend horizontally beyond the viewport in a JPanel
我在具有 46 列的 JPanel 中有一个 JTable。问题是您无法读取列的 headers ,更不用说任何后续行了。我无法轻松找到一种方法将 table 扩展到视口之外,这将允许水平滚动条。这是我用来设置 table 及其视口的代码。
private void setupReferencePanel() {
refTable = new JTable(null, makeHeaderVector());
refTable.addMouseListener(new TableListener(presenter));
refListingPanel = new JPanel();
refListingPanel.setLayout(new BorderLayout());
refListingPanel.setBorder(BorderFactory.createTitledBorder("Reference File"));
refScroller = new JScrollPane(refTable);
refScroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
refTable.setFillsViewportHeight(true);
//refTable.setFillsViewportWidth(true);
refListingPanel.add(refScroller, BorderLayout.CENTER);
refCountLbl = new JLabel("item count: 0");
refSelect = new SelectionPanel("Selection");
refListingPanel.add(refSelect, BorderLayout.PAGE_START);
refListingPanel.add(refCountLbl, BorderLayout.PAGE_END);
refTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
getContentPane().add(refListingPanel);
}
private Vector makeHeaderVector() {
String[] cols = { ... }; // hidden array of string contents
Vector<String> results = new Vector<String>(Arrays.asList(cols));
return results;
}
我应该设置
refTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF)
而不是
refTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
稍后在文档中,常量 AUTO_RESIZE_OFF 定义为 'Do not adjust column widths automatically; use a horizontal scrollbar instead.'
我在具有 46 列的 JPanel 中有一个 JTable。问题是您无法读取列的 headers ,更不用说任何后续行了。我无法轻松找到一种方法将 table 扩展到视口之外,这将允许水平滚动条。这是我用来设置 table 及其视口的代码。
private void setupReferencePanel() {
refTable = new JTable(null, makeHeaderVector());
refTable.addMouseListener(new TableListener(presenter));
refListingPanel = new JPanel();
refListingPanel.setLayout(new BorderLayout());
refListingPanel.setBorder(BorderFactory.createTitledBorder("Reference File"));
refScroller = new JScrollPane(refTable);
refScroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
refTable.setFillsViewportHeight(true);
//refTable.setFillsViewportWidth(true);
refListingPanel.add(refScroller, BorderLayout.CENTER);
refCountLbl = new JLabel("item count: 0");
refSelect = new SelectionPanel("Selection");
refListingPanel.add(refSelect, BorderLayout.PAGE_START);
refListingPanel.add(refCountLbl, BorderLayout.PAGE_END);
refTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
getContentPane().add(refListingPanel);
}
private Vector makeHeaderVector() {
String[] cols = { ... }; // hidden array of string contents
Vector<String> results = new Vector<String>(Arrays.asList(cols));
return results;
}
我应该设置
refTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF)
而不是
refTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
稍后在文档中,常量 AUTO_RESIZE_OFF 定义为 'Do not adjust column widths automatically; use a horizontal scrollbar instead.'