简体   繁体   中英

why we should use “null” in dialog boxes?

I want to know why we should write "null" in dialog boxes ?

str= JOptioPane.showMessageDialog(
                                 parentComponent(null), // <--
                                 messageStringExpression,
                                 boxTitleString,
                                 meeageType);

and when we have to write it ? can I write something else ?

thanks all

why we should write "null" in dialog boxes ?

If the parentComponent is null (aka parentComponent(null) ), then the JDialog depends on no visible window, and it's placed in a look-and-feel-dependent position such as the center of the screen.

can I write something else

Sure, you can add there any component you want like JButton, ....

Some example:

private JButton btn_Save;

btn_Save = new JButton(save);
        btn_Save.setText("Save Configuration");
        btn_Save.setBounds(20, 459, 290, 25);
        btn_Save.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == btn_Save) {
                    if(saveData()){
                        JOptionPane.showMessageDialog(btn_Save, "Event Configuration saved successfully!");
                    }
                    else{
                        JOptionPane.showMessageDialog(btn_Save, "Failed to save Event Configuration!");
                    }
                }
            }

In this case against null , The dialog centered on button btn_Save

JOptionPane.showMessageDialog(parentComponent(null),message);       

Then dialog centered on the desktop .

JOptionPane.showMessageDialog(someComponent,message);

Then dialog centered on the someComponent

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM