A java.lang.NoSuchMethodException error (layouts) - java

I want to display another layout in another window, but whenever I add a new layout to start method
BorderPane sch = new BorderPane();
sch.setCenter(addVBox2(primaryStage));
scene1 = new Scene(sch, 800, 500);
I get this error
Exception in Application start method
Exception in thread "main" **java.lang.NoSuchMethodException**: sample.Main.main([Ljava.lang.String;)
at java.lang.Class.getMethod(Class.java:1786)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:119)
Where might the problem in the code be? Thank you for any help in advance.
public void start(Stage primaryStage) throws IOException {
stage = primaryStage;
BorderPane border = new BorderPane();
border.setLeft(addVBox(primaryStage));
border.setCenter(addGridPane(primaryStage));
border.setRight(addDatePicker(primaryStage));
HBox hbox = addHBox();
border.setBottom(hbox);
scene = new Scene(border, 800, 500);
BorderPane sch = new BorderPane();
sch.setCenter(addVBox2(primaryStage));
scene1 = new Scene(sch, 800, 500);
stage.setScene(scene);
stage.setTitle("Booking Form");
stage.show();
}
private VBox addVBox2(Stage stage) {
javafx.scene.control.Label label = new javafx.scene.control.Label("Choose rooms' schedule");
label.setFont(javafx.scene.text.Font.font("Tahoma", FontWeight.BOLD, 16));
room1 = new RadioButton("Room 1 of type 1");
room1.setSelected(true);
room2 = new RadioButton("Room 2 of type 1");
room3 = new RadioButton("Room 1 of type 2");
room4 = new RadioButton("Room 2 of type 2");
room5 = new RadioButton("Room 1 of type 3");
room6 = new RadioButton("Room 2 of type 3");
VBox buttons2 = new VBox(20, label, roomtype1, roomtype2);
buttons2.setAlignment(Pos.CENTER);
buttons2.setMaxWidth(225);
stackPane.getChildren().add(buttons2);
ToggleGroup toggleGroup = new ToggleGroup();
toggleGroup.getToggles().addAll(roomtype1, roomtype2);
Scene scene = new Scene(stackPane, 400, 200);
stage.setTitle("RadioButton control");
stage.setScene(scene);
stage.show();
return buttons2;
}
private 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;");
hbox.setAlignment(Pos.CENTER);
final Text actionTarget = new Text();
Button schedule = new Button("Schedule");
schedule.setPrefSize(100, 20);
schedule.setOnAction(e -> stage.setScene(scene1));
...}

Exception in thread "main" java.lang.NoSuchMethodException: Main.main([Ljava.lang.String;)
This error means your main class is not found. When you have this kind of error, most of the time it means you moved your main class from a file to another without changing your run configuration.
Edit your run configuration in intellij IDE. Your run configuration is pointing to wrong class it seems. edit run configuration

Related

JavaFX ScrollPane setVvalue() not working properly

I am testing the JavaFX ScrollPane class and realized that it is not working as I expect, I don't know why. I have the following code:
public class Client3 extends Application {
int indexMsg = 0;
Button send;
GridPane root;
ScrollPane msgPane;
GridPane msgPaneContent;
FlowPane writePane;
TextField writeMsg;
Scene scene;
#Override
public void start(Stage primaryStage) {
root = new GridPane();
root.setAlignment(Pos.CENTER);
root.setVgap(10);
root.setPadding(new Insets(10, 10, 10, 10));
msgPane = new ScrollPane();
msgPane.setPrefSize(280, 280);
msgPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
msgPaneContent = new GridPane();
msgPaneContent.setPrefWidth(270);
msgPaneContent.setVgap(10);
writePane = new FlowPane(10, 10);
writePane.setAlignment(Pos.CENTER);
writePane.setPrefWidth(280);
writeMsg = new TextField();
writeMsg.setPrefWidth(150);
writeMsg.setPromptText("Write your message");
writePane.getChildren().add(writeMsg);
GridPane.setConstraints(msgPane, 0, 0);
GridPane.setConstraints(writePane, 0, 1);
msgPane.setContent(msgPaneContent);
root.getChildren().addAll(msgPane, writePane);
writeMsg.setOnAction((ev) -> {
if (!writeMsg.getText().isEmpty()) {
TextArea msg = new TextArea(writeMsg.getText());
msg.setMaxWidth(135);
msg.setPrefRowCount(msg.getLength() / 21 + 1);
msg.setWrapText(true);
GridPane.setConstraints(msg, 0, indexMsg);
indexMsg++;
writeMsg.deleteText(0, writeMsg.getText().length());
msgPaneContent.getChildren().add(msg);
msgPane.setVvalue(1.0);
}
});
scene = new Scene(root, 300, 300);
primaryStage.setTitle("Chat App");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Basically, I have a GridPane as the root with a ScrollPane and a GridPane as its children. The ScrollPane has a children GridPane. There is a TextField with an EventHandler which generates a TextArea inside the GridPane (the ScrollPane's children). Each TextArea object is created in the vertical direction, downwards. I want to set the scrollbar always at its maximum value (setVvalue(1.0)) each time a new TextArea is added. The thing is that it doesn't seem to work as it should because the vertical value is never set to the maximum after handling the event, but it seems to be set to the maximum value that it had before handling it (the bottom of the previous TextArea added).
Any solution for this? Thanks in advance.

How to call a method when a button is clicked in JavaFX?

I'm having a problem with creating a method and calling it when a button is clicked.
What I want to do is when a button is clicked the scene changes to a new one. I have already achieved this but I came across a problem when I need to create more of such actions.
Here is my code:
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
BorderPane borderPane = new BorderPane();
Button dugmeStart = new Button("Start");
dugmeStart.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
BorderPane borderPane2 = new BorderPane();
Label prvo_pritanje = new Label("Prvo pitanje: Kada je poceo Prvi svetski rat?");
prvo_pritanje.setStyle("-fx-font-weight: bold;");
Button dalje1 = new Button("Dalje");
//Navigacija
HBox navigacija_goranj = new HBox();
navigacija_goranj.getChildren().add(prvo_pritanje);
navigacija_goranj.setAlignment(Pos.CENTER);
navigacija_goranj.setPadding(new Insets(15,12, 15, 12));
HBox navigacija_donja = new HBox();
navigacija_donja.getChildren().add(dalje1);
navigacija_donja.setPadding(new Insets(15,12, 15, 12));
navigacija_donja.setAlignment(Pos.CENTER);
//Odgovori
HBox odgovori = new HBox();
ToggleGroup pitanja1 = new ToggleGroup();
RadioButton prvo_pitanje = new RadioButton("1914");
prvo_pitanje.setToggleGroup(pitanja1);
RadioButton drugo_pitanje = new RadioButton("1918");
drugo_pitanje.setToggleGroup(pitanja1);
RadioButton trece_pitanje = new RadioButton("1910");
trece_pitanje.setToggleGroup(pitanja1);
odgovori.getChildren().addAll(prvo_pitanje, drugo_pitanje, trece_pitanje);
odgovori.setSpacing(15);
odgovori.setAlignment(Pos.CENTER);
//Dodavanje u BorderPane
borderPane2.setBottom(navigacija_donja);
borderPane2.setTop(navigacija_goranj);
borderPane2.setCenter(odgovori);
Scene scena2 = new Scene(borderPane2, 300, 300);
primaryStage.setScene(scena2);
}
});
Label dobrodoslica = new Label("Pocetak kviza");
dobrodoslica.setStyle("-fx-font-weight: bold;");
//Definisanje Hbox-a sa dugmetom "Start"
HBox navigacija1 = new HBox();
navigacija1.setPadding(new Insets(15, 12, 15, 12));
navigacija1.getChildren().add(dugmeStart);
navigacija1.setAlignment(Pos.CENTER);
borderPane.setBottom(navigacija1);
borderPane.setCenter(dobrodoslica);
Scene scene = new Scene(borderPane,300,300);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("Domaci: Kviz");
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
You can see that when a button is clicked it creates a whole new scene and BorderPane, and populates it with content. I figured that it would be best to create a method with each scene and then call each method when a button is clicked. The problem is I cant figure out how to do this.
Any help is appreciated. Thanks in advance.

JavaFX: Add textField and Labels to scene

I'm trying to get two tabs, each with their own little form that contain labels and textfields/textareas. I thought my making a "Master" VBox that should display everything but nothing other than the tabs are showing and neither am i getting any errors.
How do I add them to the scene?
// Label and Button variables
private Label fname,lname, appt, AppointmentInfo;
protected String input;
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Person Appointments");
primaryStage.setWidth(500);
primaryStage.setHeight(500);
//Create and set Tab Names
Tab InfoTab = new Tab();
Tab ApptTab = new Tab();
InfoTab.setText("PersonInfo");
ApptTab.setText("Appointments");
// VBoxes
VBox personInfoBox = new VBox();
VBox FirstBox = new VBox();
VBox appointmentBox = new VBox();
// Setup labels and fields
fname = new Label();
lname = new Label();
appt = new Label();
AppointmentInfo = new Label();
fname.setText("First Name");
lname.setText("Last Name");
appt.setText("Appt Count");
AppointmentInfo.setText("Appt Info");
// Text Fields
TextField firstNameText = new TextField();
TextField lastNameText = new TextField();
TextField appointmentText = new TextField();
TextArea apptInfo = new TextArea();
firstNameText.setText("First Name Here");
lastNameText.setText("Last Name Here");
appointmentText.setText("Appointment Here");
personInfoBox.getChildren().add(fname);
personInfoBox.getChildren().addAll(firstNameText);
personInfoBox.getChildren().add(lname);
personInfoBox.getChildren().addAll(lastNameText);
appointmentBox.getChildren().add(AppointmentInfo);
appointmentBox.getChildren().addAll(apptInfo);
// Grid for Tabs
GridPane grid = new GridPane();
GridPane grid2 = new GridPane();
grid.add(fname, 0, 0);
grid.add(firstNameText, 0, 1);
grid.add(lname, 1, 0);
grid.add(lastNameText, 1, 1);
grid.add(appt, 2, 0);
grid.add(appointmentText, 2, 1);
grid2.add(AppointmentInfo, 0, 0);
grid2.add(apptInfo, 0, 1);
InfoTab.setContent(grid);
ApptTab.setContent(grid2);
// TabPane
TabPane tabPane = new TabPane();
tabPane.getTabs().add(InfoTab);
tabPane.getTabs().add(ApptTab);
InfoTab.setClosable(false);
ApptTab.setClosable(false);
InfoTab.setContent(personInfoBox);
ApptTab.setContent(appointmentBox);
VBox one = new VBox(tabPane, personInfoBox);
Scene scene = new Scene(one, 500, 500);
primaryStage.setScene(scene);
primaryStage.show();`
You can't assign a Node more than once to the SceneGraph:
A node may occur at most once anywhere in the scene graph. Specifically, a node must appear no more than once in all of the following: as the root node of a Scene, the children ObservableList of a Parent, or as the clip of a Node.
http://docs.oracle.com/javase/8/javafx/api/javafx/scene/Node.html
You assigned e.g. lname to personInfoBox AND grid. So finally lname will be assigned to grid only. Then you set grid as content for a Tab and after that you set personInfoBox as content, which is now empty

Java FX - Removing a node if the user unchecks a checkbox

I am making a GUI using JavaFx, I want a name field to appear if the check box is checked and disappear if it is not checked.
Here is the code I wrote
test1.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Boolean b = test1.isSelected();
log(b);
Label label1 = new Label("Name:");
TextField textField = new TextField();
HBox hb = new HBox();
if (test1.isSelected()) {
hb.getChildren().addAll(label1, textField);
hb.setSpacing(10);
grid.add(hb, 3, 3);
} else {
**hb.getChildren().removeAll(label1 , textField);** <---look at this!
}
}
});
I am removing the node if it is unchecked but it doesn't execute it accordingly. What am I doing wrong?
Incase it is required, here is how I execute my scene.
Scene scene = new Scene(grid, 500, 500);
primaryStage.setScene(scene);
primaryStage.show();

JavaFX - BorderPane/StackPane not resizing after children changed

I am having a resize issue when content is added and removed from a JavaFX BorderPane. The BorderPane content is not being resized until the window is manually resized. I have written a small test app to model this behavior. The application builds a BorderPane that contains a rectangle embedded within a StackPane at the center of the BorderPane. Along the bottom of the BorderPane there is a VBox and HBox that contain text and a separator. There is a menu item that removes the content from the bottom of the BorderPane (MoveText -> Right) and adds similar content to the right position of the BorderPane.
When the text is added to the right position of the BorderPane, the rectangle overlaps the text. In otherwords the content of the BorderPane center is overlapping the content in the BorderPane right.
I saw the following link -
https://stackoverflow.com/questions/5302197/javafx-bug-is-there-a-way-to-force-repaint-this-is-not-a-single-threading-pr
Calling requestLayout does not seem to help. I have also tried calling impl_updatePG and impl_transformsChanged on various nodes in the graph. I got this idea from this thread - https://forums.oracle.com/forums/thread.jspa?threadID=2242083
public class BorderPaneExample extends Application
{
private BorderPane root;
private StackPane centerPane;
#Override
public void start(Stage primaryStage) throws Exception
{
root = new BorderPane();
root.setTop(getMenu());
root.setBottom(getBottomVBox());
centerPane = getCenterPane();
root.setCenter(centerPane);
Scene scene = new Scene(root, 900, 500);
primaryStage.setTitle("BorderPane Example");
primaryStage.setScene(scene);
primaryStage.show();
}
private MenuBar getMenu()
{
MenuBar menuBar = new MenuBar();
MenuItem rightMenuItem = new MenuItem("Right");
rightMenuItem.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
root.setRight(getRightHBox());
root.setBottom(null);
root.requestLayout();
}
});
MenuItem bottomMenuItem = new MenuItem("Bottom");
bottomMenuItem.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
root.setRight(null);
root.setBottom(getBottomVBox());
}
});
Menu menu = new Menu("Move text");
menu.getItems().add(rightMenuItem);
menu.getItems().add(bottomMenuItem);
menuBar.getMenus().addAll(menu);
return menuBar;
}
private HBox getRightHBox()
{
HBox hbox = new HBox();
VBox vbox = new VBox(50);
vbox.setPadding(new Insets(0, 20, 0, 20));
vbox.setAlignment(Pos.CENTER);
vbox.getChildren().addAll(new Text("Additional Info 1"),
new Text("Additional Info 2"), new Text("Additional Info 3"));
hbox.getChildren().addAll(new Separator(Orientation.VERTICAL), vbox);
return hbox;
}
private VBox getBottomVBox()
{
VBox vbox = new VBox();
HBox hbox = new HBox(20);
hbox.setPadding(new Insets(5));
hbox.setAlignment(Pos.CENTER);
hbox.getChildren().addAll(new Text("Footer Item 1")
, new Text("Footer Item 2"), new Text("Footer Item 3"));
vbox.getChildren().addAll(new Separator(), hbox);
return vbox;
}
private StackPane getCenterPane()
{
StackPane stackPane = new StackPane();
stackPane.setAlignment(Pos.CENTER);
final Rectangle rec = new Rectangle(200, 200);
rec.setFill(Color.DODGERBLUE);
rec.widthProperty().bind(stackPane.widthProperty().subtract(50));
rec.heightProperty().bind(stackPane.heightProperty().subtract(50));
stackPane.getChildren().addAll(rec);
return stackPane;
}
public static void main(String[] args)
{
Application.launch(args);
}
}
Any suggestions would be appreciated.
Thanks
For the sake of having an answer:
The StackPane needs to have its minimum size set in order to do clipping. So if you explicitly add stackPane.setMinSize(0, 0); to the getCenterPane() method, it should fix your problem.
So your getCenterPane() method would now look like this:
private StackPane getCenterPane()
{
StackPane stackPane = new StackPane();
stackPane.setMinSize(0, 0);
stackPane.setAlignment(Pos.CENTER);
final Rectangle rec = new Rectangle(200, 200);
rec.setFill(Color.DODGERBLUE);
rec.widthProperty().bind(stackPane.widthProperty().subtract(50));
rec.heightProperty().bind(stackPane.heightProperty().subtract(50));
stackPane.getChildren().addAll(rec);
return stackPane;
}

Categories