Fixed Height JDialog

Possible duplicate:
JDialog allows the user to change the width of the dialog

I have a JDialog that I would like the width to change, but not the height.

This is my current code:

addComponentListener(new ComponentAdapter() {
    @Override
    public void componentResized(ComponentEvent e) {
        setLocation(getLocation().x, getLocation().y + (getHeight() - staticHeight));
        setSize(new Dimension(getWidth(), staticHeight));
        super.componentResized(e);
    }
});

The problem is that the code is called after the window is resized. Resize the window and then flicker back.

I would like to do this, when the user tries to drag the height of the window, he does nothing.

+5
source share
4 answers
  • you cannot directly stop resizing,

  • you cannot block the underlying property of containers,

  • a container from the native OS and is built on its properties,

  • and restrictions for resizing the screen

  • rest (with code) Resizing components on @camickr

  • , AbsoluteLayout,

  • ,

+4

. setResizable(), .
, JDialog, JDialog
, , ( ), ? ,

+3

. , - , , , , .

, .

, radai, , . , , ( , ).

import java.awt.Frame;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JDialog;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;

public class Test {

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

    public static void init() {
        final int staticHeight = 150;
        final JDialog dialog = new JDialog((Frame) null) {

            @Override
            protected JRootPane createRootPane() {
                JRootPane rp = new JRootPane() {
                    @Override
                    public void reshape(int x, int y, int w, int h) {
                        super.reshape(x, y, w, staticHeight);
                    }
                };
                rp.setOpaque(true);
                return rp;
            }

            @Override
            public void reshape(int x, int y, int width, int height) {
                super.reshape(x, y, width, staticHeight);
            }

        };
        dialog.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                dialog.setSize(dialog.getWidth(), staticHeight);
            }
        });
        dialog.pack();
        dialog.setVisible(true);

    }
}
+3

Here, try this sample code to set JDialognon-vertical resizing. Although I really do not want to take credit for this, all the credit belongs to the creator of this program, which is "Darryl Burke", the link to the program is here .

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

public class WidthResizeableDialog {  

  Robot robot;  
  static final int HEIGHT = 400;  
  Point lastLocation;  

  public static void main(String[] args) {  
    SwingUtilities.invokeLater(new Runnable() {  

      @Override  
      public void run() {  
        new WidthResizeableDialog().makeUI();  
      }  
    });  
  }  

  public void makeUI() {  
    try {  
      robot = new Robot();  
    } catch (AWTException ex) {  
      ex.printStackTrace();  
    }  
    final JDialog dialog = new JDialog();  
    dialog.setSize(400, HEIGHT);  
    dialog.setLocationRelativeTo(null);  
    dialog.addWindowListener(new WindowAdapter() {  

      @Override  
      public void windowClosing(WindowEvent e) {  
        System.exit(0);  
      }  
    });  
    dialog.addComponentListener(new ComponentAdapter() {  

      @Override  
      public void componentMoved(ComponentEvent e) {  
        SwingUtilities.invokeLater(new Runnable() {  

          @Override  
          public void run() {  
            lastLocation = dialog.getLocation();  
          }  
        });  
      }  
    });  
    dialog.getRootPane().addComponentListener(new ComponentAdapter() {  

      @Override  
      public void componentResized(ComponentEvent e) {  
        int height = dialog.getHeight();  
        if (robot != null && height != HEIGHT) {  
          robot.mouseRelease(InputEvent.BUTTON1_MASK);  
          dialog.setLocation(lastLocation);  
          dialog.setSize(dialog.getWidth(), HEIGHT);  
        }  
      }  
    });  
    dialog.setVisible(true);  
  }  
} 
+1
source

All Articles