Results 1 to 5 of 5
  1. #1
    caraldur is offline Member
    Join Date
    Feb 2015
    Posts
    27
    Rep Power
    0

    Default Tableview change column text color

    Hello,

    I am trying to change the text color for a column based a value from the observable list that i use to fill the tableview. Currently, I using a default value to change the color. Below is the code I am using to get it to work.
    Java FX Code:
    // Custom rendering of the table cell.
    		productQty.setCellFactory(column ->	{
    			return new TableCell<Product, Number>() {
    						@Override
    						protected void updateItem(Number item, boolean empty) {
    							super.updateItem(item, empty);
    							
    													
    							if (empty || item == null) {
    								setText(null);
    								setStyle("");
    							}else
    								{
    									if (item.intValue() < 1000) 
    								{
    									int value = item.intValue();
    									setText(item.toString());
    									this.getStyleClass().add("minStock");
    								}else {
    									setText(item.toString());
    								}
    							}
    						}
    					};
    		});
    On line 14 I have a value of 1000, but I want that to be based on the minimum stock for a given product. This information is in the observablelist but I can't figure out how to get the appropriate value for a given product. I'm thinking I have to take the value from the productID column and loop through the observablelist until I find the matching row and then save the minimum stock integer to a variable which I can then use in my If statement. Not sure how to accomplish this at the moment.

    Hopefully, I have explained my problem well enough that someone can point me in the right direction.



    Thanks in advance.
    -Fred

  2. #2
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Tableview change column text color

    You have to override the CellFactory and write some code for conditional formatting.

    Here is a nice example that should be relevant
    Last edited by benji2505; 05-27-2017 at 02:26 AM.

  3. #3
    caraldur is offline Member
    Join Date
    Feb 2015
    Posts
    27
    Rep Power
    0

    Default Re: Tableview change column text color

    benji2505
    Re: Tableview change column text color

    You have to override the CellFactory and write some code for conditional formatting.

    Here is a nice example that should be relevant
    I used that example to figure out the setting the color based on a default value, but I was stuck on how to have the min value change based on the the string in the first column of the same row. I did figure it out this morning. Below is the my fix:
    Java FX Code:
    		productQty.setCellFactory(column ->	{
    			return new TableCell<Product, Number>() {
    						@Override
    						protected void updateItem(Number item, boolean empty) {
    							String i = productIDColumn.getCellData(getIndex());//int value = item.intValue();
    
    							super.updateItem(item, empty);
    							int minStock = 1000;
    							for(Product a: curPartInventory){
    								if(i!= null)
    								{
    									if(i.equals(a.getProductId()))
    									{
    										minStock = a.getMinQty();
    										log.info(i +" : "+a.getMinQty());
    									}
    								}
    							}
    							
    							if (empty || item == null) {
    								setText(null);
    								setStyle("");
    							}else
    								{
    									if (item.intValue() < minStock) 
    								{
    									
    									setText(item.toString());
    									this.getStyleClass().add("minStock");
    								}else {
    									setText(item.toString());
    								}
    							}
    						}
    					};
    		});

  4. #4
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Tableview change column text color

    nice solution. BTW, I would be cautious with displaying numbers that have nothing to do with the real stock. If this display prevents somebody from re-ordering or re-stocking, it will be the IT's fault. So, make sure everybody knows about this.

  5. #5
    caraldur is offline Member
    Join Date
    Feb 2015
    Posts
    27
    Rep Power
    0

    Default Re: Tableview change column text color

    I agree 100%. I'm only displaying the current inventory and using the min stock number for the if statement to set the text fill color.

Similar Threads

  1. Show Name by Id column tableview
    By maurikius in forum JavaFX
    Replies: 9
    Last Post: 10-02-2016, 11:29 PM
  2. Change JProgressBar Text Color
    By nando88 in forum AWT / Swing
    Replies: 10
    Last Post: 09-05-2013, 04:07 PM
  3. “Extra Column” in TableView
    By pugazhendhiobs in forum JavaFX
    Replies: 2
    Last Post: 03-24-2012, 07:22 PM
  4. Change text color on type
    By corbokhan in forum New To Java
    Replies: 2
    Last Post: 11-01-2010, 07:30 PM
  5. change syntax text color
    By dejos456 in forum New To Java
    Replies: 5
    Last Post: 12-14-2009, 11:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •