Hide the title of a TitledPane in JavaFX 2.2

会有一股神秘感。 提交于 2020-01-16 11:57:29

问题


A TitledPane features a title. Here is how a couple of them can look:

The titles are "More..", "Smileys" and "Send". I want to completely hide the Send title, not just remove the text "Send". The end result should be something like this:

Is it possible?


回答1:


I would just use a standard Pane for the third content area rather than a TitledPane and apply the relevant styles to trick JavaFX into styling the bottom panel as if it was the content area of a TitlePane.

Roughly speaking you will require some FXML markup similar to this:

<VBox styleClass="titled-pane" 
      xmlns:fx="http://javafx.com/fxml/1"  
      xmlns="http://javafx.com/javafx/2.2" >
    <children>
        <TitledPane animated="false" text="untitled">
            <content>
                <AnchorPane minHeight="0.0" 
                            minWidth="0.0" 
                            prefHeight="180.0" 
                            prefWidth="200.0" />
            </content>
        </TitledPane>
        <TitledPane animated="false" text="untitled">
            <content>
                <AnchorPane id="Content" 
                            minHeight="0.0" 
                            minWidth="0.0" 
                            prefHeight="180.0" 
                            prefWidth="200.0" />
            </content>
        </TitledPane>
        <Pane prefHeight="200.0" prefWidth="200.0" styleClass="content">
            <children>
                <Button layoutX="74.0" 
                        layoutY="21.0" 
                        mnemonicParsing="false" 
                        text="Button" />
            </children>
        </Pane>
    </children>
</VBox>

This basically lays the three panes out in a VBox so that they stack correctly and applies some styles to tell JavaFX how to render the third Pane.

In order to achieve the correct look of the third Pane you will need to give it a style class of "content". This is the name of the background Pane that is part of the TitledPanes sub-structure and tells JavaFX to render the pane in the same way as the TitledPane control.

This will not work as it stands though as the actual css definition looks something like this:

.titled-pane .content { // styles defined here }

What this means is that the style will only apply to nodes that have a style class of "content" if they are also inside a node with a style class of "titled-pane".

The simple way to fix this is to give the root container Pane (the VBox in this case) a style class of "titled-pane", effectively tricking JavaFX into thinking the third pane is a titledPanes content area.

The output of this is shown below:

and with both TitledPanes collapsed:



来源:https://stackoverflow.com/questions/17513541/hide-the-title-of-a-titledpane-in-javafx-2-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!