0

I have a TableView thats populated by a MySQL table in my database. Each data item in the table has a priority (1/2/3). What I'm attempting to do is change the colour of a row based on the job's priority (red for 1, green for 3 etc).

Could I place the value of a SELECT statement that takes the priority for each row and place it into an array, a loop then runs through the array and changes the colour of each row? For example, if priority == 3, colour column red? Would I need to do this with CSS?

Here is the code for populating the table view currently (Based on James_D comment/advice);

public void UserGUI(Connection connection) throws SQLException {

    user = new User();
    privID = user.getPrivID(connection);

    dataAccessor = new JobDataAccessor();
    deleteButton = new Button("Delete Job");
    addButton = new Button("Add Job");
    deleteButton.setMinWidth(100);
    addButton.setMinWidth(100);

    jobTable = new TableView<>();
    TableColumn<Job, String> caseNumberCol = new TableColumn<>("CaseNO");
    caseNumberCol.setCellValueFactory(new PropertyValueFactory<>("caseNumber"));

    TableColumn<Job, String> caseNotesCol = new TableColumn<>("Case Notes");
    caseNotesCol.setCellValueFactory(new PropertyValueFactory<>("caseNotes"));

    TableColumn<Job, Date> dateCreatedCol = new TableColumn<>("Date Created");
    dateCreatedCol.setCellValueFactory(new PropertyValueFactory<>("dateCreated"));

    TableColumn<Job, Date> deadlineDateCol = new TableColumn<>("Deadline Date");
    deadlineDateCol.setCellValueFactory(new PropertyValueFactory<>("deadlineDate"));

    TableColumn<Job, String> priorityCol = new TableColumn<>("Priority");
    priorityCol.setCellValueFactory(new PropertyValueFactory<>("prioritySetting"));

    TableColumn<Job, String> completedCol = new TableColumn<>("Completed (Y/N)");
    completedCol.setCellValueFactory(new PropertyValueFactory<>("completedStatus"));

    jobTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
    jobTable.getColumns().addAll(caseNumberCol, caseNotesCol, dateCreatedCol, deadlineDateCol, priorityCol,
            completedCol);
    jobTable.setMinWidth(700);

    if (privID < 20) {
        String query = "SELECT * FROM progdb.adamJobs";
        jobTable.getItems().addAll(dataAccessor.getJobList(connection, query));
    } else if (privID > 20) {
        String query = "SELECT * FROM progdb.kitJobs";
        jobTable.getItems().addAll(dataAccessor.getJobList(connection, query));
    }

    gPaneMain = new GridPane();
    gridpane1 = new GridPane();
    primaryStage = new Stage();
    scene = new Scene(gPaneMain, 900, 600);

    gPaneMain.setAlignment(Pos.CENTER);
    gridpane1.setAlignment(Pos.CENTER);
    gPaneMain.setGridLinesVisible(true);
    gridpane1.setGridLinesVisible(true);
    gPaneMain.setVgap(10);
    gridpane1.setVgap(10);
    gridpane1.setHgap(10);

    GridPane.setHalignment(jobTable, HPos.CENTER);
    gPaneMain.add(jobTable, 0, 0);

    GridPane.setHalignment(gridpane1, HPos.CENTER);
    gPaneMain.add(gridpane1, 0, 1);

    GridPane.setHalignment(addButton, HPos.CENTER);
    gridpane1.add(addButton, 1, 0);

    GridPane.setHalignment(deleteButton, HPos.CENTER);
    gridpane1.add(deleteButton, 2, 0);

    primaryStage.getIcons().add(sdcLogo);
    scene.getStylesheets().add("Style.css");

    primaryStage.setTitle("Logged In As: " + username);
    primaryStage.setScene(scene);
    primaryStage.show();

And the code for the model class (Based on James_D comment/advice);

public class Job {

private String caseNumber;
private String caseNotes;
private String dateCreated;
private String deadlineDate;
private int prioritySetting;
private String completedStatus;

public String getCaseNumber() {
    return caseNumber;
}

public void setCaseNumber(String caseNumber) {
    this.caseNumber = caseNumber;
}

public String getCaseNotes() {
    return caseNotes;
}

public void setCaseNotes(String caseNotes) {
    this.caseNotes = caseNotes;
}

public String getDateCreated() {
    return dateCreated;
}

public void setDateCreated(String dateCreated) {
    this.dateCreated = dateCreated;
}

public String getDeadlineDate() {
    return deadlineDate;
}

public void setDeadlineDate(String deadlineDate) {
    this.deadlineDate = deadlineDate;
}

public int getPrioritySetting() {
    return prioritySetting;
}

public void setPrioritySetting(int prioritySetting) {
    this.prioritySetting = prioritySetting;
}

public String getCompletedStatus() {
    return completedStatus;
}

public void setCompletedStatus(String completedStatus) {
    this.completedStatus = completedStatus;
}

public Job(String caseNumber, String caseNotes, String dateCreated, String deadlineDate, int prioritySetting, String completedStatus) {
    setCaseNumber(caseNumber);
    setCaseNotes(caseNotes);
    setDateCreated(dateCreated);
    setDeadlineDate(deadlineDate);
    setPrioritySetting(prioritySetting);
    setCompletedStatus(completedStatus);
}
jbanks
  • 163
  • 1
  • 6
  • 17
  • If you know the columns in your database table, you should create a model class, instead of just using an `ObservableList` for each row. Then the priority will be one of the properties of that object, and you can create a `rowFactory`. But until you have a model class, it's hard to answer this question. See http://stackoverflow.com/questions/25651641/javafx-mysql-connection-example-please to get started. – James_D Aug 25 '15 at 10:41
  • That's interesting, thanks for the comment. I haven't seen anything about a model class before. The model is just the embodiment of each table I have in the database? How would the mysql query then populate the TableView? – jbanks Aug 25 '15 at 10:44
  • Almost every example of JavaFX `TableView` contains a model class; for example the standard [Oracle tutorial](http://docs.oracle.com/javase/8/javafx/user-interface-tutorial/table-view.htm#CJAGAAEE) uses a `Person` class to model each row of the table. The example I linked in the previous comment has a complete example of using populating a table modeled by a `Person` class from a person table in a database. – James_D Aug 25 '15 at 10:47
  • @James_D I have now implemented a model class for a "job". How would I go about saying if the prioirty is 3/2/1 then highlight that row? – jbanks Aug 25 '15 at 15:12
  • Can you edit your question with the model class and updated code for populating the TableView? – James_D Aug 25 '15 at 16:29
  • Modified the question @James_D – jbanks Aug 26 '15 at 08:04

0 Answers0