how to set location of popup window ?

processing 3.3.5

import static javax.swing.JOptionPane.*;          
void draw() {
          final String id = showInputDialog("Please enter new ID");
          if (id == null)   exit();
          else if ("".equals(id)){
            showMessageDialog(null, "Empty ID Input!!!", 
            "Error", ERROR_MESSAGE);
         }
  }

Answers

  • Answer ✓

    Example for you to try.

    Kf

    //REFERENCES: https://stackoverflow.com/questions/13760117/how-to-set-the-location-of-joptionpane-showmessagedialog
    //REFERENCES: 
    //REFERENCES:
    
    //INSTRUCTIONS:
    //         *-- This program has three states
    //         *-- First state: Your provided example. Leave input text box blank for this example. Pressing
    //         *-- ok leads to next stage. Cancel wil exit code.
    //         *-- Second state: Shows a scond dialog in a random height in the screen. Press in the x in the 
    //         *-- corner of the dialog to dismiss it. 
    //         *-- Third stage: it does nothing.
    //         *-- Note: Current state is shown in the title bar of the main sketch
    
    //===========================================================================
    // IMPORTS:
    import static javax.swing.JOptionPane.*; 
    
    import javax.swing.JDialog;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JButton;
    
    //===========================================================================
    // FINAL FIELDS:
    
    
    //===========================================================================
    // GLOBAL VARIABLES:
    
    int cidx=0;
    CustomDialog myDialog;
    
    //===========================================================================
    // PROCESSING DEFAULT FUNCTIONS:
    
    void settings() {
      size(400, 600);
    }
    
    void setup() {
    
      textAlign(CENTER, CENTER);
      rectMode(CENTER);
    
      fill(255);
      strokeWeight(2);
    }
    
    
    
    void draw() {
      background(0);
    
      if (cidx==0) {
    
        final String id = showInputDialog("Please enter new ID");
        if (id == null)   exit();
        else if ("".equals(id)) {
          mouseReleased();
          showMessageDialog(null, "Empty ID Input!!!", 
            "Error", ERROR_MESSAGE);
        }
    
        myDialog=null;
      } else if (cidx==1){
        if(myDialog==null)
        myDialog=new CustomDialog(null,true,"Hello");
    
      }
    }
    
    void keyReleased() {
    }
    
    void mouseReleased() {
    
      cidx=(cidx+1)%3;
      surface.setTitle(""+cidx);
    }
    
    
    
    //===========================================================================
    // OTHER FUNCTIONS:
    
    public class CustomDialog extends JDialog {
      private JPanel myPanel = null;
      private JButton yesButton = null;
      private JButton noButton = null;
    
      public CustomDialog(JFrame frame, boolean modal, String myMessage) {
        super(frame, modal);
        myPanel = new JPanel();
        getContentPane().add(myPanel);
        myPanel.add(new JLabel(myMessage));
        yesButton = new JButton("Yes");
        myPanel.add(yesButton);
        noButton = new JButton("No");
        myPanel.add(noButton);
        pack();
        //setLocationRelativeTo(frame);
        setLocation(200, int(random(200,displayHeight-200))); // <--
        setVisible(true);
      }
    }
    
  • wow,really complicated! Thank you very much

Sign In or Register to comment.