开发者

How to disable X on dialog boxes?

Is there a way to disable all X on dialog boxes?

I can accomplish this on one by doing:

JOptionPane pane = new开发者_StackOverflow社区 JOptionPane("Are you hungry?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);

JDialog dialog = pane.createDialog("Title"); 
dialog.addWindowListener(new WindowAdapter() {    
public void windowClosing(WindowEvent evt) {
    } }); 
dialog.setContentPane(pane);
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); 
dialog.pack();

But the problem is thst I have to apply this to other 20-40 dialog boxes which will be time consuming..

So I was wondering just how you can do one line of code to make the fonts bigger on all the Dialog boxes (shown below), is there a way to for the X disable feature.

UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("ARIAL",Font.BOLD,30)));


JDialog method setDefaultCloseOperation works just like JFrame's:

http://download.oracle.com/javase/6/docs/api/javax/swing/JDialog.html#setDefaultCloseOperation(int)

Neither will do this for all frames, it will just do it for the instance it is set on. If you want to apply to all your dialogs you need to do one of the following:

  • Call this every time you construct a dialog.
  • Extend JDialog and have it set the default for you, then use that new class.
  • Create a factory method that will construct a dialog with this default set. Then call this method whenever you need a new dialog.


You need to add a windowListener but also set the default close operation for the dialog.

    setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    this.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            if (someConditionIsMet) {
                dispose();
            }
        }
    });


Rather than disable a control, you should remove it. Users shouldn't see an "X" button and then have it not behave like they expect.

See:

  • Dialog.setUndecorated() for a single dialog
  • JDialog.setDefaultLookAndFeelDecorated() to provide a hint for future dialogs


Why not just create a custom class that extends the JDialog setting your desired close behavior in the constructor?

class NoCLoseDialog extends JDialog {
  NoCloseDialog(){
    addWindowListener(new WindowAdaptor(){
      // ... Close behavior ...
    });
  }
}

Crtl+F Replace JDialog with NoCloseDialog ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜