Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java -- JDialog unmovable

What code will facilitate making a JDialog unmovable? I've looked at two options:

  1. setUndecorated(true); which works but removes all the trimmings.
  2. addComponentListener and overriding the componentMoved() method, which causes the JDialog to subsequently call induceEpilepticSeizure() upon moving.

Any ideas?

like image 642
farm ostrich Avatar asked Sep 05 '11 16:09

farm ostrich


1 Answers

My first instinct is - you can't unless you DO use setUndecorated(true)... You could manually put some trimmings there, but, well, UGH!

So if you want the native trimmings AND you want it immovable without the horrible flickering from using a component listener, I think you can't.

You could create a border manually that LOOKS like the default border...here's an example of how to do it, although I've intentionally made the border look like the ugliest thing you've seen all day. You'll need to find the right combination of BorderFactory calls to achieve what you want to do.

public static void main(String[] args) throws InterruptedException {
    JDialog frame = new JDialog((Frame) null, "MC Immovable");
    frame.setUndecorated(true);
    JPanel panel = new JPanel();
    panel.setBorder(BorderFactory.createEtchedBorder(Color.GREEN, Color.RED));
    panel.add(new JLabel("You can't move this"));

    frame.setContentPane(panel);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    frame.setVisible(true);
}
like image 148
Steve McLeod Avatar answered Oct 20 '22 00:10

Steve McLeod