如何在JTabbedPane中调整JTable的大小

时间:2014-09-26 08:25:00

标签: java swing jtable jpanel jtabbedpane

我想在JTabbedPane中调整JTable的大小,但我不知道如何做到这一点。目前我的JTab只显示默认大小的JTable。

enter image description here

目前我的代码是这样的

    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);

这是我的完整代码= My Code

2 个答案:

答案 0 :(得分:2)

固定尺寸​​是因为JPanel的默认布局是FlowLayout,它使用了表格的首选尺寸。相反,请使用填充可用空间的GridLayout之类的内容。

JPanel panel = new JPanel(new GridLayout());

答案 1 :(得分:1)

此代码可以帮助您设置表格的行高。

table.setRowHeight(anInteger);

此外,您知道使用此代码,您可以享受jtabel的愉快高度:

int scrollPaneHeight = scrollPane.getHeight();

所以你希望每一行都有这个高度:

int spaceForRows = scrollPaneHeight -heightOfheaderOfTabel;

float rowH =(float) spaceForRows / (rowData.length );  

但正如您所知,当您重新调整jframe的大小时,因此您的scrollPane和桌子不能全屏两次。所以你最常使用的是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);

}
相关问题