hi,

i developed a chatting application..

i want to do three things in the Frame Resizing. but i don't know how to do..? any one pls help me..

1. i tried to restrict the user cann't resize the frame less than (300,600) by setPreferedSize(). it works. but the user can resize upto 0,0 .. when the user leave the mouse button for resize, then it comes to the preferedsize. i want to restrict the user cannot resize less than 300,600. How to do..?

2. when the user minimize or maximize the frame size, after leave the mouse button for minisize/maximize, it draws the frame based for frame size.. i need to draw same time when i resize the frame. how to do?

3. when the frame is in resized size, i want to show to the dots (like triangle)in the right-bottom corner of the status bar to indicate the user can maximize or resize the frame. how to do..?

if any one come to know this, pls tell me..

Dani AI

Generated

and were on the right track: you need to enforce size constraints and force repaints while the user is dragging, and the resize-grip is just a small painted component. Below are practical, copy-paste friendly patterns that fill the gaps in the earlier replies and explain platform caveats.

To absolutely prevent the window from ending up smaller than 300x600 on release, set a minimum and also clamp size continuously if you need to avoid temporary undershoot on some platforms. The portable way to prevent the temporary “shrink while dragging” effect is to provide your own resize handling (undecorated frame + mouse drag on a corner/border) and clamp width/height as you compute the new bounds:

final JFrame frame = myFrame;
final int MIN_W = 300, MIN_H = 600;
final Point start = new Point();
final Dimension startSize = new Dimension();

frame.setUndecorated(true); // use your own decorations if needed

frame.addMouseListener(new MouseAdapter() {
  public void mousePressed(MouseEvent e) {
    start.setLocation(e.getXOnScreen(), e.getYOnScreen());
    startSize.setSize(frame.getWidth(), frame.getHeight());
  }
});

frame.addMouseMotionListener(new MouseMotionAdapter() {
  public void mouseDragged(MouseEvent e) {
    int dx = e.getXOnScreen() - start.x;
    int dy = e.getYOnScreen() - start.y;
    int w = Math.max(MIN_W, startSize.width + dx);
    int h = Math.max(MIN_H, startSize.height + dy);
    frame.setSize(w, h); // immediate, clamped resizing
  }
});

To make the contents redraw continuously while resizing (not only after mouse release), ensure you trigger layout/paint during resize events. Adding a listener that calls validate()/repaint() on the content pane (on the EDT) keeps the UI responsive:

frame.addComponentListener(new ComponentAdapter() {
  public void componentResized(ComponentEvent e) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        frame.getContentPane().validate();
        frame.getContentPane().repaint();
      }
    });
  }
});

For the little triangle grip on the status bar, add a tiny custom JComponent that paints a stacked-dot/rect pattern and (optionally) changes the cursor and starts your resize handling when dragged:

class ResizeGrip extends JComponent {
  ResizeGrip() { setPreferredSize(new Dimension(14,14)); setOpaque(false); }
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(UIManager.getColor("controlShadow"));
    int w = getWidth(), h = getHeight();
    for (int i = 0; i < 3; i++) {
      g.fillRect(w - 3 - i*3, h - 3 - i*3, 2, 2);
    }
  }
}

Notes: run Swing changes on the Event Dispatch Thread, avoid heavy work in resize handlers (they fire frequently), and remember native window managers behave differently—if you need absolute, flicker-free control, custom decorations + handlers are the reliable route.

Recommended Answers

All 2 Replies

Hmmmm.... Maybe add a component listener to your JFrame(extending Component Adapter). Then you should get a method labeled componentResized(). In that method you need some custom checking.

Try setMinimumSize().

As for the dots, you mean the thing that looks like this?

< Removed - My ascii art kind of sucks :P >
I think you'd have to draw that yourself though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.