JavaFX Scene builder - more objects without Scene builder - javafx

I created a GUI in Scenebuilder. 2 rectangles. A circle should be displayed for a KeyEvent. The circle is not displayed (playShoot()). Can objects be inserted without Scenebuilder? I think yes, but how? Do you have an idea?
Controller Class
public class PlayAnonymController{
#FXML
private Rectangle PLAYER;
#FXML
private Label gamoverLabel;
#FXML
private Label scoreLabel;
//Variablen
public enum Direction {
LEFT, RIGHT, SPACE, NONE
}
double cX;
public static List<Circle> bullets = new ArrayList<>();
#FXML
public void movePlayer(KeyEvent event) throws IOException{
KeyCode key = event.getCode(); // keyboard code for the pressed key
// System.out.println("Key Pressed: " + key); // for testing
switch (event.getCode()) {
case LEFT:
moveLeft();
break;
case RIGHT:
moveRight();
break;
case SPACE:
playerShoot();
break;
}
}
public void playerShoot() {
System.out.println("Test");
Circle circle = new Circle(50, 50, 50);
circle.setFill(Color.RED);
}
void moveLeft() {
PLAYER.setTranslateX(cX-=10);
}
void moveRight() {
PLAYER.setTranslateX(cX+=10);
}
}
My FXML file from Scenebuilder. At the bottom of the circle should be visible after a KeyEvent.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.shape.Rectangle?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.PlayAnonymController">
<top>
<BorderPane prefHeight="37.0" prefWidth="600.0" BorderPane.alignment="CENTER">
<left>
<Label fx:id="scoreLabel" text="Label" BorderPane.alignment="CENTER" />
</left>
<center>
<Label fx:id="gamoverLabel" text="Label" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
</top>
<bottom>
<Label text="Move: Pfeiltaste links / recht. Schießen Space." BorderPane.alignment="CENTER" />
</bottom>
<center>
<Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<Rectangle fx:id="PLAYER" arcHeight="5.0" arcWidth="5.0" fill="#000911" height="5.0" layoutX="280.0" layoutY="305.0" onKeyPressed="#movePlayer" stroke="BLACK" strokeType="INSIDE" width="40.0" />
</children>
</Pane>
</center>
</BorderPane>

Related

Passing a Button in FXML using JavaFX

Hello I Have this portion of my code, I actually need disable the button when the user is logging, I am trying to pass the button to the FXML, but nothing happens: this is the code in the Main Controller, when the user is logged and data match with the password and username the button with the variable btn1 must disabled, I post my entire code for any help.
public Button getBtn1() {
return btn1;
}
public void conexion () {
String usus="";
String Passu ="";
String Bd="jdbc:sqlserver: // THUMANO2:1433;databaseName=QUORA";
String Usuario="sa";
String Pass="milkas87";
String SqlQuery= "select NOMBREUSUARIO, CONVERT (VARCHAR(50), (DecryptByPassPhrase('*xc/6789oÑ---+y',PASS))) as PASS from usuarios where CONVERT (VARCHAR(50), (DecryptByPassPhrase('*xc/6789oÑ---+y',PASS)))='"+fcontrasena.getText().toString().trim()+"'";
Connection Conexion = null;
try {
Conexion=DriverManager.getConnection(Bd, Usuario, Pass);
PreparedStatement ps =Conexion.prepareStatement(SqlQuery);
ResultSet rs = ps.executeQuery();
while(rs.next()) {
usus = rs.getString("NOMBREUSUARIO");
Passu = rs.getString("PASS");
}
if(fcontrasena.getText().toString().trim().equals(Passu) && fusuario.getText().toString().equals(usus)) {
Stage administrador=new Stage();
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Admin.fxml"));
Stage login=(Stage)fusuario.getScene().getWindow();
Parent root = loader.load();
AdminScreenController controlador = loader.<AdminScreenController>getController();
controlador.setBtn1(btn1);
Scene scene=new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
administrador.setScene(scene);
administrador.setTitle("AdminScreen");
login.hide();
administrador.show();
} catch(Exception e) {}
}
} catch(SQLException e) {
JOptionPane.showMessageDialog(null,"error","ERROR",0);
}
}
this is the code for AdminScreenController:
public void setBtn1(Button btn1) {
btn1.setDisable(true);
}
this is the FXML Document:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="576.0" prefWidth="791.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.AdminScreenController">
<children>
<BorderPane fx:id="bpane" prefHeight="621.0" prefWidth="791.0">
<left>
<VBox fx:id="vboxr" prefHeight="576.0" prefWidth="205.0" BorderPane.alignment="CENTER">
<children>
<BorderPane prefHeight="106.0" prefWidth="205.0">
<center>
<Button fx:id="btn1" mnemonicParsing="false" prefHeight="51.0" prefWidth="127.0" text="Planilla Sistemas" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
<BorderPane prefHeight="106.0" prefWidth="205.0">
<center>
<Button fx:id="btn2" mnemonicParsing="false" prefHeight="51.0" prefWidth="127.0" text="AMP" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
<BorderPane prefHeight="116.0" prefWidth="205.0">
<center>
<Button fx:id="btn3" mnemonicParsing="false" prefHeight="51.0" prefWidth="127.0" text="Tareas Funcionarios" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
<BorderPane prefHeight="106.0" prefWidth="205.0">
<center>
<Button fx:id="btn4" mnemonicParsing="false" prefHeight="51.0" prefWidth="127.0" text="Indicadores" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
<BorderPane prefHeight="106.0" prefWidth="205.0" />
</children>
</VBox>
</left>
<top>
<BorderPane prefHeight="145.0" prefWidth="791.0" BorderPane.alignment="CENTER">
<left>
<Pane fx:id="imgview" prefHeight="145.0" prefWidth="205.0" BorderPane.alignment="CENTER" />
</left>
</BorderPane>
</top>
</BorderPane>
</children>
</StackPane>
its something wrong? i need some orientation here. thanks.
Here is an example that shows the MVC relationship in JavaFX. The button is defined in FXML and injected into the controller where it can be accessed.
TestAppView.fxml
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox alignment="CENTER" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.app.TestAppController">
<Button fx:id="myButton" text="Hello World" />
</VBox>
TestAppController.java
public class TestAppController implements Initializable {
#FXML
private Button myButton;
#Override
public void initialize(URL url, ResourceBundle resources) {
// Prints Hello World & disables the button when clicked
myButton.setOnAction((e) -> {
System.out.println("Hello World");
myButton.setDisable(true);
});
}
}
TestAppMain.java
public class TestAppMain extends Application {
#Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader(TestAppController.class.getResource("TestAppView.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root, 200, 200);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Changing the value of comboBox via another controller isn't triggering the onAction callback in JavaFX

Two controllers are there and their respective FXML files.
If I click on the button in View1.fxml, the value of the comboBox is being changed however I am not getting the output in the console.
But if I click on the changeValueOfDemoComboBoxBtn in View2.fxml, the output is printed in the console.
View1.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller1">
<children>
<Button fx:id="button" layoutX="187.0" layoutY="120.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Button" />
</children>
</AnchorPane>
View2.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller2">
<children>
<Button fx:id="changeValueOfDemoComboBoxBtn" layoutX="200.0" layoutY="264.0" mnemonicParsing="false" onAction="#handleChangeValueOfDemoComboBoxBtnAction" text="Button" />
<ComboBox fx:id="demoComboBox" layoutX="161.0" layoutY="127.0" onAction="#handleDemoComboBox" prefWidth="150.0" />
</children>
</AnchorPane>
Controller1.java
#FXML
private Button button;
#FXML
private void handleButtonAction() throws IOException {
StackPane contentStackPane = (StackPane)mainScene.lookup("#contentStackPane");
FXMLLoader loader = new FXMLLoader(getClass().getResource("view2.fxml));
Parent view2Fxml = loader.load();
Controller2 controller2 = loader.getController();
controller2.setDemoComboBox("Apple");
contentStackPane.getChildren().removeAll();
contentStackPane.getChildren().setAll(view2Fxml );
}
Controller2.java
#FXML
private ComboBox<String> demoComboBox;
#FXML
private Button changeValueOfDemoComboBoxBtn;
#FXML
private void handleDemoComboBox() {
System.out.println("Value changed in Demo ComboBox");
}
#FXML
private void handleChangeValueOfDemoComboBoxBtnAction(){
setDemoComboBox("Oranges");
}
public void setDemoComboBox(String value){
demoComboBox.setValue(value);
}

I want to make a pie chart visible and a line chart invisible when i click a button

I am trying to make charts, but I am stuck now. I have a document where I get challenges to do. I am new to JavaFX and I am trying to get 2 charts to work when I click a button. I have a line chart and a pie chart. The charts arent working because I dont have data in it. What I want is to click "pie chart" and the pie chart is showing and the message says "Drawing a pie chart". The pie chart wont show because there is no data, but that is not the point right now. I made the charts invisible en when I click one of the charts one will be visible and the other one invisible. This is everything I tried but it didnt work. If you didnt understand just comment and I will try to help.
Controller
package ala.javafxchart;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Label;
public class MainSceneController implements Initializable {
#FXML
private Label label;
#FXML
private PieChart pieChart;
#FXML
private LineChart lineChart;
#FXML
private void handlePieChartAction(ActionEvent event) {
System.out.println("You clicked the Pie Chart!");
label.setText("Drawing a pie chart!!");
pieChart.setVisible(true);
}
#FXML
private void handleLineChartAction(ActionEvent event) {
System.out.println("You clicked the Pie Chart!");
label.setText("Drawing a line chart!!");
lineChart.setVisible(false);
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
#FXML
private void handleLineChartAction(ActionEvent event) {
System.out.println("You clicked the Pie Chart!");
label.setText("Drawing a line chart!!");
setVisible(lineChart);
}
private void setVisible(PieChart chart){
}
private void setVisible(LineChart chart){
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.LineChart?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.chart.PieChart?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane id="AnchorPane" prefHeight="768.0" prefWidth="1366.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ala.javafxchart.MainSceneController">
<children>
<BorderPane prefHeight="768.0" prefWidth="1498.0">
<top>
<Label text="Chart Demo" BorderPane.alignment="CENTER">
<font>
<Font size="35.0" />
</font>
</Label>
</top>
<left>
<VBox prefHeight="692.0" prefWidth="241.0" BorderPane.alignment="CENTER">
<children>
<Button fx:id="lineChart" mnemonicParsing="false" prefHeight="45.0" prefWidth="245.0" text="Line Chart" />
<Button fx:id="pieChart" mnemonicParsing="false" prefHeight="45.0" prefWidth="245.0" text="Pie Chart" />
</children>
</VBox>
</left>
<bottom>
<Label fx:id="statusLabel" text="status message" BorderPane.alignment="CENTER">
<padding>
<Insets right="1400.0" />
</padding>
</Label>
</bottom>
<center>
<StackPane prefHeight="150.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<LineChart title="Lost Luggage 2016" visible="false">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis side="LEFT" />
</yAxis>
</LineChart>
<PieChart title="Lost Luggage 2016" visible="false" />
</children>
</StackPane>
</center>
</BorderPane>
</children>
</AnchorPane>
Try this I made some changes to your fxml to make it more readable
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import java.net.URL;
import java.util.ResourceBundle;
public class MainSceneController implements Initializable {
public Button lineChartButton;
public Button pieChartButton;
public LineChart lineChart;
public PieChart pieChart;
public Label label;
#FXML
private void handlePieChartAction() {
System.out.println("You clicked the Pie Chart!");
label.setText("Drawing a pie chart!!");
if(pieChart.isVisible())
pieChart.setVisible(false);
else
pieChart.setVisible(true);
}
#FXML
private void handleLineChartAction() {
System.out.println("You clicked the Line Chart!");
label.setText("Drawing a line chart!!");
if(lineChart.isVisible())
lineChart.setVisible(false);
else
lineChart.setVisible(true);
}
#Override
public void initialize(URL location, ResourceBundle resources) {
// TODO
lineChartButton.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent e) {
handleLineChartAction();
}
});
pieChartButton.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent e){
handlePieChartAction();
}
});
}
}
This is the FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.LineChart?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.chart.PieChart?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<AnchorPane id="AnchorPane" prefHeight="768.0" prefWidth="1366.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MainSceneController">
<children>
<BorderPane prefHeight="768.0" prefWidth="1498.0">
<top>
<Label text="Chart Demo" BorderPane.alignment="CENTER">
<font>
<Font size="35.0" />
</font>
</Label>
</top>
<left>
<VBox prefHeight="692.0" prefWidth="241.0" BorderPane.alignment="CENTER">
<children>
<Button fx:id="lineChartButton" mnemonicParsing="false" prefHeight="45.0" prefWidth="245.0" text="Line Chart" />
<Button fx:id="pieChartButton" mnemonicParsing="false" prefHeight="45.0" prefWidth="245.0" text="Pie Chart" />
</children>
</VBox>
</left>
<bottom>
<Label fx:id="label" text="status message" BorderPane.alignment="CENTER">
<padding>
<Insets right="1400.0" />
</padding>
</Label>
</bottom>
<center>
<StackPane prefHeight="150.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<LineChart fx:id="lineChart" title="Lost Luggage 2016" visible="false">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis side="LEFT" />
</yAxis>
</LineChart>
<PieChart fx:id="pieChart" title="Lost Luggage 2016" visible="false" />
</children>
</StackPane>
</center>
</BorderPane>
</children>
</AnchorPane>
I renamed your buttons to lineChartButton and pieChartButton instead of lineChart and pieChart because thats super confusing and gave ids to the pieChart and lineChart

JavaFx: multiple controllers

I'm working on the project which contains multiple controllers (1 main and two for each of the Fxml files). Unfortunately while running the program, console throw the NullPointerExeption at me. I found out that it's the main controller fault, but still having this information I'm unable to fix this. Could you give me some tips how to solve this issue ?
Here i got the full track:
MainController class:
public class MainController {
#FXML LogInController logInController;
DBConnect dbConnect;
#FXML FlightControlController flightControlController;
#FXML public void initialize() {
logInController.initialize(this);
flightControlController.initialize(this);
dbConnect.initialize(this);
}
}
DBConnect class:
public void dbConnect() throws SQLException {
try {
Conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/nasa", "root", "1234");
} catch(Exception e) {
System.out.println(e.getStackTrace());
}
}
public boolean isCorrect(String login,String password) throws SQLException, IOException {
Stmt = Conn.createStatement();
Rs = Stmt.executeQuery("SELECT * FROM Users WHERE login='"+login+"'AND password='"+password+"';");
if(Rs.next()) {
return true;
} else {
return false;
}
}
public void initialize(MainController mainController) {
this.mainController=mainController;
}
LogInController class:
MainController mainController;
#FXML DBConnect dbConnect;
#FXML TextField logginField = null;
#FXML PasswordField passwordFiled=null;
#FXML Button logInButton;
#FXML
public void buttonClicked(ActionEvent event) throws SQLException, IOException {
mainController.dbConnect.dbConnect();
if(mainController.dbConnect.isCorrect(logginField.getText(),passwordFiled.getText())) {
Parent root = FXMLLoader.load(getClass().getResource("/view/FlightControlView.fxml"));
Scene scene = new Scene(root);
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
stage.setScene(scene);
stage.show();
} else {
System.out.print("An error have occured");
}
}
public void initialize(MainController mainController) {
this.mainController=mainController;
}
FlightControlController Class:
#FXML Label label;
MainController mainController;
public void initialize(MainController mainController) {
this.mainController = mainController;
}
And the error which occurs:
Caused by: java.lang.NullPointerException at
controller.LogInController.buttonClicked(LogInController.java:31) ...
62 more
I'm not sure but is it possible that MainController (which doesn't have his own Fxml file) so that FXMLLoader doesn't initialize MainController's properties?
#EDIT
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="693.0" prefWidth="1062.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.LogInController">
<children>
<SplitPane dividerPositions="0.21320754716981133" layoutX="331.0" layoutY="220.0" prefHeight="693.0" prefWidth="1062.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<Label alignment="CENTER" layoutY="-1.0" maxHeight="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="70.0" prefWidth="222.0" text="NASA DATABASE" />
<TextField fx:id="logginField" layoutX="18.0" layoutY="101.0" promptText="Login" />
<PasswordField fx:id="passwordFiled" layoutX="18.0" layoutY="165.0" promptText="Password" />
<Button fx:id="logInButton" onAction="#buttonClicked" layoutX="79.0" layoutY="230.0" mnemonicParsing="false" text="Log In" />
</children>
</AnchorPane>
<AnchorPane prefHeight="691.0" prefWidth="493.0" SplitPane.resizableWithParent="false">
<children>
<Label alignment="CENTER" layoutX="97.0" layoutY="104.0" prefHeight="417.0" prefWidth="635.0" text="NOT AVAILABLE, PLEASE LOG IN" AnchorPane.leftAnchor="113.0" AnchorPane.rightAnchor="113.0">
<font>
<Font name="System Bold Italic" size="37.0" />
</font>
</Label>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>

javafx invoking button to draw rectangle on scene

I am creating a simple application in javafx. The main screen has two tabs and each tab has a button. When (button)btn2 in tab2 is clicked, a rectangle should be displayed on the scene.
Here is my code.
Main class:
public class check extends Application
{
public Stage primaryStage;
private BorderPane rootLayout;
#Override
public void start(Stage primaryStage)
{
this.primaryStage = primaryStage;
this.primaryStage.setTitle("TabsExample");
RootLayout();
}
public void RootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(check.class.getResource("/view/tabs.fxml"));
rootLayout = (BorderPane) loader.load();
primaryStage.setScene(new Scene(rootLayout, 500, 500));
primaryStage.show();
control controller = loader.getController();
} catch (IOException e) {
e.printStackTrace();
}
}
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
Control.java
public class control {
#FXML
private Tab tab1;
#FXML
private Tab tab2;
#FXML
private Button btn1;
#FXML
private Button btn2;
#FXML
private AnchorPane A2;
#FXML
private AnchorPane A1;
public control(){}
private check mainclass;
#FXML public void handleSubmitButtonAction(ActionEvent event)
{
System.out.println("button clicked");
Rectangle r = new Rectangle();
r.setX(50);
r.setY(50);
r.setWidth(200);
r.setHeight(100);
r.setArcWidth(20);
r.setArcHeight(20);
A2.getChildren().add(r);
}
public void setcheck(check ch) {
this.mainclass = ch;
}
}
****FXML:****
<BorderPane maxHeight="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.control">
<top>
<TabPane prefHeight="1754.0" prefWidth="1374.0" tabClosingPolicy="UNAVAILABLE" BorderPane.alignment="CENTER">
<tabs>
<Tab id="tab1" text="Draw Circle">
<content>
<AnchorPane prefHeight="200.0" prefWidth="200.0">
<children>
<Button id="btn1" layoutX="23.0" layoutY="44.0" mnemonicParsing="false" text="rectangle" />
</children>
</AnchorPane>
</content></Tab>
<Tab id="tab2" text="Draw Rectangle">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Button id="btn2" layoutX="20.0" layoutY="58.0" mnemonicParsing="false" onAction="#handleSubmitButtonAction" prefHeight="15.0" prefWidth="77.0" text="circle" />
</children></AnchorPane>
</content>
</Tab>
</tabs>
</TabPane>
</top>
</BorderPane>
I apologize for a very long question. With this code, the button action listener is invoked and the message "Button clicked" is printed on the console. But, I do not understand why the rectangle is not drawn on the screen.
It looks like you're missing the ID for the AnchorPane in your FXML. Also, the appropriate tag to use here is "fx:id". You can read a bit more about that here: What's the difference between fx:id and id: in JavaFX?
Below is the corrected FXML
<BorderPane maxHeight="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.control">
<top>
<TabPane prefHeight="1754.0" prefWidth="1374.0" tabClosingPolicy="UNAVAILABLE" BorderPane.alignment="CENTER">
<tabs>
<Tab fx:id="tab1" text="Draw Circle">
<content>
<AnchorPane fx:id="A1" prefHeight="200.0" prefWidth="200.0">
<children>
<Button fx:id="btn1" layoutX="23.0" layoutY="44.0" mnemonicParsing="false" text="rectangle" />
</children>
</AnchorPane>
</content></Tab>
<Tab fx:id="tab2" text="Draw Rectangle">
<content>
<AnchorPane fx:id="A2" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Button fx:id="btn2" layoutX="20.0" layoutY="58.0" mnemonicParsing="false" onAction="#handleSubmitButtonAction" prefHeight="15.0" prefWidth="77.0" text="circle" />
</children></AnchorPane>
</content>
</Tab>
</tabs>
</TabPane>
</top>
</BorderPane>

Resources