Clear TextField From Another Class - java

In my main FXML controller class Alpha I have a mask with a text field and a button the clear it:
#FXML
private TextField testTF = new TextField();
#FXML
public void clearText() {
if (testTF != null)
testTF.clear();
}
If I enter text in to the text field and hit the clear button, the text is being removed. So far, all good.
I have a second controller class Beta. The corresponding fxml file contains the menu layout (menu bar). If the menu item "new" is clicked, it should also clear my text field in class Alpha.
public class Beta {
private void newApp() {
Alpha a = new Alpha();
a.clear();
}
}
Nothing happens though. What am I doing wrong here? How can I click a button/menu item in a FXML controller class and have it to clear a text field in another FXML controller class?

You should not instantiate testTF with new TextField();: FXMLLoader will automatically assign testTF the corresponding TextField object because you have a #FXML tag.
You're re-instantiating Alpha in class Beta, which exists separately from the one which was created when your fxml was loaded, resulting in two separate private TextField testTF objects. Either give Beta a reference to the first instance of Alpha, or try this alternative:
public class Alpha implements Initializable {
public static TextField tf;
#FXML
private TextField testTF;
#Override
public void initialize(URL location, ResourceBundle resources) {
tf = testTF;
}
}
public class Beta {
private void newApp() {
if (Alpha.tf != null)
Alpha.tf.clear();
}
}

Related

Struggling to get JavaFx app working, objects stop existing?

So i'm working on a pet project, essentially a digitalisation of a do it yourself role playing adventure book.
I switched to using Scenebuilder because of the freedom it allows when crafting a GUI.
I'm having trouble binding the data to the screen. It seems the objects I am calling either stop existing or are not the ones i need.
I am using a SQLite database for my data, and that seems to work fine.
I am using Maven to import the things i need, this also seems to work fine however this requires me to use
public class DdAppLauncher {
public static void main(String[] args) {
DdApp2.main(args);
}
}
into ->
public class DdApp2 extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/fxml/mainWindow.fxml"));
stage.setTitle("Deathtrap Dungeon");
stage.setScene(new Scene (root, 800,600));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
This brings up the titlescreen that contains a start button which is handled by the following controller.
public class MainController {
LocationsPool loc;
Location currentLocation;
#FXML
private ListView<String> inventory;
#FXML
private Text locationDescription;
#FXML
private Text descrA;
#FXML
private Text descrB;
#FXML
private Text descrC;
#FXML
private Text descrD;
#FXML
private Text descrE;
#FXML
private Text descrF;
#FXML
public void startButtonClicked(ActionEvent event){
try {
System.out.println("Starting game");
Stage stage = (Stage) ((Node)event.getSource()).getScene().getWindow();
// Swap screen
Parent root = FXMLLoader.load(getClass().getResource("/fxml/gameWindow.fxml"));
stage.setScene(new Scene(root,800,600));
} catch (IOException e) {
e.printStackTrace();
}
//Setup
loc = new LocationsPool();
currentLocation = loc.getLocation(1);
System.out.println(currentLocation.getDescription());
locationDescription = new Text(currentLocation.getDescription());
System.out.println(locationDescription.getText());
System.out.println(locationDescription);
}
#FXML
public void handleButtonA(){
System.out.println(currentLocation==null);
}
}
Output on console ->
starting game
The clamour... (works)
The clamour... (works)
On button press ->
True
So it seems the app "forgets" fields when it runs? or is it the controller that stop existing and is remade?
Furthermore when trying to bind data to fields with fx:id, it doesn't seem to link those fields to anything until i press that button.
Am i structuring this all wrong? What am i not getting?
The final product should have the description loaded and all the choices loaded for that location.
Then on a selection should load up a new location and new choices so the text needs to be updated.
Thanks in advance.
Kev

JavaFX application with 2 or more fxml files - properties

I have an app with 3 fxml files.
The 1st is main window of app and most of work im doing here, but the 2 others fxml files allows me to change some settings and add more records to app.
I made it that 2 small fxml files extends the main file.
Basically:
public class FXMLDocumentController implements Initializable {
#FXML
private TextField txt;
#FXML
private Stage stage;
Symulacja symulacja = new Symulacja();
Promocja promocja = new Promocja();
#Override
public void initialize(URL url, ResourceBundle rb) {
txt.textProperty().bindBidirectional(promocja.getProgKwotyZetonow(), new NumberStringConverter());
labelProgKwotyZetonow.textProperty().bind(promocja.getProgKwotyZetonow().asString());
}
#FXML
public void zmienUstawienia() {
symulacja.zmienOkno("FXMLUstawienia.fxml", "Ustawienia");
}
#FXML
public void dodajKlientow() {
symulacja.zmienOkno("FXMLDodajKlienta.fxml", "Dodawanie");
}
}
It's main fxml Controller
public class FXMLUstawieniaController extends FXMLDocumentController{
#FXML
private TextField textProgKwota;
#FXML
private Button btnZatwierdzUstawienia;
#FXML
private TextField textProgIlosc;
#FXML
public void zatwierdzUstawienia() {
promocja.setProgIlosciZetonow(Integer.parseInt(textProgIlosc.getText()));
}
#Override
public void initialize(URL url, ResourceBundle rb) {
textProgKwota.textProperty().bindBidirectional(promocja.getProgKwotyZetonow(), new NumberStringConverter());
}
}
As you see I'm using properties to dynamically change what is showing in labelProgKwotyZetonow and textProgKwota.
When im not using the 2nd window txt and labelProgKwotyZetonow works fine, but when I'm initializing 2nd window I always get 0 in TextField and even if I change it this is not changing labelProgKwotyZetonow.
It's strange to me because when I'm souting (System.out.println) the value of promocja.getProgKwotyZetonow() in some places I get:
In function initialize in the 2nd controller - 0.
In function zmienUstawienia in 1st controller - the value from txt/labelProgKwotyZetonow.
In function zatwierdzUstawienia in 2nd controller - the value from textProgKwota.

communication betwenn Tabs in JavaFx ans Scene builder

i made a mainEntredController which contains a TabPane with two tabs (HomeController) and (PatientController), The MainEntred Controle the Parameter Passage Between this two tabs but i have liitls issu that the tabs doesnet reconize the Parent (MainEntredController) or each other. it retuns an error inn this line
*** labelpatient.setText(mainec.LoadtxtFromlabelhometabhome()); ******
the idea is to get a text from the first tab to show it in a label in the second tab
hear's my code
the first is the MainEntred Class the Container
public class MainEntredController {
#FXML MenuItem closemi;
#FXML HomeController homecontroller;
#FXML PatientController patientcontroller;
public void initialize(URL location, ResourceBundle resources) {
System.out.println("Application Started");
homecontroller.intit(this);
patientcontroller.init(this);
}
public void ClickMenuItemClose(){
System.exit(0);
}
public String LoadtxtFromlabelhometabhome() {
// System.out.println(homecontroller.labelhome.getText());
return homecontroller.labelhome.getText();
}
public void setTabpatientlabel(String text) {
//patientcontroller.labelpatient.setText(text);
}
}
the Second is the First Tab HomeController
public class HomeController {
public MainEntredController mainec;
#FXML public Label labelhome;
#FXML private TextField tfhome;
#FXML private Button savehome;
#FXML private Button sendhome;
//public HomeController(){}
public void btnSaveHomeClicked(ActionEvent event){
System.out.println("le boutton save home est cliqué");
labelhome.setText(tfhome.getText());
}
public void btnSendHomeClicked(ActionEvent event){
System.out.println("le boutton send home est cliqué");
// send data to tab patient
mainec.setTabpatientlabel(labelhome.getText());
}
public void intit(MainEntredController mainEntredController) {
mainec = mainEntredController;
}
}
the Third one is the Second tab PatientController
public class PatientController {
public MainEntredController mainec;
#FXML public Label labelpatient;
#FXML private TextField tfpatient;
#FXML private Button savepatient;
#FXML private Button loadpatient;
public void btnSavePatientClicked(ActionEvent event){
System.out.println("le boutton save Patient est cliqué");
labelpatient.setText(tfpatient.getText());
}
public void btnLoadPatientClicked(ActionEvent event){
System.out.println("le boutton Load patient est cliqué");
labelpatient.setText(mainec.LoadtxtFromlabelhometabhome());
}
public void init(MainEntredController mainEntredController) {
mainec = mainEntredController;
}
}
And the FXML File of the MainEntred
<fx:include id="homecontroller" source="home.fxml" />
<fx:include id="patientcontroller" source="patient.fxml" />
i guess those two are madding the problem any advise how to solve it Please
i dont know why i am having java.lang.NullPointerException at the line declarred erlier he cant get the source (unknown source)
If i understand correctly then you want to communicate between controller. well, this can be done pretty easy.
in you main type something likes this:
FXMLLoader loader = new FXMLLoader(Main.class.getResource("your.fxml"));
Parent node = loader.load(); // this loads view
YourController instance = loader.getController();
some_class.initialize(instance);
Finelly i found what is wrong. the thing is that in the declaration in the MainController of the two Tabs we put the fx:id declared on the fxml file
and adding the word "Controller"
like this
#FXML private HomeController homeController;
#FXML private PatientController patientController;
the fx:id="home"
the second includes fx:id="patient"

JavaFX subclassed Button - How to make Label update work?

I want to have several JavaFX Buttons that update one Label in my Application with text. For testing purposes it's just Button Text.
What I did at first worked fine and looked like this:
String Text = "...";
public void kons() {
System.out.println("Works...");
System.out.println(Text);
Tekst.setText(Text);
Button G4 = new Button("Spadantes");
G4.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Text = G4.getText();
kons();
}
});
Then I decided to stylize my buttons with CSS and because I wanted to have several groups of buttons stylized in different way I subclassed JavaFX Button class in this way:
public class Buttons extends Button {
public Buttons(String text) {
super(text);
getStylesheets().clear();
getStylesheets().add("./Buttons.css");
Which still worked. But now I want my event handler to be moved to Button subclass (to avoid copy-pasting exactly same code into each and every button of mine). What I did looks like this:
public class Buttons extends Button {
public Buttons(String text) {
super(text);
getStylesheets().clear();
getStylesheets().add("./Buttons.css");
setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Main.Text = getText();
Main.kons();
}
});
}
}
Main is my extend Application class
Tekst is my label.
And sadly it throws me exception about calling non-stathic method and variable from static context. From what I understand instances are static and definitions are non-static. I tried to change everything "in the way" to static but it gives me red wall of errors after clicking button (nothing in compilation process). I also tried to call instance of my Application somehow but I have no idea how (from what I understand extend Application class intantiates itself on it's own while starting program so there's no "name" by which I can call it's Label.
What I'm looking for is "quick and dirty solution" to be able to use subclassed buttons (or other sliders, text-fields, etc.) that can call a method that updates something "on screen".
[EDIT] I'm using newest Java there is of course. In case it matters.
Instead of subclassing, why not just write a utility method that creates the buttons for you? I would also not recommend making the text variable an instance variable: just reference the Label directly.
public class SomeClass {
private Label tekst ;
// ...
private Button createButton(String buttonText) {
Button button = new Button(buttonText);
button.getStylesheets().add("Buttons.css") ;
button.setOnAction(e -> tekst.setText(buttonText));
return button ;
}
}
Then, from within the same class, when you need one of those buttons you just do
Button button = createButton("Text");
If you really want to subclass (which just seems unnecessary to me), you need to pass a reference to the label to the subclass:
public class LabelUpdatingButton extends Button {
public LabelUpdatingButton(String text, Label labelToUpdate) {
super(text);
getStylesheets().add("Buttons.css");
setOnAction(e -> labelToUpdate.setText(getText()) );
}
}
Then from your class that assembles the UI you can do
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
Label tekst = new Label();
Button someButton = new LabelUpdatingButton("Button text", tekst);
// etc...
}
}
But again, creating a subclass that does nothing other than define a constructor that calls public API methods is redundant, imo.
Also, it's a bit unusual to create an entire stylesheet just for your buttons. Typically you would set a style class on the Button:
button.getStyleClass().add("my-button-class");
and then in the stylesheet you add to the Scene do
.my-button-class {
/* styles for this type of button */
}

Managing the runtime behavior of a JavaFX MenuBar

I have a BorderPane, onto which I placed a MenuBar. At the center of the BorderPane I display differnt AnchorPanes depending on the MenuItem selected. So far so good.
Now, how do I make sure that the Menus change behavior in response to the item selected in the child AnchorPane? So for example if the user selects "Edit", there will be a different action depending on whether the item currently higlighted is a user account, a file etc.
So far I made something along these lines:
The BorderPane controller:
public class MenuTest implements Initializable{
#FXML
private BorderPane borderPaneMain;
#FXML
private AnchorPane anchorPaneMain;
#FXML
private Menu menuEdit;
#FXML
private MenuItem itemEdit;
static String menuMode;
static String entityName;
public MenuTest(){
menuMode ="";
entityName = "";
}
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
AnchorPane anchor;
try {
anchor = (AnchorPane) new FXMLLoader().load(getClass().getResource("views/MainView.fxml"));
borderPaneMain.setCenter(anchor);
} catch (IOException e) {
e.printStackTrace();
}
}
protected static void setMenuMode(String menuMd, String entityNm){
entityName = entityNm;
menuMode = menuMd;
}
#FXML
private void onEditClick(){
if(entityName.equals(AnchorTest.FILE)){
//Launches correct edit view
new FXMLLoader().load(getClass().getResource("views/EditFile.fxml"));
//Passes the name of the entity so that the controller can retrieve its data
FileEditController.setFile(entityName);
}else if(entityName.equals(AnchorTest.PERSON)){
new FXMLLoader().load(getClass().getResource("views/EditPerson.fxml"));
PersonEditController.setFile(entityName);
}
}
}
The child AnchorPane controller:
public class AnchorTest implements Initializable{
public static final String PERSON = "PERSON";
public static final String FILE = "FILE";
ObservableList<String> peopleList;
ObservableList<String> fileList;
#FXML
private ListView<String> listPeople;
#FXML
private ListView<String> listFiles;
#Override
public void initialize(URL location, ResourceBundle resources) {
peopleList = FXCollections.observableArrayList("Frank","Matin","Anne");
fileList = FXCollections.observableArrayList("hello.txt","holiday.jpg","cv.doc");
listPeople.setItems(peopleList);
listFiles.setItems(fileList);
}
#FXML
private void personSelected(){
MenuTest.setMenuMode(this.PERSON, listPeople.getSelectionModel().getSelectedItem());
}
#FXML
private void fileSelected(){
MenuTest.setMenuMode(this.FILE, listFiles.getSelectionModel().getSelectedItem());
}
}
However I'm not sure that it's the best solution, especially considering the if/elseif statement will need to be altered whenever I add a new element type and its corresponding edit options. So is there any way that I can do this better?
I think if your application has only a few (2-4) different types of "things" that are represented by a AnchorPane, then your approach is totally fine. An alternative to your approach is the state pattern. In that case, your currently selected "item type" would be your state.

Categories

Resources