我在jTable
内添加了jScrollPane
作为:
public Scenario_One() {
initComponents();
jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
}
private void initComponents() {
.
.
jScrollPane1.setViewportView(jTable1);
.
.
}
显示在一行中的文字太大,所以我不得不使用水平滚动条。但即使将策略设置为as needed
,我也看不到水平滚动条。这可能是什么原因?
答案 0 :(得分:3)
正如@MadProgrammer已经评论过的那样,控制JScrollPane中的表大小调整行为的属性是autoResizeMode。基本上,有两种选择:
(直观)期望表“适合”视口直到列太宽而无法完全显示然后自动允许滚动在核心JTable中不支持。 JXTable(包含在SwingX project中)提供了额外的大小调整选项。
答案 1 :(得分:2)
尝试这样的事情......
public class TestTableColumns {
public static void main(String[] args) {
new TestTableColumns();
}
public TestTableColumns() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
DefaultTableModel model =new DefaultTableModel(
new Object[][] {
{"1", "2"},
{"11", "21"},
{"12", "22"},
{"13", "23"},
{"14", "24"},
{"15", "25"},
{"16", "26"},
{"17", "27"},
{"18", "28"}},
new Object[] {
"Small", "Big"});
JTable table = new JTable(model);
table.setShowHorizontalLines(true);
table.setShowVerticalLines(true);
table.setShowGrid(true);
table.setGridColor(Color.DARK_GRAY);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.getColumn("Small").setPreferredWidth(100);
table.getColumn("Small").setWidth(12);
table.getColumn("Big").setPreferredWidth(400);
table.getColumn("Big").setWidth(100);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
视口的宽度由其包含的组件的宽度决定。对于JTable
,这取决于列的大小和自动尺寸政策