调整JOptionDialog的大小以适应其中的内容

时间:2015-07-28 09:34:40

标签: java swing jlist joptionpane

我想为我的项目创建一个对话框。对话框应包含两个按钮和一个列表。为此我创建了一个新类,其内容如下:

ContentPane/ScrollPane/JList

使用以下代码:

import javax.swing.AbstractListModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class testDialog extends JPanel {
    private JScrollPane scrollPane;
    private JList list;

    public testDialog() {
        GroupLayout PanelMain = new GroupLayout(this);
        PanelMain.setHorizontalGroup(
            PanelMain.createParallelGroup(Alignment.LEADING)
                .addGroup(PanelMain.createSequentialGroup()
                    .addComponent(getScrollPane(), GroupLayout.PREFERRED_SIZE, 181, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(21, Short.MAX_VALUE))
        );
        PanelMain.setVerticalGroup(
            PanelMain.createParallelGroup(Alignment.LEADING)
                .addComponent(getScrollPane(), GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE)
        );
        setLayout(PanelMain);
    }

    private JScrollPane getScrollPane() {
        if (scrollPane == null) {
            scrollPane = new JScrollPane();
            scrollPane.setViewportView(getList());
        }
        return scrollPane;
    }
    private JList getList() {
            list = new JList();
            list.setModel(new AbstractListModel() {
                String[] values = new String[] {"data1", "data2", "data3", "data4"};
                public int getSize() {
                    return values.length;
                }
                public Object getElementAt(int index) {
                    return values[index];
                }
            });
        return list;
    }
}

我打算使用另一个类中的JOptionDialog来调用此类,如下所示:

import javax.swing.JOptionPane;

public class test2 {

    public static void main(String[] args) {
                try {
                    Object[] asd = {"cancel","ok"};
                    JOptionPane.showOptionDialog(null, testDialog.class.newInstance(),"testDialog",JOptionPane.YES_NO_OPTION,JOptionPane.PLAIN_MESSAGE, null,asd,asd[1]);           
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    }

此代码效果很好但结果显示为:

enter image description here

可以看出,它有相当多的空白空间,我希望将其删除。我试图通过以下方式手动设置大小:

JOptionPane op = new JOptionPane();
op.setSize(400, 400);
op.showOptionDialog(null, testDialog.class.newInstance(),"testDialog",JOptionPane.YES_NO_OPTION,JOptionPane.PLAIN_MESSAGE, null,asd,asd[1]);

但这似乎并不合适。所以任何帮助都表示赞赏。

1 个答案:

答案 0 :(得分:1)

可以通过调用JList.setVisibleRowCount(int)来修复高度。宽度(通常)更好地留给自然尺寸,而不是在布局中建议首选尺寸所固有的猜测工作。

enter image description here

import javax.swing.*;

public class WellFitList {

    WellFitList() {
        initUI();
    }

    public void initUI() {
        String[] data = {"Data 1", "Data 2", "Data 3", "Data 4"};
        JList list = new JList(data);
        list.setVisibleRowCount(data.length);

        JOptionPane.showMessageDialog(null, new JScrollPane(list));
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                WellFitList o = new WellFitList();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}