Why should I declare TextField as public but not shortcut

I am new to programming; take it easy. :)

I have a simple program (for training) that will not compile unless I create two TextField objects. I get an error (ava.lang.IllegalAccessException: class javafx.fxml.FXMLLoader $ ValueElement cannot access a member of class firstjavafxprogram.SampleController with modifiers "private"). And I just can't understand why these two should be publicly, but Label can be closed. Hope I posted 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; } } 
+6
source share
1 answer

Edit

 @FXML private Label label; public TextField txtVolts; public TextField txtAmps; 

TO

 @FXML private Label label; @FXML private TextField txtVolts; @FXML private TextField txtAmps; 

regarding this .

+7
source

All Articles