我想调整JTable在JTabbedPane中的大小,但是我不知道如何做到这一点。目前我的JTab只显示默认大小的JTable。
目前我的代码是这样的
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setBounds(0, 0, 434, 262);
frame.getContentPane().add(tabbedPane);
JPanel panel = new JPanel();
panel.setLayout(null);
Object rowData[][] = {
{ "Row1-Column1", "Row1-Column2", "", "", "", "", "" },
{ "Row1-Column1", "Row1-Column2", "", "", "", "", "" },
{ "Row1-Column1", "Row1-Column2", "", "", "", "", "" },
{ "Row1-Column1", "Row1-Column2", "", "", "", "", "" },
{ "Row1-Column1", "Row1-Column2", "", "", "", "", "" },
{ "Row2-Column1", "Row2-Column2", "Row2-Column3", "", "", "",
"", "" } };
Object columnNames[] = { "Column One", "Column Two", "Column Three",
"", "", "", "" };
JTable table = new JTable(rowData, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
tabbedPane.addTab("table", null, scrollPane, null);
这是我的完整代码
固定大小是因为JPanel
的默认布局是FlowLayout
,它使用表的首选大小。相反,使用类似于GridLayout
的方法,这样可以填充可用空间。
JPanel panel = new JPanel(new GridLayout());
这段代码可以帮助您设置表的行高。
table.setRowHeight(anInteger);
此外,您知道,使用此代码,您可以获得令人愉悦的jtabel高度:
int scrollPaneHeight = scrollPane.getHeight();
所以您希望每一行都有这样的高度:
int spaceForRows = scrollPaneHeight -heightOfheaderOfTabel;
float rowH =(float) spaceForRows / (rowData.length );
但正如您所知,当您重新调整jframe的大小时,您的滚动窗格和表格不能两次全屏显示。因此,您最多使用componentListener。因此,当您的组件重新调整代码大小时,请计算rowH两次。完整的代码是
public static void main(String[] args) {
// TODO Auto-generated method stub
final JFrame frame = new JFrame();
final JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
frame.setContentPane(tabbedPane);
final Object rowData[][] = {
{ "Row1-Column1", "Row1-Column2", "", "", "", "", "" },
{ "Row1-Column1", "Row1-Column2", "", "", "", "", "" },
{ "Row1-Column1", "Row1-Column2", "", "", "", "", "" },
{ "Row1-Column1", "Row1-Column2", "", "", "", "", "" },
{ "Row1-Column1", "Row1-Column2", "", "", "", "", "" },
{ "Row2-Column1", "Row2-Column2", "Row2-Column3", "", "", "",
"", "" } };
Object columnNames[] = { "Column One", "Column Two", "Column Three",
"", "", "", "" };
final JTable table = new JTable(rowData, columnNames);
final JScrollPane scrollPane = new JScrollPane(table);
tabbedPane.addTab("table", null, scrollPane, null);
frame.setSize(new Dimension(500, 500));
tabbedPane.addComponentListener(new ComponentListener() {
@Override
public void componentShown(ComponentEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void componentResized(ComponentEvent arg0) {
// TODO Auto-generated method stub
int scrollPaneHeight = scrollPane.getHeight();
JTableHeader header = table.getTableHeader();
int heightOfheaderOfTabel = header.getHeight();
int spaceForRows = scrollPaneHeight - heightOfheaderOfTabel;
float rowH = (float) spaceForRows / (rowData.length);
table.setRowHeight((int) rowH);
}
@Override
public void componentMoved(ComponentEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void componentHidden(ComponentEvent arg0) {
// TODO Auto-generated method stub
}
});
frame.setVisible(true);
}