当前位置: 首页 > 知识库问答 >
问题:

如何在JavaFX中更改TitledPane中的header组件

丁立果
2023-03-14

我有一个应用程序必须有可折叠的面板,所以在Java FX中的TitledPane和Accordion设置是一个自然的候选者。

在应用程序中,当容器折叠时,我需要有容器的自定义头。我在TitledPane的css文档中看到,头实际上是一个HBox和各种其他组件。

http://docs.oracle.com/javafx/2/api/javafx/scene/doc-files/cssref.html#标题窗格

我想访问这个组件并用自定义组件替换它。

我一直在使用TitledPane API的setGraphic函数和setContentDisplay到GraphicOnly。使用自定义组件。但是,我不能让它正确地呈现。我在移除箭头和移除箭头所占的空间方面遇到了问题。

请参阅下面的链接,以获得所需外观的截图和它的实际外观。

http://tinypic.com/r/s1pxfn/6

如何去掉箭头和填充?

共有1个答案

郜俊健
2023-03-14

TitledPane是已标记的,因此可以在侦听器或扩展属性的绑定中对其设置图形。

更改背景颜色等(包括重写:Focked伪类的样式)可以通过css完成(有关示例,请参考jfxrt.jar中caspian.css样式表的TitledPane部分)。

如果需要对TitledPane的子组件进行基于代码的访问,则可以在将TitledPane添加到舞台上显示的场景后使用查找功能。

adminPane.lookup(".arrow").setVisible(false);

我建议在尝试查找之前尝试基于图形/CSS样式表的方法

如果您不需要标签显示文本,那么设置它只显示一个图形。

titledPane.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);

下面是一些示例代码,它将带有自定义头的TitledPane显示在没有自定义头的TitledPane旁边。

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.Pos;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

/** http://stackoverflow.com/questions/11765436/how-to-change-header-component-in-titledpane-in-javafx */
public class TitledPaneCustomization extends Application {
  public static void main(String[] args) { launch(args); }

  @Override public void start(Stage primaryStage) {
    TitledPane adminPane = new TitledPane("Administration", 
      VBoxBuilder.create().style("-fx-padding: 10").spacing(10).children(
        ButtonBuilder.create().text("Admin Client").maxWidth(Double.MAX_VALUE).build(),
        ButtonBuilder.create().text("Admin User").maxWidth(Double.MAX_VALUE).build()
      ).build()            
    );
    adminPane.setAnimated(false);
    adminPane.getStyleClass().add("admin");
    Node open   = HBoxBuilder.create().spacing(5).children(
      new Circle(4, 4, 8, Color.FORESTGREEN),
      new Rectangle(50, 16, Color.AQUAMARINE)
    ).build();
    Node closed = HBoxBuilder.create().spacing(5).children(
      new Circle(4, 4, 8, Color.GOLDENROD),
      new Rectangle(50, 16, Color.AQUAMARINE)
    ).build();

    adminPane.graphicProperty().bind(
      Bindings
        .when(adminPane.expandedProperty())
          .then(open)
          .otherwise(closed)
    );
    adminPane.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);

    TitledPane viewPane = new TitledPane("View", 
      VBoxBuilder.create().style("-fx-padding: 10").spacing(10).children(
        ButtonBuilder.create().text("View Client").maxWidth(Double.MAX_VALUE).build(),
        ButtonBuilder.create().text("View User").maxWidth(Double.MAX_VALUE).build()
      ).build()            
    );
    viewPane.setAnimated(false);

    VBox errorPane = VBoxBuilder.create().style("-fx-padding: 10").spacing(10).children(
      new Label("500: Aug 8, 12:15pm"), 
      new Label("404: Aug 7, 3:27am")
    ).build();
    Label nErrors = new Label();
    nErrors.getStyleClass().add("nerrors");
    nErrors.textProperty().bind(Bindings.size(errorPane.getChildren()).asString());

    TitledPane connectivityPane = new TitledPane(
      "",      
      errorPane
    );
    Label connectivityErrorLabel = new Label("CONNECTIVITY ERROR");
    connectivityErrorLabel.getStyleClass().add("connectivityErrorLabel");
    connectivityPane.getStyleClass().add("connectivity");
    connectivityPane.setAnimated(false);
    connectivityPane.setGraphic(
      HBoxBuilder.create().spacing(2).alignment(Pos.CENTER).styleClass("header").children(
        nErrors,    
        new ImageView(
          new Image(
            "http://openiconlibrary.sourceforge.net/gallery2/open_icon_library-full/icons/png/48x48/actions/network-disconnect-2.png",
            0, 24, true, true
          )
        ),
        connectivityErrorLabel
      ).build()
    );

    HBox layout = new HBox(10);
    layout.setStyle("-fx-padding: 10; -fx-background-color: cornsilk;");
    layout.getChildren().addAll(adminPane, viewPane, connectivityPane);
    layout.setPrefHeight(150);
    layout.getStylesheets().add(this.getClass().getResource("titledpanecustomization.css").toExternalForm());
    primaryStage.setScene(new Scene(layout));
    primaryStage.show();

    Node arrow = adminPane.lookup(".arrow");
    arrow.setVisible(false);
    arrow.setManaged(false);

    // translate the titledpane arrow and header so that the arrow is displayed to right of the header.
    Pane connectivityArrow = (Pane) connectivityPane.lookup(".arrow");
    connectivityArrow.translateXProperty().bind(
      connectivityPane.widthProperty().subtract(connectivityArrow.widthProperty().multiply(2))
    );
    Pane connectivityTitle = (Pane) connectivityPane.lookup(".header");
    connectivityTitle.translateXProperty().bind(
      connectivityArrow.widthProperty().negate()
    );
  }
}
/** titledpanecustomization.css place in same build directory as TitledPaneCustomization.java 
    and ensure build system copies it to the output classpath. */
.admin .titled-pane > .title {
  -fx-background-color: blue, yellow, linear-gradient(to bottom, derive(coral, +50%), coral);
}

.connectivity {
  -fx-text-fill: white;
}

.nerrors {
  -fx-background-color: derive(-fx-focus-color, -15%);
  -fx-padding: 5 8 5 8;
  -fx-text-fill: white;
}

.connectivityErrorLabel {
  -fx-text-fill: white;
  -fx-padding: 0 40 0 3; 
}

.connectivity .titled-pane > .title {
  -fx-background-color: -fx-box-border, -fx-inner-border, -fx-body-color;
  -fx-background-insets: 0, 1, 2;
  -fx-background-radius: 0 0 0 0, 0 0 0 0, 0 0 0 0;
  -fx-padding: 0.166667em 1.166667em 0.25em 0; /* 2 14 3 0 */
  -fx-color: -fx-focus-color;
}

.connectivity .titled-pane > .title > .arrow-button .arrow {
  -fx-background-color: white;
}
 类似资料:
  • 我正在使用javaFX。我做了一个按钮并为此设置了一个图像。代码是: 但是我想当我点击按钮时,图像会变成另一张图片。我该怎么做呢?

  • 主要内容:创建标题窗格标题窗格是具有标题的面板,窗格可以打开和关闭。我们可以添加节点(如UI控件或图像)和一组元素到窗格。 上面的代码生成以下结果。 创建标题窗格 要创建一个控件,请调用其构造函数。 以下代码使用的两个参数构造函数。它将标题窗格命名为“我的窗格”,并用一个控件填充窗格。 接下来的几行做了与上面的代码相同的事情,但不使用带参数的构造函数。 它创建一个带有默认空构造函数的,然后再设置控件的标题和内容。 以下

  • 问题内容: 我想在的标题上添加一个小图标。因此,我设置了一个空标题并添加了一个包含a 和as的图形。这样,图标显示在文本结尾附近。我希望它始终显示在的右侧边框旁边。我怎样才能做到这一点?我也尝试使用a 并将其添加到中间,在右侧添加,但是没有获得TitledPane的最大大小。所以我试图将MaxWidth设置为Max- Value,但这没有帮助 有人知道该怎么办吗? 编辑:我创建的“自定义”控件将在

  • 我想在TextField中更改字体颜色。我找到了、来更改背景和边框的颜色,但没有找到文本的颜色。

  • 树细胞是定制的,它看起来像下图。 在右侧,我选择了一棵树单元格或树项目。正如您所看到的,每个单元格的左侧都有手的图像视图。默认情况下,它是黑色的,但我想用白色图标替换它。如上所述。 我希望所有的文本和图像视图图标在选择改变为白色。和最后选择的树单元格回到正常的黑色。 我的树细胞代码如下。 问题是正确选择并添加了新的白色图标,但如何将上一个选定树项目的图像视图改回黑色图标。实际上,我有两张相同类型的

  • 我在论坛上发现了这样的sollution,正如我所说的,它只检测添加/删除对象