javafx populate dynamic tableview from database with checkbox - sqlite

I want to populate a dynamic tableview from database with 32 columns,First column contain the name of employee and remaining 31 columns for marking employee attendence from day1 to day31(checkbox).But I can able to populate tableview from database with 2 columns(name,checkbox) using get and set methods.Here is my code
String sql ="SELECT * FROM attendence";
pst = (PreparedStatement) con.prepareStatement(sql);
rs=pst.executeQuery();
while (rs.next()) {
//get string from db,whichever way
String name=rs.getString(3);
int day=rs.getInt(6);
data.add(new User(name,day!=0)); //converting integer to boolean and storing on data(Observable list)
}
etname.setCellValueFactory(new PropertyValueFactory<>("name"));
col.setCellValueFactory(new PropertyValueFactory<>("day1));
col.setCellFactory(new Callback<TableColumn<User, Boolean>, TableCell<User, Boolean>>() {
public TableCell<User, Boolean> call(TableColumn<User, Boolean> p) {
return new CheckBoxTableCell<User, Boolean>();
}
});
jTable.getColumns().add(etname,col);
jTable.setItems(data);
}
This is User.java
public class User {
private final SimpleStringProperty ename;
private BooleanProperty day1;
User(String Ename,boolean day1)
{
this.ename = new SimpleStringProperty(Ename);
this.day1 = new SimpleBooleanProperty(day1);
this.day1.addListener(new ChangeListener<Boolean>() {
public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
System.out.println(enameProperty().get() + " invited: " + t1);
System.out.println();
}
});
}
public String getEname() {
return ename.get();
}
public void setEname(String Ename) {
ename.set(Ename);
}
public BooleanProperty day1Property() {
return day1;
}
public StringProperty enameProperty() {
return ename;
}
CheckBoxTableCell.java
public class CheckBoxTableCell<S, T> extends TableCell<S, T> {
private final CheckBox checkBox;
private ObservableValue<T> ov;
public CheckBoxTableCell() {
this.checkBox = new CheckBox();
this.checkBox.setAlignment(Pos.CENTER);
setAlignment(Pos.CENTER);
setGraphic(checkBox);
}
#Override public void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
setGraphic(checkBox);
if (ov instanceof BooleanProperty) {
checkBox.selectedProperty().unbindBidirectional((BooleanProperty) ov);
}
ov = getTableColumn().getCellObservableValue(getIndex());
if (ov instanceof BooleanProperty) {
checkBox.selectedProperty().bindBidirectional((BooleanProperty) ov);
}
}
}
}
But no idea how i can do with 32 columns.
So i need a big help from anyone for my 2 problems.
1) How i can populate dynamic tableview from database with checkboxes 2) when i press a button should read all the names along with checkbox status(isSelected or not selected) like
jhon true true false ... ....
rose false false true ... ..
george true true true false
Answers will be appreciated.Thank you in advance.
.. ..

try with this code.
private static final List<String> groups = Arrays.asList("Group 1", "Group 2", "Group 3", "Group 4"); //declared an array
TableView<AttributeRow> attributeTable = new TableView<>(); //new Table
for (String group : groups) { //Creating dynamic column
TableColumn<AttributeRow, Boolean> groupColumn = new TableColumn<>(group);
groupColumn.setCellFactory(CheckBoxTableCell.forTableColumn(groupColumn));
groupColumn.setCellValueFactory(cellData -> cellData.getValue().activeProperty(group));
attributeTable.getColumns().add(groupColumn);
}
and AttributeRow class
class AttributeRow {
private final Map<String, BooleanProperty> activeByGroup = new HashMap<>();
public AttributeRow(List<String> companyGroups) {
for (String group : companyGroups) {
activeByGroup.put(group, new SimpleBooleanProperty()) ;
}
}
public final BooleanProperty activeProperty(String group) {
return activeByGroup.get(group) ;
}
public final boolean isActive(String group) {
return activeProperty(group).get();
}
public final void setActive(String group, boolean active) {
activeProperty(group).set(active);
}
For loading checkbox data(true or false) we can use following method
String sql ="SELECT * FROM attendence";
pst = (PreparedStatement) con.prepareStatement(sql);
rs=pst.executeQuery();
while (rs.next()) {
AttributeRow row = new AttributeRow(groups);
for(int j=0;j<5;j++) //here j<5 because array size =5
{
int i=j+6;
int day=rs.getInt(i); //getting Integer value(note:sqlite does not support boolean datatype so i stored as integer with 0 or 1)
row.setActive(day!=0); //converted into boolean useing day!=0
}
jTable.getItems().add(row);
}

Related

Javafx: Reduce events of checkable Listbox

I have a generic Javafx Listbox with checkable items (up to 20 items).
When I (de-)select an item, a property change is released to update other parts of the program.
It works all fine so far.
But I have additionally 2 buttons to (de-)select all items at once.
Also this working as expected, but I get an event for each item, which means that up to 20 property change events are fired, when one would be enough.
It is not a performance problem, not much is done on the events, but it is bad style.
Can anybody suggest a better solution?
public class FilterBox extends AnchorPane {
ListView<Item> listFilter;
Button buttonAll;
Button buttonNone;
public FilterBox() {
init();
place();
action();
getChildren().addAll(listFilter, buttonAll, buttonNone);
handleChange();
}
private void init() {
listFilter = new ListView<>();
buttonAll = new Button("All");
buttonNone = new Button("None");
}
private void place() {
setTopAnchor(listFilter, 0d);
setBottomAnchor(listFilter, 40d);
setLeftAnchor(listFilter, 0d);
setRightAnchor(listFilter, 0d);
setBottomAnchor(buttonAll, 5d);
setLeftAnchor(buttonAll, 0d);
buttonAll.setPrefWidth(75d);
setBottomAnchor(buttonNone, 5d);
setRightAnchor(buttonNone, 0d);
buttonNone.setPrefWidth(75d);
}
private void action() {
listFilter.setCellFactory(CheckBoxListCell.forListView((Item item) -> item.selectedProperty()));
buttonAll.setOnAction((t) -> {
changeAll(true);
});
buttonNone.setOnAction((t) -> {
changeAll(false);
});
}
private void changeAll(Boolean state) {
for (Item i : listFilter.getItems()) {
i.setSelected(state);
}
}
private void setListener(Item item) {
item.selectedProperty().addListener((o, ov, nv) -> {
Trigger.setNewFilterEvent();
});
}
private void handleChange() {
HashSet<String> list = InputData.getSportList();
for (String s : list) {
Item item = new Item(s, true);
listFilter.getItems().add(item);
setListener(item);
}
}
public static class Item {
private final StringProperty name = new SimpleStringProperty();
private final BooleanProperty selected = new SimpleBooleanProperty();
public Item(String name, boolean on) {
setName(name);
setSelected(on);
}
public final StringProperty nameProperty() {
return this.name;
}
public final String getName() {
return this.nameProperty().get();
}
public final void setName(final String name) {
this.nameProperty().set(name);
}
public final BooleanProperty selectedProperty() {
return this.selected;
}
public final boolean isSelected() {
return this.selectedProperty().get();
}
public final void setSelected(final boolean sel) {
this.selectedProperty().set(sel);
}
#Override
public String toString() {
return getName();
}
}
}
Assuming Trigger.setNewFilterEvent() is what you want called only once when the "select all" or the "deselect all" actions are fired, then you can use a boolean flag for this.
public class FilterBox {
private boolean ignoreIndividualChanges;
// other fields omitted for brevity
private void changeAll(boolean state) {
ignoreIndividualChanges = true;
try {
for (Item i : listFilter.getItems()) {
i.setSelected(state);
}
Trigger.setNewFilterEvent(); // fire one event
} finally {
ignoreIndividualChanges = false;
}
}
private void setListener(Item item) {
item.selectedProperty().addListener((obs, ov, nv) -> {
if (!ignoreIndividualChanges) {
Trigger.fireNewFilterEvent();
}
});
}
// other methods omitted for brevity
}
Also, note I changed the parameter type for changeAll from Boolean to boolean. There's no reason to use the reference type here, so you should stick with the primitive type.

Listview not displaying the data which is stored in database

I am having trouble displaying the data in listview. In the dialogbox the user enters the item and click save button it stores the data in sqlite database but it does not displaying in listview. when i moved to MainActivity.java and returns back to AddCount.java it display the item which is stored in sqlite database. How can i display the item in listview as soon as user clicks save in dialogbox
public class AddCount extends AppCompatActivity {
ArrayList<User>userList;
User user;
DbHandler myDB;
Cursor data;
int numRows;
Two_columnListAdapter adapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_count);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ListView listView = (ListView)findViewById(R.id.listview);
myDB = new DbHandler(this);
userList = new ArrayList<>();
data = myDB.getListContents();
numRows = data.getCount();
if (numRows == 0) {
Toast.makeText(AddCount.this, "There is nothing in database", Toast.LENGTH_LONG).show();
} else {
while (data.moveToNext()) {
user = new User(data.getString(1), data.getString(2));
userList.add(user);
}
}
adapter = new Two_columnListAdapter(this,R.layout.list_item_layout,userList);
listView.setAdapter(adapter);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openDialog();
}
});
}
public void openDialog() {
final AlertDialog.Builder mydialog = new AlertDialog.Builder(AddCount.this);
mydialog.setTitle("Add Count");
LinearLayout layout = new LinearLayout(AddCount.this);
layout.setOrientation(LinearLayout.VERTICAL);
final EditText title = new EditText(AddCount.this);
title.setHint("Title");
layout.addView(title);
final EditText value = new EditText(AddCount.this);
value.setHint("Count");
layout.addView(value);
mydialog.setView(layout);
mydialog.setPositiveButton("Save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
String UserTitle = title.getText().toString();
String UserCount = value.getText().toString();
if (UserTitle.length()!= 0 && UserCount.length() != 0) {
AddData(UserTitle,UserCount);
title.setText("");
value.setText("");
adapter.notifyDataSetChanged();
}
else {
Toast.makeText(AddCount.this ,"Empty!",Toast.LENGTH_SHORT).show();
}
}
}).create();
mydialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
}).create();
mydialog.show();
}
public void AddData(String title,String count){
boolean insertData = myDB.insertUserInputs(title, count);
if (insertData==true){
Toast.makeText(AddCount.this ,"Saved",Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(AddCount.this ,"Some thing went wrong",Toast.LENGTH_SHORT).show();
}
}
}
DbHandler.java (create and manage the database)
public class DbHandler extends SQLiteOpenHelper {
private static final int DB_VERSION = 1;
private static final String DB_NAME = "users.db";
private static final String TABLE_Inputs = "userinputs";
private static final String KEY_ID = "id";
private static final String KEY_Title = "Title";
private static final String KEY_Count = "Count";
public DbHandler(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
// Create a new table
String CREATE_TABLE = "CREATE TABLE " + TABLE_Inputs + "(ID INTEGER PRIMARY KEY AUTOINCREMENT," + "Title,Count)";
db.execSQL(CREATE_TABLE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if exist
db.execSQL("DROP TABLE IF EXISTS " + TABLE_Inputs);
// Create tables again
onCreate(db);
}
public boolean insertUserInputs(String UserTitle, String UserCount) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_Title, UserTitle);
contentValues.put(KEY_Count, UserCount);
long newRowId = db.insert(TABLE_Inputs, null, contentValues);
if (newRowId == -1){
return false;
}
else {
return true;
}
}
public Cursor getListContents(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor data = db.rawQuery("SELECT * FROM " + TABLE_Inputs,null);
return data;
}
}
Two_columnListAdapter.java
public class Two_columnListAdapter extends ArrayAdapter<User> {
private LayoutInflater layoutInflater;
private ArrayList<User>users;
private int mviewResourceId;
public Two_columnListAdapter(Context context,int textViewResourceId,ArrayList<User>users){
super(context,textViewResourceId,users);
this.users = users;
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mviewResourceId = textViewResourceId;
}
public View getView(int position, View convertView, ViewGroup parents){
convertView = layoutInflater.inflate(mviewResourceId,null);
User user= users.get(position);
if (user != null){
TextView text = (TextView)convertView.findViewById(R.id.title);
TextView num = (TextView)convertView.findViewById(R.id.value);
if (text != null){
text.setText(user.getText());
}
if (num != null){
num.setText(user.getNum());
}
}
return convertView;
}
}
You don't add anything to the list via your adapter, nor you add anything to the list directly when the data is saved to the DB.
You shuold add items to the list via your adapter:
adapter.add(someItem);
or you can add items to the list, then call notifyDataSetChanged
userlist.add(user);
adapter.notifyDataSetChanged();
In your OnClickListener add the user to the list via the adapter...
mydialog.setPositiveButton("Save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
String UserTitle = title.getText().toString();
String UserCount = value.getText().toString();
if (UserTitle.length()!= 0 && UserCount.length() != 0) {
AddData(UserTitle,UserCount);
title.setText("");
value.setText("");
adapter.add(new User(....));
...
Check out this answer on adapters.

Unable select Javafx combobox items programmatically which uses objects

In the JavaFx ComboBox which uses a class object list .I want to select items in the ComboBox programmatically using getSelectionModel().select(object or index). i am not getting the desired result Although the value is set but it is something like this main.dao.Company.Company.CompanyTableData#74541e7b.
The code is somewhat like this.
ComboBox<CompanyTableData> company = new ComboBox<>();
company.setItems(GetCompany.getCompanyTableData());//where Observable list is set..
GetCompany.getCompanyTableData() returns observablelist of CompanyTableData class.
The ComboBox Looks as follows.
The CompanyTableData Class is as.
public class CompanyTableData {
private SimpleStringProperty itemCompanyId;
private SimpleStringProperty itemCompanyName;
private SimpleStringProperty createBy;
private SimpleStringProperty createdOn;
public CompanyTableData(CompanyData companyData){
this.itemCompanyId = new SimpleStringProperty(companyData.getItemCompanyId());
this.itemCompanyName = new SimpleStringProperty(companyData.getItemCompanyName());
this.createBy = new SimpleStringProperty(companyData.getCreatedBy());
this.createdOn = new SimpleStringProperty(companyData.getCreatedOn());
}
public String getItemCompanyId() {
return itemCompanyId.get();
}
public SimpleStringProperty itemCompanyIdProperty() {
return itemCompanyId;
}
public void setItemCompanyId(String itemCompanyId) {
this.itemCompanyId.set(itemCompanyId);
}
public String getItemCompanyName() {
return itemCompanyName.get();
}
public SimpleStringProperty itemCompanyNameProperty() {
return itemCompanyName;
}
public void setItemCompanyName(String itemCompanyName) {
this.itemCompanyName.set(itemCompanyName);
}
public String getCreateBy() {
return createBy.get();
}
public SimpleStringProperty createByProperty() {
return createBy;
}
public void setCreateBy(String createBy) {
this.createBy.set(createBy);
}
public String getCreatedOn() {
return createdOn.get();
}
public SimpleStringProperty createdOnProperty() {
return createdOn;
}
public void setCreatedOn(String createdOn) {
this.createdOn.set(createdOn);
}
}
The Cell Factory is set
company.setCellFactory(param -> new CompanyCell());
And the CompanyCell
public class CompanyCell extends ListCell<CompanyTableData> {
#Override
protected void updateItem(CompanyTableData item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null || item.getItemCompanyName() == null) {
setText(null);
} else {
setText(item.getItemCompanyName());
}
}
}
After all this when i try to set the items programmetically as
company.getSelectionModel().select(getSelectedCompanyIndex());
The getSelectedCompanyIndex() function is as follows.
public static CompanyTableData getSelectedCompanyIndex(){
CompanyTableData c = null,i;
Iterator<CompanyTableData> itr = GetCompany.getCompanyTableData().iterator();
while (itr.hasNext()){
i = itr.next();
if (i.getItemCompanyName().equals(Element.getItemTableData().getCompany())){
c = i;
}
}
return c;
}
And the result i am getting is
And
At the end it should select a name or item in the list but it has set some type of object i think.
Now what should i do. Is there any type of string conversion required.
The buttonCell used to display the item when the combobox popup is not shown is not automatically created using the cellFactory. You need to set this property too to use the same cell implementation:
company.setCellFactory(param -> new CompanyCell());
company.setButtonCell(new CompanyCell());

Javafx Custom TableCell

I have tables with editable fields item,Description,Quantity,Unit price and Sub Total.
I am creating a cellFactory and Column Update like this:
TableColumn DescriptionCol = new TableColumn("Description");
EditableTableSupport.createEditingColumn(DescriptionCol,"description");
TableColumn QuantityCol = new TableColumn("Quantity");
EditableTableSupport.createEditingColumn(QuantityCol,"quantity");
TableColumn UnitPriceColumn = new TableColumn<>("Unit Price");
EditableTableSupport.createEditingColumn(UnitPriceColumn,"unitPrice");
TableColumn DiscountColumn = new TableColumn<>("Discount");
EditableTableSupport.createEditingColumn(DiscountColumn,"discount");
SubTotalColumn = new TableColumn<>("SubTotal");
EditableTableSupport.createColumn(SubTotalColumn,"subTotal");
TableColumn SubTotalColumn = new TableColumn<>("SubTotal");
EditableTableSupport.createColumn(SubTotalColumn,"subTotal");
DescriptionCol.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<DUMMY_PurchaseOrderLine, String>>() {
#Override
public void handle(TableColumn.CellEditEvent<DUMMY_PurchaseOrderLine, String> t) {
((DUMMY_PurchaseOrderLine) t.getTableView().getItems().get(t.getTablePosition().getRow())).setDescription(t.getNewValue());
}
});
QuantityCol.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<DUMMY_PurchaseOrderLine, String>>() {
#Override
public void handle(TableColumn.CellEditEvent<DUMMY_PurchaseOrderLine, String> t) {
((DUMMY_PurchaseOrderLine) t.getTableView().getItems().get(t.getTablePosition().getRow())).setQuantity(t.getNewValue());
}
});
UnitPriceColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<DUMMY_PurchaseOrderLine, String>>() {
#Override
public void handle(TableColumn.CellEditEvent<DUMMY_PurchaseOrderLine, String> t) {
((DUMMY_PurchaseOrderLine) t.getTableView().getItems().get(t.getTablePosition().getRow())).setUnitPrice(t.getNewValue());
}
});
DiscountColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<DUMMY_PurchaseOrderLine, String>>() {
#Override
public void handle(TableColumn.CellEditEvent<DUMMY_PurchaseOrderLine, String> t) {
((DUMMY_PurchaseOrderLine) t.getTableView().getItems().get(t.getTablePosition().getRow())).setDiscount(t.getNewValue());
}
});
public class EditableTableSupport {
public static void createEditingColumn(TableColumn Column ,String name){
Callback<TableColumn, TableCell> cellFactory = new Callback<TableColumn, TableCell>() {
#Override
public TableCell call(TableColumn p) {
return new EditingCell();
}
};
Column.setSortable(false);
Column.setCellValueFactory(new PropertyValueFactory<DUMMY_PurchaseOrderLine, String>(name));
Column.setCellFactory(cellFactory);
}
public static void createColumn(TableColumn Column, String name) {
Column.setSortable(false);
Column.setCellValueFactory(new PropertyValueFactory<DUMMY_PurchaseOrderLine, String>(name));
}}
Question:How to Update Subtotal Column When i updating Quantity Column or UnitPrice Column
Thank you..
public class DUMMY_PurchaseOrderLine {
private String name;
private String description;
private BigDecimal quantity;
private BigDecimal unitPrice;
private BigDecimal discount;
private BigDecimal subTotal;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public BigDecimal getQuantity() {
return quantity;
}
public void setQuantity(BigDecimal quantity) {
this.quantity = quantity;
}
public BigDecimal getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(BigDecimal unitPrice) {
this.unitPrice = unitPrice;
}
public BigDecimal getDiscount() {
return discount;
}
public void setDiscount(BigDecimal discount) {
this.discount = discount;
}
public BigDecimal getSubTotal() {
return subTotal;
}
public void setSubTotal(BigDecimal subTotal) {
this.subTotal = subTotal;
}
public DUMMY_PurchaseOrderLine(String name, BigDecimal description, BigDecimal quantity,BigDecimal unitPrice,BigDecimal discount,BigDecimal subTotal) {
this.name = name;
this.description = description;
this.quantity = quantity;
this.unitPrice = unitPrice;
this.discount = discount;
this.subTotal = quantity.multiply(unitPrice).subtract(discount);
}
}
In your DUMMY_PurchaseOrderLine class create a read only property named subTotal and initialize it in the constructor via binding. The combination of the binding and the PropertyValueFactory you use to set the value for the SubTotalColumn will ensure that the correct subtotal is always reflected.
class DUMMY_PurchaseOrderLine {
private IntegerProperty quantity = new SimpleIntegerProperty(0);
private DoubleProperty unitPrice = new SimpleDoubleProperty(0);
private DoubleProperty discount = new SimpleDoubleProperty(0);
private ReadOnlyDoubleWrapper subTotal = new ReadOnlyDoubleWrapper(0);
DUMMY_PurchaseOrderLine() {
subTotal.bind(quantity.multiply(unitPrice).subtract(discount));
}
IntegerProperty quantityProperty() { return quantity; }
IntegerProperty unitPriceProperty() { return unitPrice; }
IntegerProperty discountProperty() { return discount; }
ReadOnlyDoubleProperty subTotalProperty() { return subTotal.getReadOnlyProperty(); }
}
Note the naming conventions used. Using the correct naming convention is key.
I'm assuming here that the subtotal is just the calculated value for a single row (specifically by quantity * unitPrice - discount), not a total of values calculated across multiple rows (which would be quite a difficult problem to solve with a TableView).
Update based on question edit
I see from your update that you are using BigDecimal and JavaFX doesn't have a corresponding BigDecimalProperty, so either you will need to create one (not trivial if you want it to be fully featured) or use one of the existing property types.
Your alternate to using properties is to use the low level binding api to calculate subtotals, but I'd advise using properties if you can.

JavaFX Row Not Updating

There is a very similar question to this already, but mine's a bit different. I am using properties and on observable list to change it, it won't update.
Original question is here.
So when I am transferring rows between tables, like this:
The first row would appear, but when adding more than one would cause the ones after the first row to not update, like this:
They only reappear when I move around the columns though.
//Loot identification
TableColumn lootIdentCol = new TableColumn<>("Identification");
TableColumn<ItemDef, Integer> lootIDCol = new TableColumn<>("ID");
lootIDCol.setCellValueFactory(
new PropertyValueFactory<ItemDef, Integer>("id"));
TableColumn<ItemDef, String> lootNameCol = new TableColumn<>("Name");
lootNameCol.setCellValueFactory(
new PropertyValueFactory<ItemDef, String>("name"));
lootIdentCol.getColumns().addAll(lootNameCol, lootIDCol);
//Loot price
TableColumn<ItemDef, Integer> lootPriceCol = new TableColumn<>("Price");
lootPriceCol.setCellValueFactory(
new PropertyValueFactory<ItemDef, Integer>("price"));
//To loot items table
toLootItemsTableView.getColumns().addAll(lootIdentCol, lootPriceCol);
grid.add(toLootItemsTableView, 0, 1);
//Lootable items table
lootableItemsTableView.getColumns().addAll(lootIdentCol, lootPriceCol);
grid.add(lootableItemsTableView, 2, 1);
toLootItemsTableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
lootableItemsTableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
lootableItemsTableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
toLootItemsTableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
lootableTableList.add(new ItemDef("Ab", 141, false, false));
lootableTableList.add(new ItemDef("Ac", 25, false, false));
lootableTableList.add(new ItemDef("AD", 262, false, false));
AddRemoveButtons<ItemDef> addRemoveLootButtons = new AddRemoveButtons<>(
lootableTableList, lootableItemsTableView.getSelectionModel(),
toLootTableList, toLootItemsTableView.getSelectionModel()
);
Code for AddRemoveButtons:
private final ObservableList<E> fromList;
private final ObservableList<E> toList;
public AddRemoveButtons(final ObservableList<E> fromList, final SelectionModel<E> from,
final ObservableList<E> toList, final SelectionModel<E> to) {
this.fromList = fromList;
this.toList = toList;
setAlignment(Pos.CENTER);
setPadding(new Insets(5, 5, 5, 5));
setSpacing(15);
ObservableList<Node> children = getChildren();
Button moveInto = new Button("Add");
moveInto.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
if (from instanceof MultipleSelectionModel) {
MultipleSelectionModel<E> multipleFrom = (MultipleSelectionModel<E>) from;
ObservableList<Integer> selectedIndices = multipleFrom.getSelectedIndices();
for (int i : selectedIndices)
transfer(i, true);
} else
transfer(from.getSelectedIndex(), true);
}
});
Button delete = new Button("Del");
delete.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
if (to instanceof MultipleSelectionModel) {
MultipleSelectionModel<E> multipleFrom = (MultipleSelectionModel<E>) to;
ObservableList<Integer> selectedIndices = multipleFrom.getSelectedIndices();
for (int i : selectedIndices)
transfer(i, false);
} else
transfer(to.getSelectedIndex(), false);
}
});
children.addAll(moveInto, delete);
}
private void transfer(int index, boolean forward) {
if (forward)
toList.add(fromList.remove(index));
else
fromList.add(toList.remove(index));
}
ItemDef which implements Identifiable, Serializable, Comparable:
private final String name;
private final int id;
private final boolean members;
private final boolean stackable;
private int price;
public ItemDef(JSONObject jsonObject) {
this(
(String) jsonObject.get("name"),
Integer.parseInt((String) jsonObject.get("id")),
Boolean.parseBoolean((String) jsonObject.get("members")),
Boolean.parseBoolean((String) jsonObject.get("stackable"))
);
}
public ItemDef(String name, int id, boolean members, boolean stackable) {
this.name = name;
this.id = id;
this.members = members;
this.stackable = stackable;
price = -1;
}
public String getName() {
return name;
}
#Override
public int getId() {
return id;
}
public boolean isMembers() {
return members;
}
public boolean isStackable() {
return stackable;
}
public int getPrice() {
return price != -1 ? price : updatePrice();
}
//Other methods not relevant
Figured out why it kept doing that.
You just can't have the same TableColumn being referenced on multiple tables.
You should not share columns in multiple tables if you want data to update in multiple tables share the data set between them not the columns.

Resources