3 Replies Latest reply on Feb 1, 2008 11:28 PM by 843806

    Undecorated and resizable dialog

    843806
      Does anyone know how to create a JDialog that is both undecorated and resizable?
      Trying to call setUndecorated(True) and setResizable(True), only makes it undecorated. The dialog will not allow me to resize.

      Thanks.
        • 1. Re: Undecorated and resizable dialog
          843806
          sircastyx

          You have misspelled this word, and your post was not a good example of its meaning :)

          Nevertheless, one way you could do this is to use my Resizeable class. Search google for tjacobs Resizeable.java and you should find it. You will probably have to modify the code to apply the resize directly to your Window
          • 2. Re: Undecorated and resizable dialog
            843806
            LOL...thanks (I think). But someone already registered sarcastic, as a username.
            So I went with what I could.

            Thanks for the reply. Will take a look at your code.
            While briefly looking over it, this could help me out.

            Thanks again for the quick response.
            • 3. Re: Undecorated and resizable dialog
              843806
              I'm not sure why the Sun folks didn't implement this, but there is a way you can do this so that it will work as a normal dialog (i.e. with a contentPane that uses a LayoutManager).

              The basic idea is to subclass JDialog, then create a private contentPane that has narrow components around the edges (I use empty JLabels, but others will do). You then set the relevant cursors and mouse listeners for these, capture the mouse events and resize accordingly.

              Here's some subclass code that does this:
              import java.awt.*;
              import java.awt.event.*;
              import java.util.*;
              
              import javax.swing.*;
              import javax.swing.border.*;
              
              /**
               * This JDialog subclass supports resizing and undecorated mode.
               * There is also support for disalbing the resizing on any given side.
               */
              public class CUndecoratedResizeableDialog extends JDialog implements MouseListener, MouseMotionListener
              {
                   static final protected int     NW_SIDE          = 1;
                   static final protected int     N_SIDE          = 2;
                   static final protected int     NE_SIDE          = 3;
                   static final protected int     L_SIDE          = 4;
                   static final protected int     R_SIDE          = 5;
                   static final protected int     SW_SIDE          = 6;
                   static final protected int     S_SIDE          = 7;
                   static final protected int     SE_SIDE          = 8;
                   
                   private JPanel     resizePanel          = null;
                   private JPanel     contentPanel     = null;
                   private JLabel     left               = null;
                   private JLabel     right               = null;
                   private JLabel     top                    = null;
                   private JLabel     bottom               = null;
                   private JLabel     topleft               = null;
                   private JLabel     topright          = null;
                   private JLabel     bottomleft          = null;
                   private JLabel     bottomright          = null;
              
                   private boolean bInDrag = false;;
                   private Rectangle startSize = null;
                   
                   private HashSet<Integer> hsDisabledSides = new HashSet<Integer>();
                   
              
                   public CUndecoratedResizeableDialog(Frame owner)
                   {
                        super(owner);
                        initialize();
                   }
                   
                   public CUndecoratedResizeableDialog()
                   {
                        super();
                        initialize();
                   }
                   
                   private void initialize()
                   {
                        resizePanel = new JPanel(new BorderLayout());
                        contentPanel = new JPanel(new BorderLayout());
                        setUndecorated(true);
                        
                        left = new JLabel();
                        right = new JLabel();
                        top = new JLabel();
                        bottom = new JLabel();
                        topleft = new JLabel();
                        topright = new JLabel();
                        bottomleft = new JLabel();
                        bottomright = new JLabel();
                        
                        left.setPreferredSize(new Dimension(2, 0));
                        left.setMinimumSize(new Dimension(2, 0));
                        
                        right.setPreferredSize(new Dimension(2, 0));
                        right.setMinimumSize(new Dimension(2, 0));
                        top.setPreferredSize(new Dimension(0, 2));
                        top.setMinimumSize(new Dimension(0, 2));
                        bottom.setPreferredSize(new Dimension(0, 2));
                        bottom.setMinimumSize(new Dimension(0, 2));
                        
                        left.addMouseListener(this);
                        right.addMouseListener(this);
                        top.addMouseListener(this);
                        bottom.addMouseListener(this);
              
                        left.addMouseMotionListener(this);
                        right.addMouseMotionListener(this);
                        top.addMouseMotionListener(this);
                        bottom.addMouseMotionListener(this);
                        
                        left.setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
                        right.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
                        top.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
                        bottom.setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
              
                        topleft.setPreferredSize(new Dimension(4, 4));
                        topleft.setMinimumSize(new Dimension(4, 4));
                        topright.setPreferredSize(new Dimension(4, 4));
                        topright.setMinimumSize(new Dimension(4, 4));
                        bottomleft.setPreferredSize(new Dimension(4, 4));
                        bottomleft.setMinimumSize(new Dimension(4, 4));
                        bottomright.setPreferredSize(new Dimension(4, 4));
                        bottomright.setMinimumSize(new Dimension(4, 4));
                        
                        topleft.addMouseListener(this);
                        topright.addMouseListener(this);
                        bottomleft.addMouseListener(this);
                        bottomright.addMouseListener(this);
              
                        topleft.addMouseMotionListener(this);
                        topright.addMouseMotionListener(this);
                        bottomleft.addMouseMotionListener(this);
                        bottomright.addMouseMotionListener(this);
                        
                        topleft.setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
                        topright.setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
                        bottomleft.setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
                        bottomright.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
                        
                        JPanel northPanel = new JPanel(new BorderLayout());
                        northPanel.add(topleft, BorderLayout.WEST);
                        northPanel.add(top, BorderLayout.CENTER);
                        northPanel.add(topright, BorderLayout.EAST);
              
                        JPanel southPanel = new JPanel(new BorderLayout());
                        southPanel.add(bottomleft, BorderLayout.WEST);
                        southPanel.add(bottom, BorderLayout.CENTER);
                        southPanel.add(bottomright, BorderLayout.EAST);
                        
                        resizePanel.add(left, BorderLayout.WEST);
                        resizePanel.add(right, BorderLayout.EAST);
                        resizePanel.add(northPanel, BorderLayout.NORTH);
                        resizePanel.add(southPanel, BorderLayout.SOUTH);
                        resizePanel.add(contentPanel, BorderLayout.CENTER);
                        
                        this.setContentPane(resizePanel);
                   }
                   
                   @Override
                   public Container getContentPane()
                   {
                        return contentPanel;
                   }
              
                   public void mousePressed(MouseEvent e) 
                   {
                        startSize = this.getBounds();
                   }
              
                   public void mouseDragged(MouseEvent e)
                   {
                        if (startSize == null)
                             return;
                        if (e.getSource() == topleft)
                        {
                             if (hsDisabledSides.contains(NW_SIDE))
                                  return;
                             startSize.y += e.getY();
                             startSize.height -= e.getY();
                             startSize.x += e.getX();
                             startSize.width -= e.getX();
                             this.setBounds(startSize);
                        }
                        else
                        if (e.getSource() == top)
                        {
                             if (hsDisabledSides.contains(N_SIDE))
                                  return;
                             startSize.y += e.getY();
                             startSize.height -= e.getY();
                             this.setBounds(startSize);
                        }
                        else
                        if (e.getSource() == topright)
                        {
                             if (hsDisabledSides.contains(NE_SIDE))
                                  return;
                             startSize.y += e.getY();
                             startSize.height -= e.getY();
                             startSize.width += e.getX();
                             this.setBounds(startSize);
                             
                        }
                        else
                        if (e.getSource() == left)
                        {
                             if (hsDisabledSides.contains(L_SIDE))
                                  return;
                             startSize.x += e.getX();
                             startSize.width -= e.getX();
                             this.setBounds(startSize);
                        }
                        else
                        if (e.getSource() == right)
                        {
                             if (hsDisabledSides.contains(R_SIDE))
                                  return;
                             startSize.width += e.getX();
                             this.setBounds(startSize);
                        }
                        else
                        if (e.getSource() == bottomleft)
                        {
                             if (hsDisabledSides.contains(SW_SIDE))
                                  return;
                             startSize.height += e.getY();
                             startSize.x += e.getX();
                             startSize.width -= e.getX();
                             this.setBounds(startSize);
                        }
                        else
                        if (e.getSource() == bottom)
                        {
                             if (hsDisabledSides.contains(S_SIDE))
                                  return;
                             startSize.height += e.getY();
                             this.setBounds(startSize);
                        }
                        else
                        if (e.getSource() == bottomright)
                        {
                             if (hsDisabledSides.contains(SE_SIDE))
                                  return;
                             startSize.height += e.getY();
                             startSize.width += e.getX();
                             this.setBounds(startSize);
                        }
                   }
                   
                   public void mouseClicked(MouseEvent e) {}
                   public void mouseMoved(MouseEvent e) {}
                   public void mouseReleased(MouseEvent e) {} 
                   public void mouseEntered(MouseEvent e) {} 
                   public void mouseExited(MouseEvent e) {}
              
                   protected void disableResizeSide(int side)
                   {
                        hsDisabledSides.add(side);
                        if (side == NW_SIDE)
                        {
                             topleft.setCursor(Cursor.getDefaultCursor());
                        }
                        else
                        if (side == N_SIDE)
                        {
                             top.setCursor(Cursor.getDefaultCursor());
                        }
                        else
                        if (side == NE_SIDE)
                        {
                             topright.setCursor(Cursor.getDefaultCursor());
                        }
                        else
                        if (side == L_SIDE)
                        {
                             left.setCursor(Cursor.getDefaultCursor());
                        }
                        else
                        if (side == R_SIDE)
                        {
                             right.setCursor(Cursor.getDefaultCursor());
                        }
                        else
                        if (side == SW_SIDE)
                        {
                             bottomleft.setCursor(Cursor.getDefaultCursor());
                        }
                        else
                        if (side == S_SIDE)
                        {
                             bottom.setCursor(Cursor.getDefaultCursor());
                        }
                        else
                        if (side == SE_SIDE)
                        {
                             bottomright.setCursor(Cursor.getDefaultCursor());
                        }
                   }
                   
                   /**
                    * Test Bed
                    */
                   static public void main(String[] args)
                   {
                        CUndecoratedResizeableDialog dlg = new CUndecoratedResizeableDialog();
                        dlg.setSize(500, 600);
                        dlg.setLocation(new Point(500, 500));
              
                        //Try disabling certain sides. This is useful if the undecorated dialog
                        //is non-moveable (e.g. fixed to an edge)
              //          dlg.disableResizeSide(CUndecoratedResizeableDialog.L_SIDE);
                        
                        JLabel hello = new JLabel("Hello!");
                        dlg.getContentPane().add(hello);
                        
                        dlg.setVisible(true);
                   }
              }
              You can also specify certain sides not to be resizable. You might be thinking - 'why on earth would you want that?' - well, undecorated dialogs can be used for a great variety of uses - including things like anchored displays - e.g. fixed to the left side of a parent. In such scenarios, you don't want the user to be able to mess up your carefully laid out components!

              Hope this helps!