paul paul - 1 month ago 8
Java Question

Equally spacing string while also aligning them

I have a ListView and the top most item looks as follows.

ID FIRST NAME LAST NAME DOB SS PHONE NUMBER


Now under these item I want to place other items that actually contain this info

ID FIRST NAME LAST NAME DOB SS PHONE NUMBER
1 Paul Lopez some date some number some number


I cant seem to figure out how to align these items so that the length of the string doesn't effect the alignment (i.e if u add two letter to Paul it won't push everything in front forward). Here is what i have.

String headerString = ID FIRST NAME LAST NAME DOB SS PHONE NUMBER;
private void updateEmployees(){
employeeLV.getItems().clear();
employeeLV.getItems().add(headerString);
for (EmployeeInfo employee : dateBaseHelper.getAllEmployees()){
employeeLV.getItems().add(
employee.getId() + spacer
+ employee.getfName() + spacer
+ employee.getlName() + spacer + spacer
+ employee.getDob() + spacer + spacer
+ employee.getSsNum() + spacer + spacer
+ employee.getPhoneNumber());
}
}


I do realize this is a poor attempt and I hope you guys can help.

Answer

What you need is a TableView. With a TableView you are able to make auto-aligned columns. So you can just insert values (of different types) and let JavaFX do the alignment for you.

You may have a look at this tutorial about TableViews.

Please consider to implement your employee to have SimpleIntegerProperty, SimpleStringProperty and SimpleObjectProperty as fields instead of int, String and Date. This would make using a TableView much easier.