-1

How to make a JFrame unmovable?

I am working on an application that needs the use of multiple frames, using various mouse events for each of them. The application requires the frames to stay where they are defined to be.

Thank You for your answers.

Mohit Jain
  • 15
  • 1
  • 4

3 Answers3

1

Try something like

myJFrame.addComponentListener(new ComponentAdapter() {
         public void componentResized(ComponentEvent e) {
            setSize(1024,768);  // or whatever your full size is
         }
         public void componentMoved(ComponentEvent e) {
            setLocation(0,0);
         }
      });
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
1

I believe the best you can do is remember their positions and move them back on mouse release. This is a very bad idea, and you shouldn't do it. If the user wants to move frames around, let them.

Eric Stein
  • 13,209
  • 3
  • 37
  • 52
  • 1
    Somehow I think someone is going to end up with an app that is constantly snapping back to its position every time it's moved by 1 pixel...I just hope it's not me. – Alex Aug 22 '13 at 13:04
1

One approach for making the frame unmovable is to make it undecorated

myFrame.setUndecorated(true);
Color color = UIManager.getColor("activeCaptionBorder");
myFrame.getRootPane().setBorder(BorderFactory.createLineBorder(color, 4));
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Thank you it works. But is there another way where the title bar stays. – Mohit Jain Aug 23 '13 at 05:11
  • Not really, approaches that use a listener to ground the frame give a "jumpy" result when you attempt to move the frame. As an aside, you could give the undecorated frame a border to make it more frame-like – Reimeus Aug 23 '13 at 11:30