wlfbck
wlfbck

Reputation: 527

How do i create a labeled Separator?

edit: Since it seems to be unclear: I want a normal JavaFx Separator (like in the second picture below), but with text on it. Here's an example how it looks like in Swing (altough i don't want to put a whole box around something!):

enter image description here

This seems like something that should really be simple, but the the Separator class that JavaFX comes with can't be labeled. Result should look something like the "general options" or "specific options" in this:

enter image description here

I found http://tiwulfx.panemu.com/2013/01/02/creating-custom-menu-separator-in-javafx/ and tried to apply that to the normal Separator, but since you can't set content at all for that one it didn't work (and you can't use the MenuItemSeparator in other GUI parts then Menu). The shown Options will be a Tab in a TabPane.

Any ideas on how to do this? :)

SOLUTION:

HBox labeledSeparator = new HBox();
Label label = new Label(text);
Separator leftSeparator = new Separator();
Separator rightSeparator = new Separator();
labeledSeparator.getChildren().add(leftSeparator);
labeledSeparator.getChildren().add(label);
labeledSeparator.getChildren().add(rightSeparator);
labeledSeparator.setAlignment(Pos.CENTER);

Upvotes: 3

Views: 2636

Answers (2)

CAG Gonzo
CAG Gonzo

Reputation: 589

Why not use an AnchorPane that holds a Separator and a Label? The Separator needs to be anchored to the sides of the AnchorPane while the Label can be anchored at an offset to the left side, say around 20.0. Then, make the background of the Label match the background of your program so the portion of the Separator obscured by the Label is not visible.

Upvotes: 3

Mansueli
Mansueli

Reputation: 6984

It looks like you want a menu with custom CSS and a TabPane underneath it, below I show you an example made with FXML.

enter image description here

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="382.0" prefWidth="592.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children><MenuButton layoutX="0.37109375" layoutY="1.5" mnemonicParsing="false" text="MenuButton">
  <items>
    <MenuItem mnemonicParsing="false" text="Action 1" />
    <MenuItem mnemonicParsing="false" text="Action 2" />
  </items>
</MenuButton><MenuButton layoutX="99.0" layoutY="1.5" mnemonicParsing="false" text="MenuButton">
  <items>
    <MenuItem mnemonicParsing="false" text="Action 1" />
    <MenuItem mnemonicParsing="false" text="Action 2" />
  </items>
</MenuButton><MenuButton layoutX="197.62890625" layoutY="1.5" mnemonicParsing="false" text="MenuButton">
  <items>
    <MenuItem mnemonicParsing="false" text="Action 1" />
    <MenuItem mnemonicParsing="false" text="Action 2" />
  </items>
</MenuButton><Separator layoutX="2.0" layoutY="24.0" prefHeight="5.0" prefWidth="590.0" /><Pane layoutX="2.0" layoutY="33.0" prefHeight="350.0" prefWidth="590.0">
<children><TabPane layoutY="-8.0" prefHeight="350.0" prefWidth="590.0" tabClosingPolicy="UNAVAILABLE">
  <tabs>
    <Tab text="Start">
      <content>
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
      </content>
    </Tab>
    <Tab text="ConvergenceRate1">
      <content>
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
      </content>
    </Tab><Tab text="FitneesFunction1">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
  </tabs>
</TabPane>
</children></Pane>
</children></Pane>

Upvotes: 0

Related Questions