So, I can't seem to make the HBox show up in my application. The Button shows up, but the HBox does not. Can someone please let me know what I'm doing wrong?
package cyanlauncher;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
public class Main extends Application {
private int numApps = 0;
private float btnSize = 48;
private float btnSpacing = 12;
#Override
public void start(Stage primaryStage) {
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.setTitle("CyanLauncher");
Button quitBtn = new Button();
Pane root = new Pane();
root.getChildren().add(addHBox());
quitBtn.setGraphic(new ImageView(new Image(getClass().getResourceAsStream("./../data/exit.png"))));
quitBtn.setLayoutX(10 + ((btnSize + btnSpacing) * numApps));
quitBtn.setLayoutY(7);
quitBtn.setPadding(new Insets(0,0,0,0));
quitBtn.setPrefSize(btnSize, btnSize);
quitBtn.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent event){
System.exit(0);
}
});
root.getChildren().add(quitBtn);
Scene scene = new Scene(root,64,64);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
scene.setFill(null);
primaryStage.setScene(scene);
primaryStage.show();
}
public HBox addHBox() {
HBox hbox = new HBox();
hbox.setPrefSize(64, 64);
hbox.setLayoutX(10);
hbox.setLayoutY(7);
return hbox;
}
public static void main(String[] args) {
launch(args);
}
}
And here is my CSS:
/* JavaFX CSS - Leave this comment until you have at least create one rule which uses -fx-Property */
.root {
-fx-background-color: rgba(0,255,0,1);
}
.button {
-fx-background-color: rgba(0,0,255,1);
}
.hbox {
-fx-background-color: rgba(255,0,0,1);
}
HBox has no style class by default (see docs). So you need to do hbox.getStyleClass().add("hbox") in your (really badly named) addHBox() method.
Related
I'm using NetBeans last version and macOS. I try this is my code and JOptionPane.showMessageDialog not working. It's working if I put the syntax in the main. Please tell me why. I try vscode, and I have same problem
import javax.swing.*;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.TextInputDialog;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class App extends Application {
TextField txt;
public void start(Stage primaryStage) {
Label lb1 = new Label();
TextField txt = new TextField("Type here");
RadioButton rb1 = new RadioButton();
RadioButton rb2 = new RadioButton();
Button bt = new Button("click");
Button bt1 = new Button("anas aljaghbeer");
MyHandlerClass handler1 = new MyHandlerClass();
bt.setOnAction(handler1);
txt.setPrefSize(10, 10);
lb1.setText("Enter here");
txt.getText();
VBox box = new VBox();
Scene scene = new Scene(box, 1000, 1000);
box.getChildren().addAll(lb1, txt, bt);
primaryStage.setTitle("anas");
primaryStage.setScene(scene);
primaryStage.show();
}
class MyHandlerClass implements EventHandler<ActionEvent> {
#Override
public void handle(ActionEvent e) {
JOptionPane.showMessageDialog(null, " Hello");
}
}
public static void main(String[] args) {
Application.launch(args);
}
}
JOptionPane.showMessageDialog()…It's working if I put the syntax in the main. Please tell me why.
When you invoke JOptionPane.showMessageDialog() in main(), it executes on the initial thread. In a Swing program, you would invoke it from main() like this:
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JOptionPane.showMessageDialog(null, "Click to continue…");
…
});
}
In a JavaFX program you are well advised not to "mix Swing and JavaFX," unless you account for JavaFX-Swing Interoperability. Instead, evoke an Alert as shown here and below:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class App extends Application {
TextField txt;
#Override
public void start(Stage primaryStage) {
Label label = new Label();
TextField text = new TextField("Type here");
Button button = new Button("Click");
MyHandlerClass handler = new MyHandlerClass();
button.setOnAction(handler);
text.setPrefSize(10, 10);
label.setText("Enter here");
text.getText();
VBox box = new VBox();
box.getChildren().addAll(label, text, button);
Scene scene = new Scene(box, 320, 240);
primaryStage.setScene(scene);
primaryStage.setTitle("anas");
primaryStage.show();
}
class MyHandlerClass implements EventHandler<ActionEvent> {
#Override
public void handle(ActionEvent e) {
Alert alert = new Alert(Alert.AlertType.INFORMATION, "You clicked the button.");
alert.showAndWait();
}
}
public static void main(String[] args) {
Application.launch(args);
}
}
I create a button and I want to set up its width and height through css. I tried this but it didn't work.
Other properties( such as font-size and text fill) are fine and work correctly. But the width of Button seems to be fixed and doesn't change.
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
VBox root = new VBox();
Button button = new Button("Button");
root.getChildren().add(button);
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("Style.css").toExternalForm());
button.setId("button");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
And in "Style.css":
#button{
-fx-width: 300;
-fx-font-size:20;
-fx-text-fill: yellow;
}
I am trying to add some custom css to a button of mine, the css file is in the same folder as my testButton.java. this is my main/only class:
import com.jfoenix.controls.JFXButton;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.stage.Window;
public class testButton extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Vulpix Skyen");
GridPane gridPane = createRegistrationFormPane();
addUIControls(gridPane);
Scene scene = new Scene(gridPane, 800, 500);
scene.getStylesheets().clear();
scene.getStylesheets().add(getClass().getResource("test.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
private GridPane createRegistrationFormPane() {
GridPane gridPane = new GridPane();
return gridPane;
}
private void addUIControls(GridPane gridPane) {
JFXButton jfoenixButton = new JFXButton("JFoenix Button");
JFXButton button = new JFXButton("Raised Button".toUpperCase());
button.getStyleClass().add("button-raised");
jfoenixButton.getStyleClass().add("button-raised");
gridPane.add(jfoenixButton, 0, 0);
gridPane.add(button, 1, 0);
}
public static void main(String[] args) {
launch(args);
}
}
and here is the css file:
.button-raised {
-fx-padding: 0.7em 0.57em;
-fx-font-size: 140px;
-jfx-button-type: raised;
-fx-background-color: rgb(77, 102, 204);
-fx-pref-width: 200;
-fx-text-fill: ORANGE;
}
And no matter what I change, my button stays the same default style. nothing in particular i am trying to add with the css, but no idea why its not changing at all.
You are not adding the styled button to the gridPane. The only button added to the pane is jfoenixButton which does not have the button-raised class.
Either add the class to that button too:
jfoenixButton.getStyleClass().add("button-raised");
Or add the styled button to your gridPane:
gridPane.add(button, 1, 0);
One of the options should solve your problem.
You should import your CSS in your GridPane (with Scene Builder) to allow you utilize jfoenixButton.getStyleClass().add("button-raised");
I'm very new to JavaFX. I want to communication between two window in my program, but I don't know how to do it. For example: after login successfully, another screen will pop up (but I don't want to load the file FXML because I used the loop to generate the toggle buttons in this window instead of dragging-dropping in FXML file). I don't know whether it makes sense or not. If not please forgive me :(
Main.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parentroot=FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Login");
primaryStage.setScene(new Scene(root, 400,300));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Controller.java
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.io.IOException;
public class Controller {
#FXML
private TextField usernameField;
#FXML
private TextField passwordField;
#FXML
public void login(){
if(usernameField.getText().equals("admin") && passwordField.getText().equals("123")){
successLogin();
}
}
public void successLogin(){
try {
Parent root = FXMLLoader.load(getClass().getResource("anotherscene.fxml"));
Stage stage = new Stage(StageStyle.DECORATED);
stage.show();
}catch (IOException e){
e.printStackTrace();
}
}
}
AnotherScene.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class AnotherScene implements Initializable {
ToggleButton[][] matrix;
private static final int SIZE = 10;
private static int length = SIZE;
private static int width = SIZE;
#FXML
private BorderPane borderPane;
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
Parent root = null;
try{
root = FXMLLoader.load(getClass().getResource("anotherscene.fxml"));
}catch (IOException e){
e.getStackTrace();
}
Stage stage = new Stage();
VBox vBox = new VBox();
vBox.getChildren().add(new Label("Seat Number: "));
borderPane.setLeft(vBox);
GridPane gridPane = new GridPane();
gridPane.setHgap(10);
gridPane.setVgap(10);
borderPane.setCenter(gridPane);
matrix = new ToggleButton[length][width];
for(int row = 0; row < length; row++){
for(int col = 0; col < width; col++){
matrix[row][col] = new ToggleButton();
matrix[row][col].setText(row + "" +col);
matrix[row][col].setPrefWidth(40);
gridPane.add(matrix[row][col], row, col);
}
}
// Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
stage.setTitle("Hello World");
Scene scene = new Scene(root, 700, 500);
stage.setScene(scene);
stage.show();
}
}
When I change scenes on my stage with the following code, my stage changes size. However the button in the top right to maximize/minimize the windows says that the stage is still maximized even though it is clearly not.
How am I able to keep the stage maximized when a scene change happens?
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class Program2 extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
try {
StackPane p = new StackPane();
primaryStage.setTitle("Chart Application");
Label loader = new Label("Loading...");
loader.setGraphic(new ImageView(new Image("https://media.giphy.com/media/FmcNeI0PnsAKs/giphy.gif")));
loader.setFont(new Font(35));
p.setStyle("-fx-background: #FFFFFF;");
p.getChildren().add(loader);
StackPane.setAlignment(loader, Pos.CENTER);
primaryStage.setScene(new Scene(p));
primaryStage.setMaximized(true);
Task<VBox> task = new Task<VBox>() {
#Override
public VBox call() {
VBox result = new VBox();
for(int i = 0; i < 50000; i++) { //Here simply for small delay
result.getChildren().add(new Label(Integer.toString(i)));
}
return result ;
}
};
task.setOnSucceeded(e -> {
VBox result = task.getValue();
ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(result);
scrollPane.setStyle("-fx-background: #FFFFFF;");
primaryStage.setScene(new Scene(scrollPane));
});
new Thread(task).start();
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
As this is operating system specific I image I am using Windows 10 with JDK 8 u112 and JavaFX 8 with the e(fx)clipse plugin for eclipse
Instead of replacing the scene, use the same scene and replace its root:
primaryStage.getScene().setRoot(scrollPane);