Is different font sizes possible in a button in javafx - css

final Button btnl2 = new Button("SIGN IN");
btnl2.setFont(Font.font("Calibri", FontWeight.BOLD, 16));
btnl2.setPrefSize(150, 60);
btnl2.setStyle(" -fx-base: #0066cc;");
Image imageOk1 = new Image(getClass().getResourceAsStream("signin.png"));
btnl2.setGraphic(new ImageView(imageOk1));
This is the code of one of the buttons in my project. The .png image is displayed in middle of the button. I want to know 2 things :
1) Is it possible to display it to the left side of the button ?
2) Is it possible to have different font sizes in the button ? Now my button has "SIGN IN" label on it. I want to add a new label to the same button with a different font size below the "SIGN IN" label.

1) yes IIRC it is named ContentDisplay but i could be wrong and it's named differently
2) yes but you don't use the text attribute any more but eg but a mixtutre of hbox/vbox/label/imageview on the graphic attribute who accepts a Node
Button b = new Button();
VBox box = new VBox();
box.getChildren().addAll(new Label("line1"), new Label("line2"));
b.setGraphic(box);

Related

Placing buttons on opposite sides of an adjustable window in JavaFX

I have two buttons that I'd like to use in my display screen: a reset button, and a settings button.
The problem is, I want them on opposite sides of the window (reset in the top right corner and settings in the top left) while being the same height. When I expand the window, however, the buttons stay in the same place, but I want them to move with the window walls.
I first tried using setAlignment of my reset button to Pos.*TOP_RIGHT* and the settings button to top left, but it comes out with the settings button being on top of the reset button.
resetButton = new Button("Reset Gameboard");this.setTop(resetButton);
resetBox = new HBox(resetButton);
resetBox.setAlignment(Pos.TOP_RIGHT);
backButton = new Button("Back to Settings");
this.setTop(backButton);
backBox = new HBox(backButton);
backBox.setAlignment(Pos.TOP_LEFT);
resetbackBox = new HBox(backButton, resetButton);
this.setTop(resetbackBox);`
I currently am using (below) which during the initial window appears to be working and correctly spaced, but as soon as you expand the window the reset button doesn't follow it, although the settings button does.
`resetButton = new Button("Reset Gameboard");
this.setTop(resetButton);
resetBox = new HBox(resetButton);
resetBox.setAlignment(Pos.TOP_RIGHT);
backButton = new Button("Back to Settings");
this.setTop(backButton);
backBox = new HBox(backButton);
backBox.setAlignment(Pos.TOP_LEFT);
resetbackBox = new HBox(backButton, resetButton);
resetbackBox.setSpacing(380);
this.setTop(resetbackBox);`
The problem start from this line:
resetbackBox.setSpacing(380);
You have setted a fixed spacing value. To fix that you can easily add a component to HBox called Region, this component allow to set a grow area between two buttons:
Button resetButton = new Button("Reset Gameboard");
Button backButton = new Button("Back to Settings");
Region region = new Region();
HBox.setHgrow(region, Priority.ALWAYS);
HBox topHBox = new HBox(backButton, region, resetButton);
this.setTop(resetbackBox);

Bold Text Parts in ControlsFX Notification

I'm trying to write bold text inside the body of a Notification.
Notifications.create()
.title("My Title")
.text("This is <bold>text</bold>")
.showInformation();
As far as I can tell there's only the .text("my text") method to set the text of a Notification.
However I'd like to use a TextFlow in there to modify certain parts of a text.
I've also tried to use CSS for this but that only lets me change to overall appearances of the Notification as setting specific text through CSS is explicitly not supported.
My question now is: Is there a way to style parts of a text inside a Notification differently?
Got it working now.
The solution is to use .graphic(Node node). If you still want to display an Image at the left side you need to put everything in a Pane and add some padding.
I yoinked the original image from here.
BorderPane borderPane = new BorderPane();
borderPane.setLeft(new ImageView(Controller.class.getResource("/dialog-information.png").toExternalForm()));
TextFlow textFlow = new TextFlow();
textFlow.setPadding(new Insets(15, 0, 5, 5));
Text text1 = new Text("This is ");
Text text2 = new Text("bold");
text2.setStyle("-fx-font-weight: bold");
textFlow.getChildren().addAll(text1, text2);
borderPane.setRight(textFlow);
Notifications.create()
.title("My Title")
.graphic(borderPane)
.hideAfter(Duration.seconds(10))
.show();
It is important to use .show() and not .showInformation/Error/Warning() because that would override the BorderPane.
Result:

Tab text alignment in QTabWidget

I am using QTabWidget class of Qt.
In TabWidget i am dynamically adding new tab and setting the TextElideMode to right to display the toolbutton in tabBar.
tabWidget = new QTabWidget(this);
m_addNewTab = new QWidget(tabWidget);
m_addNewGridLayout = new QGridLayout(m_addNewTab);
m_addNewWebView = new MyWebView(m_addNewTab);
widget = new QWidget(m_addNewTab);
tb = new QToolButton(widget);
tb->setFixedHeight(20);
tb->setText("<");
tb1 = new QToolButton(widget);
tb1->setFixedHeight(20);
tb1->setText(">");
m_horizontalLayout = new QHBoxLayout(widget);
m_horizontalLayout->addWidget(tb);
m_horizontalLayout->addWidget(tb1);
Please see the below screen shot for the output of the sample application.
When the current tab is selected then both the toolbutton should display and text elide mode should be right but when tab is not selected then toolbutton should not be displayed but text elide mode should be left.
Here in below screen shot i am able to hide and show the toolbutton depending on tab selection but when the tab is not selected text elide mode is setting as right so we are able to see the unnecessary space (check last tab). Setting the text elide mode left also not working because we have already set the toolbutton at left side.
Can someone guide me how to remove the space (last tab from screen shot) when there is not tab selected ?
You'll have to:
Keep track of tab index and corresponding holding widget (let's say std::map<int,QToolButton> toolbutton_by_index
When QTabWidget.currentChanged is emitted, deactivate all widgets except the selected
You can do the 2nd part like this:
std::for_each(toolbutton_by_index.begin(), toolbutton_by_index.end(),
[&index](auto pair){
(pair.first == index)?pair.second->hide():pair.second->show()});

set spacing among elements within button javafx

I want to specify spacing between two elements within JavaFX Buttons, Here is the Code :
ImageView fiv = new ImageView(new Image("/modified/map.png"));
fiv.setFitHeight(20);
fiv.setPreserveRatio(true);
Button cr = new Button( "Crop", fiv);
Here I want to Specify Spacing Between "Crop" and fiv, How can i do this ?
Use graphicTextGap property of the button.

JavaFX buttons with same size

I have these buttons with different size:
Image
How I can make all buttons with same with size?
It depends on layout where the button is located. For example, if you add all the buttons into GridPane or BorderPane, you have to specify each button width to correspond to certain variable. In the following example I wrap all buttons inside VBox, set VBox preference width and tie up all buttons minimum width to it:
VBox vBox = new VBox();
vBox.setPrefWidth(100);
Button btn1 = new Button("Short");
Button btn2 = new Button("Super Long Button");
btn1.setMinWidth(vBox.getPrefWidth());
btn2.setMinWidth(vBox.getPrefWidth());
vBox.getChildren().addAll(btn1, btn2);
It is also worth to mention that there are two ways to specify the button size. You can do it in the java code or specify it in javafx .fxml file. The above method is an example for java code implementation.
You can also unclamp a button's maximum dimensions so it will grow to fill the available space (unlike most nodes, by default a button node has it's max size clamped to it's preferred size so it doesn't usually grow to fill available space). An Oracle tutorial on Tips for Sizing and Aligning Nodes explains this in more detail.
VBox vBox = new VBox();
Button btn1 = new Button("Short");
Button btn2 = new Button("Super Long Button");
btn1.setMaxWidth(Double.MAX_VALUE);
btn2.setMaxWidth(Double.MAX_VALUE);
vBox.getChildren().addAll(btn1, btn2);
using css you can override the preferred width of all buttons like
.button {
-fx-pref-width: 200px;
}
or create your own style class for certain button groups and add the style to the button like:
css:
.my-special-button {
-fx-pref-height: 28px;
-fx-pref-width: 200px;
}
and then set the style to your button with either
fxml:
styleClass="my-special-button"
or in java
myButton.getStyleClass().add("my-special-button");

Resources