JavaFX Textfelder geben immer null zurück

programmierer12

Bekanntes Mitglied
Hallo zusammen,
ich habe in JavaFX ein paar Textfelder und einen Button nun will ich wenn man auf den Button klickt von allen Textfeldern einen Wert zurück bekommen! Ich habe das eben mit der
Code:
.getText();
Methode gemacht doch ich bekomme auch wenn ich etwas eingegeben habe null bzw "" zurück.
Woran kann das liegen?
 
Hi,
Ohne code lässt sich nur raten wo das Problem liegen könnte.... 😳
Poste doch mal den Code damit wir weiterhelfen können.
 
Also gut:
Die Controller Klasse
Java:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package installer;

import java.io.File;
import java.net.URL;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
import javax.swing.JOptionPane;

/**
 *
 * @author Lukas
 */
public class StyleController implements Initializable {
    
    @FXML
    public static TextField name;
    @FXML
    public static TextField email;
    @FXML
    public static final TextField serial1 = new TextField();
    @FXML
    public static final TextField serial2 = new TextField();
    @FXML
    public static final TextField serial3 = new TextField();
    
    private static final String DIR = "C:\\Users\\"+ System.getProperty("user.name") +"\\AppData\\Roaming\\";
    
    @FXML
    private void abbrechen(ActionEvent event) {
        
        if (JOptionPane.showConfirmDialog(null, "Wollen Sie das Setup wirklich abbrechen?", "Setup Abbrechen", 0) == 0) {
            
            System.exit(0);
            
        }
        
        else{
            
            
            
        }
        
    }
    
    @FXML
    private void zuMaske1(ActionEvent event){
        
        Installer.maske1();
        
    }
    
    @FXML
    private void zuMaske2(ActionEvent event){
        
        Installer.maske2();
        
    }
    
    private static void test(){
        
        name.setText("NAME");
        
    }
    
    @FXML
    private void insallieren(ActionEvent event) throws InterruptedException, SQLException{
        
        int state = 0;
        
        if (name.getText().isEmpty() || email.getText().isEmpty()) {
            
            System.err.println(name.getText());
            System.err.println(email.getText());
            System.err.println(name.isDisabled());
            String name1 = name.getText();
            String email1 = email.getText();
            test();
            
        }
        
        else{
        
            File installation = new File(DIR + "AB Cleaner");
            Dir.create(installation, Dir.DIRECTORY);
        
            String enterSerial = serial1.getText() + serial2.getText() + serial3.getText();
            
            Database db = new Database(JdbcTemplate.getInstance().getDataSource());
        
            db.register(name.getText(), email.getText());
      
            ArrayList<String> serial = db.serial();
            
            for (String string : serial) {
                
                if (string.equals(enterSerial)) {
                    
                    state = 1;
                    JOptionPane.showMessageDialog(null, "Ihr Produkt wurde erfolgreich aktiviert und wird nun installiert.");
                    db.deleteSerial(string);
                    Installer.maske3();
                    break;
                    
                }
                
                else{
                    
                    state = 0;
                    
                }
                
            }
            
            JOptionPane.showMessageDialog(null, "Ihr Aktivierungscode ist fehlerhaft. Prüfen Sie ihn auf Fehler oder erwerben Sie einen neuen");
            
        }
        
    }
    
    @FXML
    private void main(ActionEvent event){
        
        Installer.zumain();
        
    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        
        name = new TextField();
        email = new TextField();
        
    }    
    
}
und die dazugehörige Fxml Datei mit den TextFeldern und Buttons
[XML]<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="310.0" prefWidth="522.0000999999975" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="installer.StyleController">
<children>
<TextField fx:id="name" layoutX="182.0" layoutY="108.0" prefWidth="326.0" />
<TextField fx:id="email" layoutX="182.0" layoutY="144.0" prefWidth="326.0" />
<TextField fx:id="serial1" layoutX="182.0" layoutY="181.0" prefWidth="103.0" />
<TextField fx:id="serial2" layoutX="294.0" layoutY="181.0" prefWidth="103.0" />
<TextField fx:id="serial3" layoutX="406.0" layoutY="181.0" prefWidth="103.0" />
<Button layoutX="271.0" layoutY="277.0" mnemonicParsing="false" onAction="#zuMaske1" text="Zurück" />
<Button layoutX="339.0" layoutY="277.0" mnemonicParsing="false" onAction="#insallieren" text="Installieren" />
<Button layoutX="430.0" layoutY="277.0" mnemonicParsing="false" onAction="#abbrechen" text="Abbrechen" />
<Label layoutX="14.0" layoutY="14.0" text="Installer AB Cleaner - Konfiguration">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Label>
<Label layoutX="14.0" layoutY="64.0" text="Registrieren Sie nun ihre Software:">
<font>
<Font size="13.0" />
</font>
</Label>
<Label layoutX="14.0" layoutY="111.0" text="Vor- und Nachname" />
<Label layoutX="14.0" layoutY="144.0" text="E-Mail Adresse" />
<Label layoutX="14.0" layoutY="181.0" text="Serial-Key" />
<Label layoutX="13.0" layoutY="236.0" prefWidth="495.0001220703125" text="Klicken Sie auf Installieren um fortzufahren oder auf Abbrechen um das Setup zu beenden" />
</children>
</AnchorPane>
[/XML]

Wenn noch andere Klassen interessant sind poste ich die auch nocht
MFG
programmierer12
 
Hi,
Frage, wieso erstellst du die TextFields final und statisch? Was ist der Grund?
Java:
    @FXML
    public static TextField name;
    @FXML
    public static TextField email;
    @FXML
    public static final TextField serial1 = new TextField();
    @FXML
    public static final TextField serial2 = new TextField();
    @FXML
    public static final TextField serial3 = new TextField();
 
Hi,
Ich hab die Controiller- Klasse und das fxml- File mal angeschaut und daraus eine Version in Eclipse erstellt.
Java:
package application;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
 
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
import javax.swing.JOptionPane;
 
/**
 *
 * @author Lukas
 */
public class StyleController implements Initializable {
    
    @FXML
    TextField name;
    @FXML
    TextField email;
    @FXML
    TextField serial1;
    @FXML
    TextField serial2;
    
    @FXML
    private void abbrechen(ActionEvent event) { 
        if (JOptionPane.showConfirmDialog(null, "Wollen Sie das Setup wirklich abbrechen?", "Setup Abbrechen", 0) == 0) {   
            System.exit(0);   
        } 
        else {    
        } 
    }
    
    @FXML
    private void zuMaske1(ActionEvent event){ 
        Installer.maske1();  
    }
    
    @FXML
    private void zuMaske2(ActionEvent event){
        Installer.maske2();
    }
    
    
    @FXML 
    protected void installieren(ActionEvent event) {
        System.out.println(name.getText());
    }
    
    @FXML
    private void main(ActionEvent event){   
        Installer.zumain();
    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
    	
    }    
    
}
[XML]
<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="310.0" prefWidth="522.0000999999975" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.StyleController">
<children>
<TextField fx:id="name" layoutX="182.0" layoutY="108.0" prefWidth="326.0" />
<TextField fx:id="email" layoutX="182.0" layoutY="144.0" prefWidth="326.0" />
<TextField fx:id="serial1" layoutX="182.0" layoutY="181.0" prefWidth="103.0" />
<TextField fx:id="serial2" layoutX="294.0" layoutY="181.0" prefWidth="103.0" />
<TextField fx:id="serial3" layoutX="406.0" layoutY="181.0" prefWidth="103.0" />
<Button layoutX="271.0" layoutY="277.0" mnemonicParsing="false" onAction="#zuMaske1" text="Zurück" />
<Button layoutX="339.0" layoutY="277.0" mnemonicParsing="false" onAction="#installieren" text="Installieren" />
<Button layoutX="430.0" layoutY="277.0" mnemonicParsing="false" onAction="#abbrechen" text="Abbrechen" />
<Label layoutX="14.0" layoutY="14.0" text="Installer AB Cleaner - Konfiguration">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Label>
<Label layoutX="14.0" layoutY="64.0" text="Registrieren Sie nun ihre Software:">
<font>
<Font size="13.0" />
</font>
</Label>
<Label layoutX="14.0" layoutY="111.0" text="Vor- und Nachname" />
<Label layoutX="14.0" layoutY="144.0" text="E-Mail Adresse" />
<Label layoutX="14.0" layoutY="181.0" text="Serial-Key" />
<Label layoutX="13.0" layoutY="236.0" prefWidth="495.0001220703125" text="Klicken Sie auf Installieren um fortzufahren oder auf Abbrechen um das Setup zu beenden" />
</children>
</AnchorPane>
[/XML]
Diese Version funktioniert und gibt in der Console den Text aus, der im Name- Feld eingegeben wurde, wenn auf Installieren gedrückt wird.
Die Probleme in deinem Code:
Java:
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        
        name = new TextField();
        email = new TextField();
        
    }
Da du das Interface Initializable implementierst (mit der Methode initialize die implementiert wird) wird die Methode initialize vor dem Darstellen des Gui ausgeführt. Nun dies ist ok, aber du erstellst hier 2 Instanzen TextField... das ist nicht gut, denn alle componenten (textField, button RadioButton etc) welche im fxml deklariert werden, werden beim Start vom Controller automatisch geladen. Diese dürfen nicht manuell im Controller instanziert werden (naja, ausser man will dynamisch Components erstellen)
Weiter hast du, wie schon erwähnt ganz am Anfang vom Controller die textField definiert und erstellst dabei auch neue Instanzen.
Java:
@FXML
    public static TextField name;
    @FXML
    public static TextField email;
    @FXML
    public static final TextField serial1 = new TextField();
    @FXML
    public static final TextField serial2 = new TextField();
    @FXML
    public static final TextField serial3 = new TextField();
diese braucht es nicht...
um im Controller auf die Elemente, die im fxml deklariert sind zugreifen zu können genügt das hier:
Java:
    @FXML
    TextField name;
    @FXML
    TextField email;
    @FXML
    TextField serial1;
    @FXML
    TextField serial2;
Ich hoffe, dass dir dies genügt, um weiter zu kommen...
 
Zuletzt bearbeitet:
Ersteinmal nennt man keine Methode "main". Das irritiert nur.

Zudem hast du das Prinzip von den "@FXML" nicht so wirklich verstanden. Du darfst du einzelnen Instanzvariablen nicht einfach instanziieren. (Okey, ich habe das selbst noch nie versucht, aber bin mir ziemlich sicher, dass es Murks ist).

Java:
@Override
    public void initialize(URL url, ResourceBundle rb) {
       
        name = new TextField();
        email = new TextField();
       
    }

Wie genau nutzt du den Controller? Ich wette du nimmst nicht die load-Methode, sondern machst einfach ein "SytleController controller = new StyleController();"?
 
Danke knilch! Das instanziieren war das Problem jetzt klappt alles wieder!:toll::applaus:

Wie genau nutzt du den Controller? Ich wette du nimmst nicht die load-Methode, sondern machst einfach ein "SytleController controller = new StyleController();"?
Wenn es dich so brennent interessiert hier noch meine Main klasse:
Java:
package installer;

import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author Lukas
 */
public class Installer extends Application {
    
    public static Stage stage;
    
    @Override
    public void start(Stage primarystage) throws Exception {
        
        stage = primarystage;
        
        main();
        
        stage.show();
    
    }
    
    public void main(){
        
        try {
            generateSceneContent("Style.fxml");
        } catch (Exception ex) {
            Logger.getLogger(Installer.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    public static void maske1(){
        
        replaceSceneContent("Maske1.fxml");
        
    }
    
    public static void maske2(){
        
        replaceSceneContent("Maske2.fxml");
        
    }
    
    public static void maske3(){
        
        replaceSceneContent("Maske3.fxml");
        
    }
    
    public static void zumain(){
        
        replaceSceneContent("Style.fxml");
        
    }
    
        private static Initializable replaceSceneContent(String fxml){

		FXMLLoader loader = new FXMLLoader();

		try {

			loader = new FXMLLoader();
			InputStream in = Installer.class.getResourceAsStream(fxml);
			loader.setBuilderFactory(new JavaFXBuilderFactory());
			loader.setLocation(Installer.class.getResource(fxml));
			Parent page;
			try {
				page = (Parent) loader.load(in);
			} finally {
				in.close();
			}
			page.autosize();
			stage.getScene().setRoot(page);
			stage.sizeToScene();

		} catch (IOException e) {
			System.err.println(e.getMessage());
		}

		return (Initializable) loader.getController();
	}

	private Initializable generateSceneContent(String fxml) throws Exception {
		FXMLLoader loader = new FXMLLoader();
		InputStream in = getClass().getResourceAsStream(fxml);
		loader.setBuilderFactory(new JavaFXBuilderFactory());
		loader.setLocation(Installer.class.getResource(fxml));
		Parent page;
		try {
			page = (Parent)loader.load(in);
		} finally {
			in.close();
		}
		Scene scene = new Scene(page, 522, 310);
		stage.setResizable(false);
		stage.setScene(scene);        
		stage.sizeToScene();
		stage.setTitle("Installer");
		return (Initializable) loader.getController();

	}
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
}

MFG
programmierer12
 

Neue Themen


Zurück
Oben