0

This is my JavaFX code and I am trying to create a to-do list. When I try to populate my Task tab, it is not accepting the entry. The rest of the tab is accepting the entries.

I believe it has to do with the setter and getter methods. I tried using the lambda expression instead of the PropertyValue() but could not find the solutions.

The text field is supposed to accept the entry when the user enters any input.

import javafx.application.*;
import javafx.beans.property.SimpleStringProperty;
import javafx.geometry.Insets;
import javafx.scene.layout.*;
import javafx.scene.text.Font;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.stage.*;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.collections.ObservableList;
import javafx.collections.FXCollections;
import javafx.event.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import java.awt.Desktop;
import java.io.File;
// import java.io.FileInputStream;
// import java.io.FileOutputStream;
import java.io.IOException;
// import java.io.ObjectInputStream;
// import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.print.PrinterJob;


/**
 *
 * @author Bahr
 */
public class App extends Application {
    
    private final TableView<Task> tableChron = new TableView<Task>();
    private final ObservableList<Task> data =
        FXCollections.observableArrayList(
        new Task("Wash car", "Saturday", "8:00am", "Monthly", "Dad", "Squeaky clean"),
        new Task("Java project", "Mon & Wed", "6:00am", "Apr 23", "Professor Lambda", "Use JavaFX, git & command line"));
        
    final HBox hb = new HBox();
    Label response;
    
    private Desktop desktop = Desktop.getDesktop();
    
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        
        //To create: Add check box next to each task you add, delete and edit buttons, add a week and month view, and a menu on top so you can save different copies. 
                
        VBox vBox1 = new VBox(); // Create new vertical box.
        Scene scene1 = new Scene(vBox1); // Create scene1.
        vBox1.setSpacing(7); // Spacing for box.
    
        response = new Label("Menu");
        
        MenuBar menuBar = new MenuBar();
        
        Menu fileMenu = new Menu("_File"); // Alt-f shortcut for file 
        MenuItem open = new MenuItem("Open...");
        MenuItem save = new MenuItem("Save As...");
        MenuItem print = new MenuItem("Print...");
        MenuItem exit = new MenuItem("Exit");
        SeparatorMenuItem separator = new SeparatorMenuItem();
               
        fileMenu.getItems().add(open);
        fileMenu.getItems().add(save);
        fileMenu.getItems().add(print);
        fileMenu.getItems().add(separator);
        fileMenu.getItems().add(exit);
                        
        menuBar.getMenus().add(fileMenu);
        
        // Create one event handler that will handle menu action events.
        EventHandler<ActionEvent> MEHandler = 
                new EventHandler<ActionEvent>() {
            public void handle(ActionEvent ae) {
                String name = ((MenuItem)ae.getTarget()).getText();
 
                // if Exit is chosen, the program is  terminated.
                if(name.equals("Exit")) Platform.exit();
 
                response.setText( name + " selected");
            }
        };
        
        open.setOnAction(MEHandler);
        save.setOnAction(MEHandler);
        print.setOnAction(MEHandler);
        exit.setOnAction(MEHandler);
        
        final FileChooser fileChooser = new FileChooser();
                        
        open.setOnAction(
                new EventHandler<ActionEvent>(){
                    @Override
                    public void handle(final ActionEvent e) {
                        File opensFile = fileChooser.showOpenDialog(primaryStage);
                        if (opensFile != null) {
                            openFile(opensFile);
                        }
                    }
                
                });
        
        save.setOnAction(
                new EventHandler<ActionEvent>(){
                    @Override
                    public void handle(ActionEvent e) {
                        File savesFile = fileChooser.showSaveDialog(primaryStage);
                        if (savesFile != null) {
                            saveFile(savesFile);
                        }
                    }
                    
                });
        
        // Allow a clicks on the print menuItem to open the print dialogue box.
        print.setOnAction(
                new EventHandler<ActionEvent>(){
                    @Override
                    public void handle(ActionEvent e) {
                        PrinterJob job = PrinterJob.createPrinterJob();
                        if (job != null) {
                            boolean showPrintDialog = job.showPrintDialog(primaryStage.getOwner());
                            if(showPrintDialog){
                                job.endJob();
                            }
                        }
                    }
                });
                                      
        final Label label = new Label("To Do List"); // Main window heading label.
    label.setFont(new Font("Arial", 18));
        label.setPadding(new Insets(10, 10, 10, 10));
        
        tableChron.setEditable(true);
        
        // Column headings in the tableChron.
        TableColumn<Task,String> taskCol = new TableColumn<>("Task");
        TableColumn<Task,String> dayCol = new TableColumn<>("Day");
        TableColumn<Task,String> timeCol = new TableColumn<>("Time");
        TableColumn<Task,String> deadlineCol = new TableColumn<>("Deadline");
        TableColumn<Task,String> mentorCol = new TableColumn<>("Mentor");
        TableColumn<Task,String> descriptionCol = new TableColumn<>("Task Description");
                
        taskCol.setCellValueFactory(
            new PropertyValueFactory<Task, String>("Task"));
        taskCol.setCellFactory(TextFieldTableCell.forTableColumn());
        taskCol.setOnEditCommit(
            new EventHandler<TableColumn.CellEditEvent<Task, String>>() {
                @Override
                public void handle(TableColumn.CellEditEvent<Task, String> t) {
                    ((Task) t.getTableView().getItems().get(
                        t.getTablePosition().getRow())
                        ).setToDo(t.getNewValue());
                }
            }
        );
        
        dayCol.setCellValueFactory(
            new PropertyValueFactory<Task, String>("Day"));
        dayCol.setCellFactory(TextFieldTableCell.forTableColumn());
        dayCol.setOnEditCommit(
            new EventHandler<TableColumn.CellEditEvent<Task, String>>() {
                @Override
                public void handle(TableColumn.CellEditEvent<Task, String> t) {
                    ((Task) t.getTableView().getItems().get(
                        t.getTablePosition().getRow())
                        ).setDay(t.getNewValue());
                }
            }
        );
        
        timeCol.setCellValueFactory(
            new PropertyValueFactory<Task, String>("Time"));
        timeCol.setCellFactory(TextFieldTableCell.forTableColumn());
        timeCol.setOnEditCommit(
            new EventHandler<TableColumn.CellEditEvent<Task, String>>() {
                @Override
                public void handle(TableColumn.CellEditEvent<Task, String> t) {
                    ((Task) t.getTableView().getItems().get(
                        t.getTablePosition().getRow())
                        ).setTime(t.getNewValue());
                }
            }
        );
        
        deadlineCol.setCellValueFactory(
            new PropertyValueFactory<Task, String>("Deadline"));
        deadlineCol.setCellFactory(TextFieldTableCell.forTableColumn());
        deadlineCol.setOnEditCommit(
            new EventHandler<TableColumn.CellEditEvent<Task, String>>() {
                @Override
                public void handle(TableColumn.CellEditEvent<Task, String> t) {
                    ((Task) t.getTableView().getItems().get(
                        t.getTablePosition().getRow())
                        ).setDeadline(t.getNewValue());
                }
            }
        );
        
        mentorCol.setCellValueFactory(
            new PropertyValueFactory<Task, String>("Mentor"));
        mentorCol.setCellFactory(TextFieldTableCell.forTableColumn());
        mentorCol.setOnEditCommit(
            new EventHandler<TableColumn.CellEditEvent<Task, String>>() {
                @Override
                public void handle(TableColumn.CellEditEvent<Task, String> t) {
                    ((Task) t.getTableView().getItems().get(
                        t.getTablePosition().getRow())
                        ).setMentor(t.getNewValue());
                }
            }
        );
        
        descriptionCol.setCellValueFactory(
            new PropertyValueFactory<Task, String>("Description"));
        descriptionCol.setCellFactory(TextFieldTableCell.forTableColumn());
        descriptionCol.setOnEditCommit(
            new EventHandler<TableColumn.CellEditEvent<Task, String>>() {
                @Override
                public void handle(TableColumn.CellEditEvent<Task, String> t) {
                    ((Task) t.getTableView().getItems().get(
                        t.getTablePosition().getRow())
                        ).setDescription(t.getNewValue());
                }
            }
        );
        
        tableChron.setItems(data);
        tableChron.getColumns().add(taskCol);
        tableChron.getColumns().add(dayCol);
        tableChron.getColumns().add(timeCol);
        tableChron.getColumns().add(deadlineCol);
        tableChron.getColumns().add(mentorCol);
        tableChron.getColumns().add(descriptionCol);


        // tableChron.getColumns().addAll(taskCol, dayCol, timeCol, deadlineCol, mentorCol, descriptionCol); // Place the column headings in tableChron.
    // Set width
    tableChron.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
    taskCol.setMinWidth(150);
    dayCol.setMinWidth(70);
    timeCol.setMinWidth(60);
    deadlineCol.setMinWidth(60);
    mentorCol.setMinWidth(120);
    descriptionCol.setMinWidth(200);

    // Create horizontal box.
    HBox hbox1 = new HBox();
    hbox1.setSpacing(8);
        hbox1.setPadding(new Insets(10, 10, 10, 10));
        
        // Create text fields so the user can enter new tasks into the table.
    TextField tftask = new TextField();
    TextField tfDay = new TextField();
    TextField tfTime = new TextField();
    TextField tfDeadline = new TextField();
    TextField tfmentor = new TextField();
    TextField tfdesc = new TextField();
        
        // Set initial text in fields.
    tftask.setText("Enter task");
    tfDay.setText("Enter day");
    tfTime.setText("Time");
    tfDeadline.setText("Deadline");
    tfmentor.setText("Give report to...");
    tfdesc.setText("Add specific descriptions");

        // Set length
        tftask.setPrefWidth(150);
        tfDay.setPrefWidth(75);
        tfTime.setPrefWidth(65);
        tfDeadline.setPrefWidth(65);
        tfmentor.setPrefWidth(120);
        tfdesc.setPrefWidth(150);
        
    // Create add button. 
    Button btnAdd = new Button("Add");
    btnAdd.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                data.add(new Task(
                    tftask.getText(),
                    tfDay.getText(),
                    tfTime.getText(),
                    tfDeadline.getText(),
                    tfmentor.getText(),
                    tfdesc.getText()));
                    tftask.clear();
                    tfDay.clear();
                    tfTime.clear();
                    tfDeadline.clear();
                    tfmentor.clear();
                    tfdesc.clear();
            }
        });
        
        // Get user entry fields all in a horizontal row.
    hbox1.getChildren().addAll(tftask, tfDay, tfTime, tfDeadline, tfmentor, tfdesc, btnAdd);
        
    // Get all items in a vertical, stacking format.
    vBox1.getChildren().addAll(menuBar, label, tableChron, hbox1);

    primaryStage.setWidth(750);
    primaryStage.setHeight(500);
    primaryStage.setTitle("Task Tracker 1.0"); // Names the window.
    primaryStage.setScene (scene1); // Sets the scene to the stage.
    primaryStage.show(); // Shows the stage.
    }
    
    public static class Task {
        
        private final SimpleStringProperty todo;
        private final SimpleStringProperty day;
        private final SimpleStringProperty time;
        private final SimpleStringProperty deadline;
        private final SimpleStringProperty mentor;
        private final SimpleStringProperty description;
               
        private Task(String todo1, String day1, String time1, String deadline1, String mentor1, String description1){
            
            this.todo = new SimpleStringProperty(todo1);
            this.day = new SimpleStringProperty (day1);
            this.time = new SimpleStringProperty(time1);
            this.deadline = new SimpleStringProperty(deadline1);
            this.mentor = new SimpleStringProperty(mentor1);
            this.description = new SimpleStringProperty(description1);
        }
        
        public String getToDo() {
            return todo.get();
        }
        
        public void setToDo(String todo1) {
            todo.set(todo1);
        }
        
        public String getDay() {
            return day.get();
        }
        
        public void setDay(String day1) {
            day.set(day1);
        }
        
        public String getTime() {
            return time.get();
        }
        
        public void setTime(String time1) {
            time.set(time1);
        }
        
        public String getDeadline() {
            return deadline.get();
        }
        
        public void setDeadline(String deadline1) {
            deadline.set(deadline1);
        }
        
        public String getMentor() {
            return mentor.get();
        }
        
        public void setMentor(String mentor1) {
            mentor.set(mentor1);
        }
        
        public String getDescription() {
            return description.get();
        }
        
        public void setDescription(String deadline1) {
            description.set(deadline1);
        }
                
    }
    
    private void openFile(File opensFile) {
        try{
            desktop.open(opensFile);
        } catch (IOException ex) {
            Logger.getLogger(
                    App.class.getName()).log(
                            Level.SEVERE, null, ex
                    );
        }
    }
    
    private void saveFile(File savesFile) {
        try{
            desktop.open(savesFile);
        } catch (IOException ex) {
            Logger.getLogger(
                    App.class.getName()).log(
                            Level.SEVERE, null, ex
                    );
        }
    }

}

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<VBox fx:id="VBox" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="819.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="App">
   <children>
      <MenuBar fx:id="MenuBar">
        <menus>
          <Menu fx:id="Menu" mnemonicParsing="false" text="File" />
        </menus>
      </MenuBar>
      <Label prefHeight="61.0" prefWidth="133.0" text="TO DO LIST">
         <font>
            <Font size="24.0" />
         </font>
      </Label>
      <TableView fx:id="tableview" prefHeight="200.0" prefWidth="712.0">
        <columns>
          <TableColumn fx:id="taskCol" prefWidth="75.0" text="TASK" />
          <TableColumn fx:id="dayCol" prefWidth="75.0" text="DAY" />
            <TableColumn fx:id="timeCol" prefWidth="75.0" text="TIME" />
            <TableColumn fx:id="deadlineCol" prefWidth="75.0" text="DEADLINE" />
            <TableColumn fx:id="mentorCol" prefWidth="75.0" text="MENTOR" />
            <TableColumn fx:id="descriptionCol" prefWidth="125.0" text="TASK DESCRIPTION" />
        </columns>
      </TableView>
      <HBox fx:id="Hbox" prefHeight="100.0" prefWidth="200.0">
         <children>
            <TextField fx:id="tftask" prefHeight="26.0" prefWidth="111.0" promptText="Enter Task" />
            <TextField fx:id="tfday" prefHeight="26.0" prefWidth="118.0" promptText="Enter Day" />
            <TextField fx:id="tftime" prefHeight="26.0" prefWidth="96.0" promptText="Time" />
            <TextField fx:id="tfdeadline" prefHeight="26.0" prefWidth="98.0" promptText="Deadline" />
            <TextField fx:id="tfmentor" prefHeight="26.0" prefWidth="123.0" promptText="Give report to" />
            <TextField fx:id="tfdesc" promptText="Add specific description" />
            <Button fx:id="btnAdd" mnemonicParsing="false" onAction="#buttonADD" prefHeight="26.0" prefWidth="117.0" text="ADD" />
         </children>
      </HBox>
   </children>
</VBox>
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • the Task class has no property named "Task" .. anyway: __understand__ how to use tableView, all factories are given the wrong parameter (though the implementation is lenient in this case ;), do not use propertyValueFactory. When asking for debugging help, always provide a [mcve] minding the __M__! – kleopatra May 11 '22 at 07:51
  • `java.awt.Desktop` is not the right thing to use to open and save your todo list files. – jewelsea May 11 '22 at 23:40
  • Perhaps study the [makery tutorial](https://code.makery.ch/library/javafx-tutorial/), that might help you with your application. Not this specific question which is already answered in the duplicate, but just as an approach in general. – jewelsea May 11 '22 at 23:42

0 Answers0