0

I seem to have the opposite problem of everyone else. My JDialog has both minimize and maximize buttons by default. When the maximize button is pressed, the dialog maximizes - but the content doesn't. It just stays the same size, centered in a huge dialog. Same happens when you just grab the edge and re-size the dialog.

I've tried adding a WindowStateListener - but it never gets invoked. I added a WindowListener - and it's only invoked on Open/Close/Activate/Deactivate.

So, I either need to be able to get the dialog content to re-size with the dialog, OR remove the maximize button. (and I'd like to get rid of the minimize button.)

I do do a pack() after creating the dialog, as the controls in the dialog are dynamically created from a block of data, so I don't have an initial size to work with.

Okay, so here's the code. All the generated UI panels are in GridBagLayouts as well.

public class FastAccessDialog extends JDialog implements BeanActionListener {

private static final long   serialVersionUID = 1L;
private static final Cursor waitCursor       = new Cursor(Cursor.WAIT_CURSOR);

private Cursor              oldCursor;
private JPanel              cmdOutput;
private JScrollPane         cmdOutputScroll;

public FastAccessDialog(Frame owner, ObjectName bean, String methodName) throws InstanceNotFoundException, IntrospectionException,
        ReflectionException, IOException {
    super(owner);
    setResizable(true);
    setModal(false);
    setTitle(BeanUtil.cleanUpCamelCase(methodName));

    boolean enabled = (UIHintUtil.isEnabled(bean) == EnableState.ENABLED);

    // Find the BeanOperationInfo for that method.
    MBeanInfo info = JMXConnectionSingleton.getInstance().getMBeanInfo(bean);
    MBeanOperationInfo[] operations = info.getOperations();
    JComponent comp = null;

    for (MBeanOperationInfo opInfo : operations) {
        if (opInfo.getName().equals(methodName)) {
            comp = OperationsManager.getInstance().createControls(bean, opInfo, this, true, enabled);
            break;
        }
    }

    if (comp == null) {
        throw new IllegalArgumentException("Unknown method name: " + methodName);
    }

    Container cont = getContentPane();
    cont.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.WEST;
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.insets = new Insets(4, 4, 4, 4);
    cont.add(comp, gbc);
    cont.validate();

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            pack();
        }
    });
    return;
}

... other methods invoked when an operation is performed ...
... none of which are invoked before having the re-size problem ...
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
CasaDelGato
  • 603
  • 7
  • 17

1 Answers1

2
  1. JPanel.(re)validate();

  2. JPanel.repaint();

  3. JDialog.pack();

  4. SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            JDialog.setVisible(true);
        }
    });
    
  5. don't extends Top-Level Containers

  6. don't use ContentPane, there no reason ... from Java5

  7. nothing else in the case that JDialog.pack(); and JDialog.setVisible(true); are last code lines in void, method or constructor that returns JDialog instance

mKorbel
  • 109,525
  • 20
  • 134
  • 319