Pergunta

I am writing a program that brings up a JDialog box that lists multiple options from a config file. The number of options can vary each time it is opened, so I need to be able to dynamically adjust the height of the window, but not the width. The window looks best using FlowLayout and defining the width of the window so that the JPanels that the data is in wrap propertly. But I am unable to dynamically adjust the height. I tried to use the pack method, but it lines all the panels up in one line like FlowLayout is meant to be. I tried GridLayout but it re-sizes all of the panels to the same size and is an unacceptable look. I also tried BoxLayout but was unable to get that one to work.

Is there a better layout manager to use or a way to make the width static?

Here is my code. Every panel and box is defined above the constructor, I just did not copy that code:

public ReSizeMe()
    {
        curConfig = new Config();
        config = curConfig.getConfig();

        setBox = new JDialog();

        setBox.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);  //  Set as 480, 600
        setBox.setLayout(new FlowLayout());

        this.buildSetFrame();

        //setBox.pack();
        setBox.setVisible(true);
    }

    public void buildSetFrame()
    {
        intPanel   = new JPanel();
        butPanel   = new JPanel();
        optPanel   = new JPanel();
        editPanel  = new JPanel[maxOptions];
        optWrapper = new JPanel[maxOptions];

        intPanel.setLayout(new FlowLayout());
        optPanel.setLayout(new GridLayout(10, 1));   //  trying different things here too.

        optText = new JTextField[maxOptions];
        editButton = new JButton[maxOptions];
        delButton = new JButton[maxOptions];

        intPanel.setBorder(BorderFactory.createTitledBorder("Modify Interface"));

        apply  = new JButton("Apply");
        newOpt = new JButton("New Option");
        help   = new JButton("Help");
        close  = new JButton("Close");

        intPanel.add(ethIntLabel);
        intPanel.add(ethIntName);

        butPanel.add(apply);
        butPanel.add(newOpt);
        butPanel.add(close);

        ethIntName.setText(config.getProperty("EthIntName"));

        setBox.add(welcomeMsg);
        setBox.add(intPanel);
        setBox.add(optPanel);

        buildOptions();

        setBox.add(butPanel);
    }

    void buildOptions()
    {
        for (int i = 0; i < maxOptions; i++)
        {
            editable = Boolean.parseBoolean(config.getProperty("option." + i + ".edit"));

            if (config.getProperty("option." + i + ".name") == null)
            {
                break;
            }
            else if (editable != false && 
                     config.getProperty("option." + i + ".name") != null && 
                     !config.getProperty("option." + i + ".name").isEmpty())
            {

                editPanel[i]  = new JPanel();
                optWrapper[i] = new JPanel();
                optText[i]    = new JTextField(20);
                editButton[i] = new JButton("Edit");
                delButton[i]  = new JButton("Delete");

                editButton[i].setActionCommand(Integer.toString(i));
                delButton[i].setActionCommand(Integer.toString(i));

                optText[i].setText(config.getProperty("option." + i + ".name"));
                optText[i].setEditable(false);
                editPanel[i].add(editButton[i]);
                editPanel[i].add(delButton[i]);
                optWrapper[i].add(optText[i]);
                optWrapper[i].add(editPanel[i]);
                optPanel.add(optWrapper[i]);
            }
        } 
    }
Foi útil?

Solução

Yes, Box should work wonderful:

Box box = Box.createVerticalBox();
box.add(...)
box.add(Box.createVerticalStrut(5)); // spacing
<etc.>

add(box);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top