whitewolfpgh
whitewolfpgh

Reputation: 451

resizing JDialog box

I'm having trouble with a dialog I created. It packs everything in cutting off border titles and input boxes. I've tried setting the size of the panel and of the components, but to no avail; size never changes. Any help would be appreciated in being able to modify the dimensions of the dialog.

JTextField account = new JTextField(6);
account.setDocument(new JTextFieldLimit(6));
account.setBorder(new TitledBorder("account"));

String[] firstDigitList = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
JComboBox firstDigitCombo = new JComboBox(firstDigitList);
firstDigitCombo.setSelectedIndex(0);
firstDigitCombo.setBorder(new TitledBorder("Leading Digit Change"));

JPanel panel = new JPanel();
panel.add(account);
panel.add(firstDigitCombo);

int result = JOptionPane.showConfirmDialog(null, panel, "Please Enter Values", JOptionPane.OK_CANCEL_OPTION);

Upvotes: 3

Views: 2983

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168845

The basic problem is that a TitledBorder will not expand a component to the point where it will be big enough to display the entire text. Instead it will just truncate the text.

The solution is to ensure the components are big enough for the text to display. I have shown this here by expanding the size of the text field, and by adding a 'full length' label in placed of the 'shortened' title.

Test Size Of Gui

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

class TestSizeOfGui {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JTextField account = new JTextField(10);
                JPanel accountPanel = new JPanel(new GridLayout());
                accountPanel.add(account);
                accountPanel.setBorder(new TitledBorder("Account"));

                String[] firstDigitList = {
                    "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};

                JLabel firstDigitListLabel = new JLabel("Leading Digit Change");
                JPanel firstDigitListPanel = new JPanel(new BorderLayout(4,2));
                firstDigitListPanel.add(firstDigitListLabel, BorderLayout.WEST);
                JComboBox firstDigitCombo = new JComboBox(firstDigitList);
                firstDigitListPanel.add(firstDigitCombo);
                firstDigitCombo.setSelectedIndex(0);
                firstDigitListPanel.setBorder(new TitledBorder("LDC"));

                JPanel panel = new JPanel();
                panel.add(accountPanel);
                panel.add(firstDigitListPanel);

                int result = JOptionPane.showConfirmDialog(
                    null,
                    panel,
                    "Please Enter Values",
                    JOptionPane.OK_CANCEL_OPTION);

                }
            });
    }
}

Upvotes: 5

Related Questions