Search code examples
javaswingjlistjoptionpane

Resize JOptionDialog to fit the contents inside it


I want to create a dialog box for my project. The Dialog box should consist of two buttons and a list. For this I created a new class with contents as as:

ContentPane/ScrollPane/JList

With following code:

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

I plan to call this class using JOptionDialog from another class as follows:

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

This code works well but the result appears something as:

enter image description here

As can be seen, it has quite an amount of blank space which I would like to be removed. I have tried to manually set size by:

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

But this doesnt really seem to work. So any help is appreciated.


Solution

  • The height can be fixed by calling JList.setVisibleRowCount(int). The width is (typically) better left to the natural size, rather than the guess work inherent in suggesting a preferred size in the layout.

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