Reputation: 11
I have made a basic login screen in netbeans using javafx and scenebuilder 2.0! by copying a youtube tutorial.
<Button id="btnLogin" layoutX="146.0" layoutY="243.0" mnemonicParsing="false" onAction="#initialize" prefHeight="59.0" prefWidth="76.0" text="Login" />
as shown above, netbeans underline "#initialize" and give the folowing error "Handler method is not accessible. Make public, or annotate with @FXML"
and the program doesn't want to start up or even show me my stage.
LoginController:
@FXML private void initialize (ActionEvent event)
{
System.out.println("test");
}
if i simply delete the code "onAction="#initialize" my program runs and shows me an interface, but my button to login doesn't work(obviously)
P.S. making it public doesnt work
any advice will appreciated thanks a lot in advance
code:
Main:
public class Login extends Application
{
@Override
public void start(Stage stage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("/fxml/Login.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Login");
stage.show();
}
Controller:
public class LoginController implements Initializable {
@FXML
private Label lblmessage;
@FXML
private TextField txtUsername;
@FXML
private Button btnLogin;
@FXML
private PasswordField txtPassword;
@FXML private void handlebutton()
{
System.out.println("test");
}
/**
* Initializes the controller class.
* @param url
* @param rb
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Upvotes: 1
Views: 1176
Reputation: 11
thanks all! my issue was that in my fxml file i was naming my textfields/password fields fields
<TextField id="txtUsername"....... etc
<Label id="lblmessage" ...... etc
<PasswordField id="txtPassword".... etc
therefore i received a null pointed everytime a null pointer everytime i tried to access those variable from my controller/main class, because my program wasnt recieving any data because i forgot my fX:id, so the above should have looked like the following
<TextField fx:id="txtUsername"....... etc
<Label fx:id="lblmessage" ...... etc
<PasswordField fx:id="txtPassword".... etc
then my program worked perfectly thanks everyone for their help
Upvotes: 0
Reputation: 3794
I guess the method name is the problem.
The method @FMXL protected void initialize()
may be used as an additional callback on startup.
You can use the initialize
method e.g. to dynamically fill your UI.
So:
Try to rename the method in code and fxml file.
As given here:
An instance of the FXMLLoader class simply looks for the initialize() method on the controller and calls it, if available. Note that, similar to other FXML callback methods such as event handlers, this method must be annotated with the @FXML annotation if it is not public.
I have just rebuilt your code. I also added this main method to the Login:
public static void main(String[] args){
launch(args);
}
This is the Login.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="LoginController">
<children>
<Label fx:id="lblmessage" text="Label" />
<TextField fx:id="txtUsername" />
<Button fx:id="btnLogin" mnemonicParsing="false" onAction="#handlebutton" text="Button" />
<PasswordField fx:id="txtPassword" />
</children>
</VBox>
And with this setup, everything works as expected.
The Stage is loaded, the handlebutton method is called when I click the button and the initialize method is called on startup.
The error currently seems to be not reproducable.
Upvotes: 1