Skip to content
Advertisement

JavaFX ListView controller communication and and Database Issues

Basically I have a main scene with a list and an Add Student button. Clicking the Add Student button opens a new window in which there are two text fields, name and surname. These two are then passed to a Student object constructor that contains, among other attributes, the last and first name of the student. The new student object is now stored and I use a method addToNames that uses student.getName() and student.getSurname() to add the attributes into the list view. The issue is that it doesn’t display the names in the list, and while debugging with print statements, I found out each student object overrides the previous one the way I implemented it. It got even stranger because I remember trying out simple print statement variations of implementing this, and some worked some didn’t. Only example I can remember is that when I used print(“lol”) in my addToNames method as it is right now, It would work. I also want to store these values in a MySQL database with which I already have an established connection. However, I don’t know how to do this . Here is my code

My JavaFX main scene controller class

public class Events {

@FXML
private Button studentADD;
@FXML
private Button add;
@FXML
private ListView<String> nameslist;

private Student[] Students = new Student[200];



public void addToNames(Student student) {
    nameslist.getItems().add("xd");
    System.out.println(student.getName() + " " + student.getSurname());
}


public void switchToStudentAdd(ActionEvent e) throws IOException {
    //Student stud = new Student("John", "Appleseed");
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("AddStud.fxml"));
    Parent root = (Parent) fxmlLoader.load();
    Stage stage = new Stage();
    stage.setScene(new Scene(root));  
    stage.show();
    //nameslist.getItems().add(stud.getName() + " " + stud.getSurname());
}}
        

My JavaFX add student window secondary scene controller class

public class AddStudEvents{

@FXML
private TextField firstName;
@FXML
private TextField lastName;
@FXML
private Button addStudent;

public void addNameToList(ActionEvent e) throws IOException {
    String name = firstName.getText();
    String lastname = lastName.getText();
    
    FXMLLoader loader = new FXMLLoader(getClass().getResource("Main.fxml"));
    Parent root = loader.load();
    Events maincontroller = loader.getController();
    
    Student student = new Student(name, lastname);
    maincontroller.addToNames(student);
}}

My SQL adding method (It does work, however I want it to insert variable student.getName() and a variable student.getSurname() strings, as well as an array of student Marks which is another attribute of the student class, I don’t know if this is possible for arrays though. I head somewhere I need to use a JSON, but I’m not sure what that means. I am using SQL workbench)

public class DatabaseControl {

static String url = "jdbc:mysql://localhost:3306/ia";
static String username = "root";
static String password = "sifra123";
static Connection connection = null;

public static void main(String[] args) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException, SQLException {
    Class.forName("com.mysql.jdbc.Driver").getDeclaredConstructor().newInstance();
    connection = DriverManager.getConnection(url, username, password);
    PreparedStatement ps = connection.prepareStatement("INSERT INTO ia.student (name) VALUES ('yussef walker');");
    PreparedStatement ps1 = connection.prepareStatement("INSERT INTO ia.student (surname) VALUES ('yussef walker');");
    PreparedStatement ps2 = connection.prepareStatement("INSERT INTO ia.student (surname) VALUES ('yussef walker');");
    
    int status = ps.executeUpdate();
    
    if(status != 0) {
        System.out.println("Connection established");
        System.out.println("Inserted");
    }

}}

Sorry for putting the final curly braces so strangely, the text editor won’t let me do it properly.

EDIT: I would prefer to just ad it to the database and then be able to populate a listview with my database name and last name if possible

Advertisement

Answer

I think your main issue here comes from the fact that, in order to get to the controller, you reload the view from FXML every time to attempt to add a name. If you reload the FXML you are not getting the same instance, but a new one. Think of it as creating an empty copy.

The reason the loader will behave this way is to make FXML reusable. For instance, if you have a Save dialog that you want to reuse in multiple places, you wouldn’t want changes you make to one instance to affect all others.

Try to change your coding accordingly – for instance, when you load your dialog for adding students, you could wire it to the main view’s controller via a setter.

As for the SQL question, you may want to create a separate question. To get you started, you might want to look at the javadoc for PreparedStatement, it holds an example on how to pass values to the statement right at the top of the page.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement