Search code examples
javaswingbuttonjoptionpane

Create dialog with no buttons


How do I remove the OK button when I use createDialog()

(I need to use createDialog because I need to set the location)

        JPanel myPanel = new JPanel();
        myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));//new BorderLayout());
        myPanel.setPreferredSize(new Dimension(400,400));
        JTextField tf= new JTextField(50);
        tf.setText("<HTML>Here is<br>my text");
        myPanel.add(tf);

        JOptionPane optionPane = new JOptionPane(myPanel,1,JOptionPane.DEFAULT_OPTION);   
        optionPane.setOptions(new Object[]{});
        JDialog dialog = optionPane.createDialog(null, "Quick Help");

                dialog.setLocation(10,10);
        dialog.setAlwaysOnTop(dialog.isAlwaysOnTopSupported());
        dialog.setVisible(true);

Solution

  • JOptionPane pane = new JOptionPane("message",  JOptionPane.PLAIN_MESSAGE, JOptionPane.PLAIN_MESSAGE);
                JDialog myDialog=  pane.createDialog(null, "New Topic");
                myDialog.setLocation(1000, 100);
                myDialog.setVisible(true);
    

    In your case would be something like this:

       JOptionPane optionPane = new JOptionPane(this,1,JOptionPane.PLAIN_MESSAGE);   
                    optionPane.setOptions(new Object[]{});
                    JDialog dialog = optionPane.createDialog(null, "Quick Help");
    
                    dialog.setLocation(1000,10);
                    dialog.setAlwaysOnTop(dialog.isAlwaysOnTopSupported());
                    dialog.setVisible(true);
    

    Tested and working.