2015-04-09 9 views
9

जेडीके 8u40 के बाद से, मैं एक पुष्टिकरण संवाद प्रदर्शित करने के लिए नया javafx.scene.control.Alert एपीआई का उपयोग कर रहा हूं। नीचे दिए गए उदाहरण में, "हाँ" बटन डिफ़ॉल्ट रूप से "नहीं" बटन के बजाय ध्यान केंद्रित है:जावाएफएक्स डिफ़ॉल्ट फोकस बटन अलर्ट संवाद

public boolean showConfirmDialog(String title, String header, String content, AlertType alertType) { 
    final Alert alert = new Alert(alertType); 
    alert.setTitle(title); 
    alert.setHeaderText(header); 
    alert.setContentText(content); 

    alert.getButtonTypes().clear(); 
    alert.getButtonTypes().addAll(ButtonType.YES, ButtonType.NO); 

    final Optional<ButtonType> result = alert.showAndWait(); 
    return result.get() == ButtonType.YES; 
} 

और मैं नहीं जानता कि यह कैसे बदलने के लिए।

संपादित करें:

यहाँ परिणाम का एक स्क्रीनशॉट जहां "हाँ" बटन डिफ़ॉल्ट रूप से केंद्रित है:

enter image description here

उत्तर

12

मैं अगर निम्नलिखित तरीके आमतौर पर यह करने के लिए है यकीन नहीं है, लेकिन आप बटन को देखकर और डिफ़ॉल्ट व्यवहार को स्वयं सेट करके डिफ़ॉल्ट बटन बदल सकते हैं:

public boolean showConfirmDialog(String title, String header, String content, AlertType alertType) { 
    final Alert alert = new Alert(alertType); 
    alert.setTitle(title); 
    alert.setHeaderText(header); 
    alert.setContentText(content); 

    alert.getButtonTypes().clear(); 
    alert.getButtonTypes().addAll(ButtonType.YES, ButtonType.NO); 

    //Deactivate Defaultbehavior for yes-Button: 
    Button yesButton = (Button) alert.getDialogPane().lookupButton(ButtonType.YES); 
    yesButton.setDefaultButton(false); 

    //Activate Defaultbehavior for no-Button: 
    Button noButton = (Button) alert.getDialogPane().lookupButton(ButtonType.NO); 
    noButton.setDefaultButton(true); 

    final Optional<ButtonType> result = alert.showAndWait(); 
    return result.get() == ButtonType.YES; 
} 
3

यदि आपके पास अल है ओक (निजी) ButtonBarSkin कक्षा में, doButtonOrderLayout() नामक एक विधि है जो कुछ डिफ़ॉल्ट ओएस व्यवहार के आधार पर बटन का लेआउट निष्पादित करती है।

इसके अंदर, तो आप इस पढ़ सकते हैं:

/* अब कि सभी बटन रखा गया है, हम यह सुनिश्चित करने के लिए ध्यान देने के सही बटन पर सेट है की जरूरत है। [...] यदि हां, तो हम इस डिफ़ॉल्ट बटन पर फ़ोकस का अनुरोध करते हैं। */

ButtonType.YES डिफ़ॉल्ट बटन है, यह एक केंद्रित होगा।

तो @ymene उत्तर सही है: आप डिफ़ॉल्ट व्यवहार बदल सकते हैं और फिर केंद्रित एक NO होगा।

या buttonOrderProperty() में BUTTON_ORDER_NONE सेट करके आप उस विधि का उपयोग करने से बच सकते हैं। अब पहले बटन पर ध्यान केंद्रित होगा, इसलिए आपको पहले NO बटन रखना होगा।

alert.getButtonTypes().setAll(ButtonType.NO, ButtonType.YES); 

ButtonBar buttonBar=(ButtonBar)alert.getDialogPane().lookup(".button-bar"); 
buttonBar.setButtonOrder(ButtonBar.BUTTON_ORDER_NONE); 

नोट YES अभी भी डिफ़ॉल्ट व्यवहार होगा: इसका मतलब यह है NO स्पेस बार (केंद्रित बटन) के साथ चुना जा सकता है, जबकि YES का चयन किया जाएगा यदि आप में प्रवेश (डिफ़ॉल्ट बटन) दबाएँ।

Alert dialog

या आप भी @crusam जवाब निम्नलिखित डिफ़ॉल्ट व्यवहार बदल सकते हैं।

+0

भी देखें: http://stackoverflow.com/a/37610648/59087 –

2

एक साधारण समारोह धन्यवाद crusam रहे हैं:

private static Alert setDefaultButton (Alert alert, ButtonType defBtn) { 
    DialogPane pane = alert.getDialogPane(); 
    for (ButtonType t : alert.getButtonTypes()) 
     ((Button) pane.lookupButton(t)).setDefaultButton(t == defBtn); 
    return alert; 
} 

उपयोग:

final Alert alert = new Alert( 
     AlertType.CONFIRMATION, "You sure?", ButtonType.YES, ButtonType.NO); 
if (setDefaultButton(alert, ButtonType.NO).showAndWait() 
     .orElse(ButtonType.NO) == ButtonType.YES) { 
    // User selected the non-default yes button 
} 
संबंधित मुद्दे