JOptionPane内的JPanel内的JTable不会按需调整大小

时间:2013-05-28 21:22:01

标签: java swing jtable jpanel joptionpane

我在JTable内有两个JPanel个对象GridLayout。我将它们放在JOptionPane中,然后将其显示为OK_CANCEL弹出窗口。我还在两个表上放了一个JScrollPane

然而,JOptionPane的大小是巨大的。我尝试使用:

设置不同的表格,滚动窗格和jpanel尺寸
table.setSize(int w, int h)
jpanel.setSize(int w, int h)
jscrollpane.setSize(int w, int h)

但这些都不会导致较小的JOptionPane(或表格)。

这是它的外观,我使用1366 * 768作为分辨率。上面提到的都没有任何区别

This is how it looks

private void showEditItemSuppliersDialog()
    {
    String newItemSupplierTables [] = { "#", "Name", "" };
        JPanel panel = new JPanel(new GridLayout(0, 2, 5, 5));

        /* table 1 */
        allItemsEditItemSuppliersTableModel = new DefaultTableModel(null, newItemSupplierTables);

        allItemsEditItemSuppliersTable = new JTable(allItemsEditItemSuppliersTableModel);
        allItemsEditItemSuppliersTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        JScrollPane allItemsEditItemSuppliersTableScrollPane = new JScrollPane();
        allItemsEditItemSuppliersTableScrollPane.setViewportView(allItemsEditItemSuppliersTable);
        /* table 1 end */

        /* table 2 */
        allItemsEditItemSuppliersAllTableModel = new DefaultTableModel(null, newItemSupplierTables);

        allItemsEditItemSuppliersAllTable = new JTable(allItemsEditItemSuppliersAllTableModel);
        allItemsEditItemSuppliersAllTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        JScrollPane allItemsEditItemSuppliersAllTableScrollPane = new JScrollPane();
        allItemsEditItemSuppliersAllTableScrollPane.setViewportView(allItemsEditItemSuppliersAllTable);
        /* table 2 end*/

        panel.add(allItemsEditItemSuppliersTableScrollPane);
        panel.add(allItemsEditItemSuppliersAllTableScrollPane);

        int option = JOptionPane.showConfirmDialog(null, panel, "Edit", JOptionPane.OK_CANCEL_OPTION);

        if (option == JOptionPane.YES_OPTION)
        {   
            System.out.println("Pressed OK");
        }
    }

1 个答案:

答案 0 :(得分:2)

您应该使用setPreferredScrollableViewportSize(Dimension)设置表格的首选视口大小。