I’m new to programming; go easy. 🙂
I have a simple program (for learning purposes) that won’t compile unless I make the two TextField objects public. I’m getting an error (ava.lang.IllegalAccessException: Class javafx.fxml.FXMLLoader$ValueElement can not access a member of class firstjavafxprogram.SampleController with modifiers “private”). And I just can’t for the life of figure out why those two have to public but Label can be private. Hope I post my code correctly:
package firstjavafxprogram;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class SampleController implements Initializable {
@FXML
private Label label;
public TextField txtVolts;
public TextField txtAmps;
@FXML
private void handleButtonAction(ActionEvent event) {
String labelMessage = getTheMessage();
label.setText(labelMessage);
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
@FXML
private String getTheMessage(){
String enteredVolts = txtVolts.getText();
int volts = Integer.parseInt(enteredVolts);
String enteredAmps = txtAmps.getText();
int amps = Integer.parseInt(enteredAmps);
int watts = volts * amps;
String msgString = "Watts: " + Integer.toString(watts);
return msgString;
}
}
Change
To
as per this example.