Adding buttons to Hbox - button

Dipping my toes for the first time in javaFX and I'm having some problems with adding buttons to a Hbox. It seems like the box doesn't like the type buttons being added. Have no clue why, so just cheking if this has happened to anyone else.
Button knapp1 = new Button("Alphabetical");
Button knapp2 = new Button("Frequency");
HBox hbox = new HBox();
hbox.getChildren().addAll(knapp1, knapp2);
It's the "addAll" method that's the problem, it gives the error
The method addAll(int, Collection<? extends Node> in the type List
<Node> is not applicable for the arguments (Button, Buttons) )
Thanks in advance for what may be a stupid question.

Here is a runnable example as said in the comments check your imports
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage stage) {
Button knapp1 = new Button("Alphabetical");
Button knapp2 = new Button("Frequency");
HBox hbox = new HBox();
hbox.getChildren().addAll(knapp1, knapp2);
Scene scene = new Scene(hbox);
stage = new Stage();
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) { launch(args); }
}
When run with these imports
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import java.awt.*;
I get the same error as you remove the import java.awt.*; and replace with import javafx.scene.control.Button;

Related

Javafx label not updating after input from TextField in another scene

I have a label called test that is supposed to display the input value from TextField in the textInput class, from another scene. the value is being sent over to the main class but the label is not updating unless i click the button to go to the dialog box from textInput.
package javafx11;
import application.textInput;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextInputDialog;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
public class Main extends Application {
Stage window = new Stage();
Scene s1;
Scene s2;
Label test;
String input;
Button btn;
#Override
public void start(Stage primaryStage)throws Exception {
window=primaryStage;
VBox layout = new VBox();
s1= new Scene(layout,500,500);
test = new Label("This is where your text will appear");
btn = new Button("Click me");
window.setTitle("Dummy program");
layout.setAlignment(Pos.CENTER);
window.setScene(s1);
window.show();
btn.setOnAction(e -> {
input = textInput.textInput("title", "mnessage");
test.setText(input);
window.setScene(s1);
System.out.println(input);
});
layout.getChildren().addAll(test, btn);
}
public static void main(String[] args) {
launch(args);
}
}
the text input class:
package application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TextInputDialog;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class textInput {
static String input;
public static TextField userField;
public static String textInput(String title, String message) {
Stage window = new Stage();
VBox layout = new VBox();
window.setTitle(title);
window.initModality(Modality.APPLICATION_MODAL);
Button btn = new Button("Click me to go back");
userField = new TextField();
Scene s1 = new Scene(layout, 500, 500);
window.setScene(s1);
layout.getChildren().add(btn);
layout.getChildren().add(userField);
btn.setOnAction(e ->{
input = userField.getText();
window.close();
System.out.println(input);
});
layout.setAlignment(Pos.CENTER);
window.show();
return input;
}
}
I tried googling it but i cant really seem to understand the solution provided by others.
Its all good, figured it out after tinkering with it a little longer.
in the main class, i had to change window.show(); to window.showAndWait();

ControlsFx Decoration on a Label

I would like to apply ControlsFx decorations in a TableCell and as such would like to apply them to a Label.
This following does NOT apply the decoration to the Label. Should it?
import org.controlsfx.control.decoration.Decorator;
import org.controlsfx.control.decoration.GraphicDecoration;
import org.controlsfx.validation.decoration.GraphicValidationDecoration;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
public class LabelDecoration extends Application {
private static final Image REQUIRED_IMAGE = new Image(GraphicValidationDecoration.class.getResource("/impl/org/controlsfx/control/validation/required-indicator.png").toExternalForm()); //$NON-NLS-1$
#Override
public void start(Stage primaryStage) throws Exception {
Label label = new Label("Test");
Node requiredDecoration = new ImageView( REQUIRED_IMAGE );
Decorator.addDecoration( label, new GraphicDecoration( requiredDecoration, Pos.TOP_LEFT ));
primaryStage.setScene( new Scene( label, 100, 100 ));
primaryStage.show();
}
public static void main(String[] args) {
launch( args );
}
}
The Decorator tries to install a DecorationPane into the scene, which does not exist yet in your case.
Wrap the line Decorator.addDecoration(...) in a Platform.runLater(...) and it will work.

JavaFX, full screen: app blinks when alert is shown

I have a very simple application in JavaFX: full screen stage with one exit button. Problem is that from time to time (no idea why not always) after clicking on "Exit" button my application blinks (seems that is minimized and maximized in miliseconds).
Any ideas?
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.util.Optional;
public class FullScreenExample extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
Button button = new Button("Exit");
button.setOnAction(event -> {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.initOwner(stage);
alert.initStyle(StageStyle.UNDECORATED);
alert.initModality(Modality.WINDOW_MODAL);
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK){
Platform.exit();
}
});
VBox box = new VBox();
box.getChildren().add(button);
final Scene scene = new Scene(box);
scene.setFill(null);
stage.setScene(scene);
stage.setFullScreenExitHint("");
stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
stage.setFullScreen(true);
stage.show();
}
}
You are using the Stage style Undecorated:
alert.initStyle(StageStyle.UNDECORATED);
Try to use
alert.initStyle(StageStyle.TRANSPARENT);
This fixes your blinking.
Its a known Issue with the Undecorated Frame in Windows.

Create a webview from a button in JavaFX

I have the following simple code where I have an horizontal block where there are two buttons, the remain part is empty. Now, I would like to view a Webview inside the free part after the user press the "current" button. I already did something similar but I wasn't able to manage the layout because it was a Jframe + JavaFX. I thought to re-build totally in JavaFx , for this reason I want to put the webview in a jfxPanel(I think it's the best solution, any other suggest is really appreciate). I should see the webview when I press the button , in this case current, for this reason I create an handle. So, How can I do it?
Thanks in advance
import java.util.ArrayList;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.TilePane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Launcher extends Application {
public static void main(String[] args) {
launch(Launcher.class, args);
}
Button buttonCurrent;
static HBox web;
static MyBrowser browser_lau;
public void start(Stage stage) {
BorderPane border = new BorderPane();
HBox hbox = addHBox();
border.setTop(hbox);
Scene scene = new Scene(border);
stage.setScene(scene);
stage.setTitle("Layout Sample");
stage.show();
buttonCurrent.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
final JFXPanel jfxPanel = new JFXPanel();
//border.setStyle("-fx-background-color: #ff0000;");
}
});
}
public HBox addHBox() {
HBox hbox = new HBox();
hbox.setPadding(new Insets(15, 12, 15, 12));
hbox.setSpacing(10); // Gap between nodes
hbox.setStyle("-fx-background-color: #336699;");
buttonCurrent = new Button("Current");
buttonCurrent.setPrefSize(100, 20);
Button buttonProjected = new Button("Projected");
buttonProjected.setPrefSize(100, 20);
hbox.getChildren().addAll(buttonCurrent, buttonProjected);
return hbox;
}
}
}
Here is some sample code to get you started, it creates a couple of WebViews and toggles between them depending upon a radio button selection.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class CountThePeople extends Application {
private static final String CURRENT_URL =
"http://www.census.gov/popclock/";
private static final String PROJECTED_URL =
"http://www.sciencedaily.com/releases/2015/08/150810110634.htm";
#Override
public void start(final Stage stage) throws Exception {
WebView current = new WebView();
current.getEngine().load(CURRENT_URL);
WebView projected = new WebView();
projected.getEngine().load(PROJECTED_URL);
StackPane viewHolder = new StackPane();
ToggleGroup choice = new ToggleGroup();
RadioButton currentRadio = new RadioButton("Current");
currentRadio.setToggleGroup(choice);
RadioButton projectedRadio = new RadioButton("Projected");
projectedRadio.setToggleGroup(choice);
choice.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {
if (projectedRadio == newValue) {
viewHolder.getChildren().setAll(projected);
} else {
viewHolder.getChildren().setAll(current);
}
});
choice.selectToggle(currentRadio);
VBox layout = new VBox(10, currentRadio, projectedRadio, viewHolder);
layout.setPadding(new Insets(10));
stage.setTitle("World Population");
stage.setScene(new Scene(layout));
stage.show();
}
public static void main(String[] args) throws Exception {
launch(args);
}
}

How to show ProgressIndicator in center of Pane in Javafx

I have a Pane with some controls and a button. When I click on the button, I want show ProgressIndicator in center of pane without removing any controls.
When I am adding a ProgressIndicator to pane during onAction of button, it adds it below the button. I want it to overlay on the pane.
The picture below explains what I want.
Code
package fx;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage arg0) throws Exception {
final VBox bx = new VBox();
bx.setAlignment(Pos.CENTER);
TextField userName = new TextField("User Name");
userName.setMaxWidth(200);
TextField email = new TextField("Email");
email.setMaxWidth(200);
Button submit = new Button("Submit");
submit.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
ProgressIndicator pi = new ProgressIndicator();
//adding here but it is adding at below of button
//how to do here
bx.getChildren().add(pi);
//Further process
}
});
bx.getChildren().addAll(userName, email, submit);
Scene c = new Scene(bx);
arg0.setScene(c);
arg0.setMinWidth(500);
arg0.setMinHeight(500);
arg0.show();
}
public static void main(String[] args) {
Main h = new Main();
h.launch(args);
}
}
You need to use a StackPane as your root layout instead of using a VBox. StackPane allows you to stack nodes on top of each other (z-order).
On the button's action you can create a new ProgressIndicator and add it to your StackPane. I have introduced another VBox as the parent to the indicator, because I did not want the indicator to capture all the available space. You can disable the already present VBox to get the greying effect on button's action after the process is done you can enable the VBox again.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage arg0) throws Exception {
StackPane root = new StackPane();
VBox bx = new VBox();
bx.setAlignment(Pos.CENTER);
TextField userName = new TextField("User Name");
userName.setMaxWidth(200);
TextField email = new TextField("Email");
email.setMaxWidth(200);
Button submit = new Button("Submit");
submit.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
ProgressIndicator pi = new ProgressIndicator();
VBox box = new VBox(pi);
box.setAlignment(Pos.CENTER);
// Grey Background
bx.setDisable(true);
root.getChildren().add(box);
}
});
bx.getChildren().addAll(userName, email, submit);
root.getChildren().add(bx);
Scene c = new Scene(root);
arg0.setScene(c);
arg0.setMinWidth(500);
arg0.setMinHeight(500);
arg0.show();
}
public static void main(String[] args) {
launch(args);
}
}

Resources