2013-06-10 22 views
11

にアイコンでタブを作成するためにどのように私はJavaFXのとFirefoxの設定パネルに似たアイコンでタブパネルを作成したいですこの?JavaFXの

+0

setGraphic(ノードyourNode)タブクラスのあなたのタブに任意のノードを追加するための良い方法です。しかし、アプリケーションでイメージに表示されているように見えるようにしたい場合は、TabPane(私の提案)ではなくToolBarで試してみてください。 –

答えて

15

タブには、JavaFXの他の多くの要素と同様に、setGraphic(Node value)というメソッドがあり、JavaFXノードを配置できます。例:

import javafx.application.Application; 
import javafx.geometry.Pos; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.control.Label; 
import javafx.scene.control.Tab; 
import javafx.scene.control.TabPane; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.HBox; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Circle; 
import javafx.stage.Stage; 

public class TabPaneTest extends Application { 
    public static void main(String[] args) { 
    Application.launch(args); 
    } 
    @Override 
    public void start(Stage primaryStage) { 
    primaryStage.setTitle("Tabs"); 
    Group root = new Group(); 
    Scene scene = new Scene(root, 400, 250, Color.WHITE); 
    TabPane tabPane = new TabPane(); 
    BorderPane borderPane = new BorderPane(); 
    for (int i = 0; i < 5; i++) { 
     Tab tab = new Tab(); 
     tab.setGraphic(new Circle(0, 0, 10)); 
     HBox hbox = new HBox(); 
     hbox.getChildren().add(new Label("Tab" + i)); 
     hbox.setAlignment(Pos.CENTER); 
     tab.setContent(hbox); 
     tabPane.getTabs().add(tab); 
    } 
    // bind to take available space 
    borderPane.prefHeightProperty().bind(scene.heightProperty()); 
    borderPane.prefWidthProperty().bind(scene.widthProperty()); 

    borderPane.setCenter(tabPane); 
    root.getChildren().add(borderPane); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 
    } 
} 

結果:

tabs in a TabPane

3

私は古いスレッドを知っている、しかし、私はどこにも直接的な答えを見つけるdidntの。だから私はいくつかそれを検索するために役立つだろういくつかを投稿することを考えた。

これは、firefoxの設定画面のようなタブを取得したものです。

イメージをsetGraphicsでタブに追加し、次のコードをアプリケーションのcssファイルに追加します。私の画像サイズは48x48でした。だから私は、画像のURLから直接画像を追加する方法70

.tab-label { 
    -fx-content-display: top; 
} 
.tab-pane { 
    -fx-tab-min-height: 70; 
    -fx-tab-max-height: 70; 
} 
+0

参考として、fxmlの設定例を示します。 http://stackoverflow.com/a/9930822/652696 – dejuknow

0

としての高さのために行ってきました:

  Tab tab = new Tab(); 
      tab.setGraphic(buildImage("patch/to/image"); 

    // Helper method to create image from image patch 
    private static ImageView buildImage(String imgPatch) { 
      Image i = new Image(imgPatch); 
      ImageView imageView = new ImageView(); 
      //You can set width and height 
      imageView.setFitHeight(16); 
      imageView.setFitWidth(16); 
      imageView.setImage(i); 
      return imageView; 
     }