2016-06-09 14 views
6

मैं बटन को JavaFX Tab में जोड़ने का तरीका खोज रहा हूं।टैब और टैब क्षेत्र में बटन जोड़ें JavaFX

इसके लिए इंटरनेट खोजा लेकिन मुझे इसके लिए कोई समाधान नहीं मिल रहा है।

नीचे स्क्रीनशॉट में बटन की तरह कुछ।

क्या कोई मेरी मदद कर सकता है?

enter image description here

उत्तर

13

Tab रों पर Button रों iconed है करने के लिए:

Tab की setGraphic विधि Tab पर प्रदर्शित करने के लिए किसी भी Node जोड़ने के लिए इस्तेमाल किया जा सकता। Button जोड़ा जा सकता है क्योंकि यह Node है।

इसके बाद एक पूर्ण कार्यात्मक बटन मौजूद है, लेकिन यह कोई आइकन प्रदर्शित नहीं करता है। Button में विधि भी है जो एक ही काम करती है, इसलिए ImageView को पर पर प्रदर्शित करने के लिए जोड़ा जा सकता है।

टैब-क्षेत्र के ऊपरी-दाएं कोने पर नियंत्रण रखने के लिए Button रों:

ये बटन TabPane पर रखा जा सकता है, बल्कि TabPane अंदर से। इसके लिए आप का उपयोग Button एस को शीर्ष-दाएं कोने में एंकर करने के लिए कर सकते हैं।

उदाहरण:

public class ButtonedTabPane extends Application { 
    @Override 
    public void start(Stage primaryStage) { 
     BorderPane root = new BorderPane(); 
     TabPane tabPane = new TabPane(); 
     tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE); 

     // HBox of control buttons 
     HBox hbox = new HBox(); 
     hbox.getChildren().addAll(createTabButton("min.png"), createTabButton("max.png")); 

     // Anchor the controls 
     AnchorPane anchor = new AnchorPane(); 
     anchor.getChildren().addAll(tabPane, hbox); 
     AnchorPane.setTopAnchor(hbox, 3.0); 
     AnchorPane.setRightAnchor(hbox, 5.0); 
     AnchorPane.setTopAnchor(tabPane, 1.0); 
     AnchorPane.setRightAnchor(tabPane, 1.0); 
     AnchorPane.setLeftAnchor(tabPane, 1.0); 
     AnchorPane.setBottomAnchor(tabPane, 1.0); 

     // Create some tabs 
     Tab tab = new Tab("Files"); 
     tab.setGraphic(createTabButton("files.png")); 
     ((Button) tab.getGraphic()).setOnAction(e -> System.out.println("I'll show the list of files!")); 
     tabPane.getTabs().add(tab); 

     tab = new Tab("Network"); 
     tab.setGraphic(createTabButton("network.png")); 
     ((Button) tab.getGraphic()).setOnAction(e -> System.out.println("I'll show the network!")); 
     tabPane.getTabs().add(tab); 

     root.setCenter(anchor); 
     Scene scene = new Scene(root, 400, 400); 
     scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm()); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    private Button createTabButton(String iconName) { 
     Button button = new Button(); 
     ImageView imageView = new ImageView(new Image(getClass().getResource(iconName).toExternalForm(), 
       16, 16, false, true)); 
     button.setGraphic(imageView); 
     button.getStyleClass().add("tab-button"); 
     return button; 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 

केवल एक चीज Button रों से डिफ़ॉल्ट पृष्ठभूमि और बॉर्डर दूर करने के लिए छोड़ दिया है। यह आपके सीएसएस स्टाइलशीट में निम्नलिखित सीएसएस चयनकर्ताओं को डालने से किया जा सकता है।

style.css

.tab-button { 
    -fx-border-width: 0; 
    -fx-background-radius: 0; 
    -fx-background-color: transparent; 
    -fx-content-display: graphic-only; 
} 

.tab-button:hover { 
    -fx-background-color: white; 
} 

परिणाम:

 
         https://i.stack.imgur.com/olclI.png
+0

Tnx कि काम किया है – JimmyD

संबंधित मुद्दे