Simple way to create a vertical ButtonBar using JavaFX Scene Builder/Gluon - javafx

How do I make a vertical ButtonBar in JavaFX 8?
I want exactly the same concepts of the ButtonBar component, but ButtonBar is horizontal, I want vertical. All the buttons must be same size.
If I use a VBox I have to set the width of the buttons and of the VBox manually.
Is there a simpler way to do that without having to set widths?

You need two things: to set fillWidth to true on the VBox, and to set maxWidth on each button. The most convenient approach is probably a simple subclass of VBox:
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
public class VerticalButtonBar extends VBox {
public VerticalButtonBar() {
setFillWidth(true);
}
public void addButton(Button button) {
button.setMaxWidth(Double.MAX_VALUE);
getChildren().add(button);
}
}
and then you can do things like
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class VerticalButtonBarExample extends Application {
#Override
public void start(Stage primaryStage) {
VerticalButtonBar bar = new VerticalButtonBar();
bar.addButton(new Button("A"));
bar.addButton(new Button("Button"));
BorderPane root = new BorderPane();
root.setLeft(bar);
primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
which looks like
See Adding a custom component to SceneBuilder 2.0 if you want to make the VerticalButtonBar visible to SceneBuilder.

As far as I can see, ButtonBar has no way to change this, so there is nothing SceneBuilder can do about it.
You might want to file a feature request for ButtonBar: http://bugs.java.com/

Related

JavaFX ColumnConstraint minWidth being ignored, preWidth used, when resizing Window

I'm I wrong to think that when resizing a window for a JavaFX-based application, the configured minimum size for a column width constraint should be used instead of the preferred size?
The program is pretty simple, from a book: JavaFX 9 By Example, with some minor changes to work in Eclipse with JavaFX 15.
First a picture showing the desired outcome. I've set the fxml ColumnConstraints prefWidth value to 50, which is the same as the minWidth.
Now, the same application is launched with a single change: the prefWidth is set to 150 instead of 50. When making the horizontal window size smaller with the mouse, you can see that the application is not using the minWidth setting (minWidth was not changed). I didn't shrink the horizontal to its minimum, to make it more visually obvious that the contents are being clipped.
Following are the two additional classes in this project (am skipping displaying the module-info). But I don't think there is anything here that affects the gist of the question.
package com.jfxbe;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class FXMLContactFormX extends Application {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Application.launch(args);
}
#Override
public void start(Stage stage) {
stage.setTitle("FXMLContactForm ");
Parent root = null;
try {
root = FXMLLoader.load(getClass().getResource("ContactFormX.fxml"));
} catch (IOException e) {
e.printStackTrace();
}
Scene scene = new Scene(root, 380, 150, Color.WHITE);
stage.setScene(scene);
stage.setMinWidth(200);
stage.setMinHeight(150);
stage.show();
}
}
package com.jfxbe;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
public class ContactFormXController {
#FXML private TextField firstNameField;
#FXML private TextField lastNameField;
#FXML private Button saveButton;
#FXML
protected void saveContactAction(ActionEvent event) {
System.out.println("Saving the following information: ");
System.out.println("First Name: " + firstNameField.getText());
System.out.println(" Last Name: " + lastNameField.getText());
}
}
Am I missing something in how column constraints work? Is there documentation that explains what is going on with the minWidth being ignored or not?

How to make a suttering window in JavaFx?

I need a window that can be move up down in my main window. Mainly IDE(IntelliJ, CodeBlock, Netbeans etc) output window feature.
I use TitledPane. By using this I can give a height that is expanded when I click the pane, but I can't expand the pane in any height.
see this - not expanded:
and this- expanded:
My code is here
Try using SplitPane. I guess this is what you want. You can learn more from the following link
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/SplitPane.html
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
SplitPane splitPane = new SplitPane(new Pane(), new Pane());
splitPane.setDividerPositions(0.5);
splitPane.setOrientation(Orientation.VERTICAL);
primaryStage.setScene(new Scene(splitPane,400,400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Change the size of the caret in a TextField (JavaFX 8)

I'm trying to resize the caret in a TextField without modifying the font size, but I can not do it.
Does anyone know how to do that?
Thanks!
I have no idea why you would want to do this. And this probably won't give you exactly what you want, but it does change the size of the caret. Note that the big caret will still be clipped to be within the bounds of the containing TextField.
The small line between the m and the a is the small caret. Image transfer into stackoverflow reduced clarity so it is hard to see:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class BiglyCaret extends Application {
#Override
public void start(Stage stage) throws Exception {
TextField bigCaretTextField = new TextField("Big Caret");
bigCaretTextField.setSkin(
new TextFieldCaretControlSkin(
bigCaretTextField,
2
)
);
TextField smallCaretTextField = new TextField("Small Caret");
smallCaretTextField.setSkin(
new TextFieldCaretControlSkin(
smallCaretTextField,
0.5
)
);
TextField normalTextField = new TextField("Standard Caret");
VBox layout = new VBox(
10,
bigCaretTextField,
smallCaretTextField,
normalTextField
);
layout.setPadding(new Insets(10));
stage.setScene(new Scene(layout));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
TextFieldCaretControlSkin.java
Note, this uses a private API class in Java 8, so it won't work for Java 9. In Java 9, the TextFieldSkin will become public API (move to a javafx package instead of a com.sun package), so it will probably work in Java 9 if you change the package.
import com.sun.javafx.scene.control.skin.TextFieldSkin;
import javafx.scene.control.TextField;
public class TextFieldCaretControlSkin extends TextFieldSkin {
public TextFieldCaretControlSkin(TextField textField, double scale) {
super(textField);
setScale(scale);
}
private void setScale(double scale) {
caretPath.setScaleX(scale);
caretPath.setScaleY(scale);
}
}
This answer was based on this answer:
Hide input caret of TextField in JavaFX8

JavaFX: set an overlapping text on a button when clicked

I'm creating a simple lottery game where I can chose numbers from 1 to 42. I would like to mark the chosen number with a red "X" (instead of using an effect) but the user should still see the chosen number beneath the "X". When the user clicks the marked number again, the "X" should disappear.
The GUI looks like this:
enter image description here
How can I display a second text on a button in the way that the first text is still visible?
Thanks for your help
kind regards
Joel
You can set the Graphic of a button to a StackPane and then add an element to the StackPane. Since the children of a StackPane are all displayed on top of each other, this yields the effect that you want. My example just shows the concept, I would probably write a new class that extends Button in your use scenario :)
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
public class NewFXMain extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Pane pane = new Pane();
StackPane buttonPane = new StackPane();
Label label = new Label("42");
buttonPane.getChildren().add(label);
Button button = new Button();
button.setGraphic(buttonPane);
button.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
if(buttonPane.getChildren().size() == 1){
Label labelX = new Label("X");
labelX.setStyle("-fx-text-fill: red;");
buttonPane.getChildren().add(labelX);
}
else
buttonPane.getChildren().remove(1);
}
});
pane.getChildren().addAll(button);
primaryStage.setScene(new Scene(pane, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

javafx 8 create images on button on hover using css

I'm making a Mario themed application, and I would like the buttons to have the effect that when the user hovers over it with his/her mouse, a mushroom (image) appears next to the text of the button, and disappears when the mouse moves away.I'm trying to do this using css.
How can one do this?
Use the hover pseudoclasses.
.button:hover{
-fx-graphic: url('your_path_to_image');
}
Complete Example
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Main extends Application {
public void start(Stage primaryStage) {
Button button = new Button("Click");
HBox container = new HBox(button);
container.setAlignment(Pos.CENTER);
Scene scene = new Scene(container, 200, 200);
scene.getStylesheets().add(getClass().getResource("/style.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
style.css
.button:hover{
-fx-graphic: url('http://files.softicons.com/download/game-icons/super-mario-icons-by-sandro-pereira/png/16/Mushroom%20-%201UP.png');
}
Image
On hover

Resources