java fx 给按钮加颜色,如何改变单条java fx的颜色

Here is my code which generates bar chart of 10 values from 0 to 10 . i want to change the color of bars as follows

if i>5 color==red

if i>8 color==blue

so the final out will be 0-5(default yellow bars) 6-8(Red bars) 9(blue bar)

kindly help me..

thanks

public class BarChartSample extends Application {

@Override public void start(Stage stage) {

stage.setTitle("Bar Chart Sample");

final CategoryAxis xAxis = new CategoryAxis();

final NumberAxis yAxis = new NumberAxis();

final BarChart bc =

new BarChart(xAxis,yAxis);

bc.setTitle("Country Summary");

xAxis.setLabel("bars");

yAxis.setLabel("Value");

XYChart.Series series1 = new XYChart.Series();

series1.setName("...");

for(int i=0;i<10;i++)

{

//here i want to change color of bar if value of i is >5 than red if i>8 than blue

series1.getData().add(new XYChart.Data("Value", i));

}

}

public static void main(String[] args) {

launch(args);

}

}

解决方案

The solution works by setting the bar's -fx-bar-fill color to a different color based on the value of the bar's data.

final XYChart.Data data = new XYChart.Data("Value " + i , i);

data.nodeProperty().addListener(new ChangeListener() {

@Override public void changed(ObservableValue extends Node> ov, Node oldNode, Node newNode) {

if (newNode != null) {

if (data.getYValue().intValue() > 8 ) {

newNode.setStyle("-fx-bar-fill: navy;");

} else if (data.getYValue().intValue() > 5 ) {

newNode.setStyle("-fx-bar-fill: firebrick;");

}

}

}

});

V9r6m.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值