문제

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top