Creating a custom blocking Java Swing prompt

后端 未结 1 1173
情歌与酒
情歌与酒 2021-01-18 12:22

This problem is solved.

I\'m am developing a Java Swing based projected, and the look & feel of the application is completely customized. We are trying to maint

相关标签:
1条回答
  • 2021-01-18 12:53

    Make CustomPrompt extends JDialog, have it's constructor call super passing the owner Window and the desired ModalityType.

    public class CustomPrompt extends JDialog {
    
      public static String showPrompt(Window parent, String title, String text, 
             String defaultText) {
        final CustomPrompt prompt = new CustomPrompt(parent);
        prompt.setTitle(title);
        // set other components text
        prompt.setVisible(true);
    
        return prompt.textField.getText();
      }
    
      private JTextField textField;
    
      // private if you only want this prompt to be accessible via constructor methods
      private CustomPrompt(Window parent) {
        super(parent, Dialog.DEFAULT_MODALITY_TYPE); 
        // Java >= 6, else user super(Frame, true) or super(Dialog, true);
        initComponents(); // like Netbeans
      }
    
      // initComponents() and irrelevant code. 
    }
    
    0 讨论(0)
提交回复
热议问题