【问题标题】:JTable inside a JPanel inside a JOptionPane won't resize as wantedJOptionPane 内的 JPanel 内的 JTable 不会根据需要调整大小
【发布时间】:2013-05-28 21:22:01
【问题描述】:

我在JPanelGridLayout 中有两个JTable 对象。我将它们放在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 作为分辨率。上面提到的都不会有任何区别

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

【问题讨论】:

    标签: java swing jtable jpanel joptionpane


    【解决方案1】:

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

    【讨论】:

    • 谢谢,只要我能接受答案,我就会接受,因为这正是我所需要的。
    猜你喜欢
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 2011-05-17
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    • 2020-03-18
    • 2012-02-09
    相关资源
    最近更新 更多