JavaFX:如何更改行颜色?
需要更改tableview控件中的行背景色 但是,如果我按行工厂更改它(使用setStyle),它也会更改选定行的颜色背景行颜色 怎么把它分开 我的代码片段如下:JavaFX:如何更改行颜色?,java,javafx,Java,Javafx,需要更改tableview控件中的行背景色 但是,如果我按行工厂更改它(使用setStyle),它也会更改选定行的颜色背景行颜色 怎么把它分开 我的代码片段如下: package javafxapplication1; import javafx.application.Application; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleObjectPrope
package javafxapplication1;
import javafx.application.Application;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.Tooltip;
import javafx.scene.control.TooltipBuilder;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.CircleBuilder;
import javafx.stage.Stage;
import javafx.util.Callback;
public class RowFactoryAndOptimisationDemo extends Application {
StackPane root;
@Override
public void start(Stage stage) throws Exception {
root = new StackPane();
root.autosize();
Scene scene = new Scene(root, Color.LINEN);
stage.setTitle("Row Factory Demo");
stage.setWidth(500);
stage.setHeight(300);
stage.setScene(scene);
stage.show();
configureTable();
}
public static void main(String[] args) {
Application.launch(args);
}
static int i = 0;
static int k = 0;
static int j = 0;
@SuppressWarnings("unchecked")
private void configureTable() {
final ObservableList<RFDDomain> data = FXCollections.observableArrayList();
int id =1;
for (int i = 1; i <= 10000; i++) {
data.add(new RFDDomain(id++,"First Row", "This is for check.", 1));
data.add(new RFDDomain(id++,"Second Row", null, 2));
data.add(new RFDDomain(id++,"Third Row", "This is for check.", 3));
data.add(new RFDDomain(id++,"Fourth Row", "dil", 4));
}
TableView<RFDDomain> tableView = new TableView<RFDDomain>();
tableView.getStyleClass().add("myTable");
tableView.setItems(data);
tableView.setRowFactory(new Callback<TableView<RFDDomain>, TableRow<RFDDomain>>() {
@Override
public TableRow<RFDDomain> call(TableView<RFDDomain> paramP) {
return new TableRow<RFDDomain>() {
@Override
protected void updateItem(RFDDomain paramT, boolean paramBoolean) {
String style = "-fx-background-color: linear-gradient(#007F0E 0%, #FFFFFF 90%, #eaeaea 90%);";
setStyle(style);
super.updateItem(paramT, paramBoolean);
}
};
}
});
TableColumn<RFDDomain, Integer> column0 = new TableColumn<RFDDomain, Integer>("Id");
column0.setCellValueFactory(new PropertyValueFactory<RFDDomain, Integer>("id"));
TableColumn<RFDDomain, String> column1 = new TableColumn<RFDDomain, String>("Title");
column1.setCellValueFactory(new PropertyValueFactory<RFDDomain, String>("name"));
TableColumn<RFDDomain, String> column2 = new TableColumn<RFDDomain, String>("Description");
column2.setCellValueFactory(new PropertyValueFactory<RFDDomain, String>("description"));
TableColumn<RFDDomain, Number> column3 = new TableColumn<RFDDomain, Number>("Status");
column3.setPrefWidth(55);
column3.setCellValueFactory(new PropertyValueFactory<RFDDomain, Number>("status"));
TableColumn<RFDDomain, String> column4 = new TableColumn<RFDDomain, String>("Action");
column4.setCellValueFactory(new PropertyValueFactory<RFDDomain, String>("name"));
tableView.getColumns().addAll(column0, column1, column2, column3, column4);
this.root.getChildren().add(tableView);
}
/**
* Domain Model for this demo.
*/
public class RFDDomain {
private SimpleIntegerProperty id = new SimpleIntegerProperty();
private SimpleStringProperty name = new SimpleStringProperty();
private SimpleStringProperty description = new SimpleStringProperty();
private SimpleIntegerProperty status = new SimpleIntegerProperty();
public RFDDomain(int id,String name, String desc, int status) {
this.id.set(id);
this.name.set(name);
this.description.set(desc);
this.status.set(status);
}
public int getId() {
return id.get();
}
public SimpleIntegerProperty idProperty() {
return id;
}
public String getDescription() {
return description.get();
}
public SimpleStringProperty descriptionProperty() {
return description;
}
public String getName() {
return name.get();
}
public SimpleStringProperty nameProperty() {
return name;
}
public int getStatus() {
return status.get();
}
public SimpleIntegerProperty statusProperty() {
return status;
}
}
}
PackageJavaFXApplication1;
导入javafx.application.application;
导入javafx.beans.property.SimpleIntegerProperty;
导入javafx.beans.property.SimpleObject属性;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.beans.value.observeValue;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.geometry.Pos;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.ContextMenu;
导入javafx.scene.control.MenuItem;
导入javafx.scene.control.TableCell;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableColumn.CellDataFeatures;
导入javafx.scene.control.TableRow;
导入javafx.scene.control.TableView;
导入javafx.scene.control.Tooltip;
导入javafx.scene.control.TooltipBuilder;
导入javafx.scene.control.cell.PropertyValueFactory;
导入javafx.scene.input.MouseButton;
导入javafx.scene.input.MouseEvent;
导入javafx.scene.layout.StackPane;
导入javafx.scene.paint.Color;
导入javafx.scene.shape.Circle;
导入javafx.scene.shape.CircleBuilder;
导入javafx.stage.stage;
导入javafx.util.Callback;
公共类RowFactoryandOptimizationDemo扩展了应用程序{
根;
@凌驾
public void start(Stage)引发异常{
root=newstackpane();
root.autosize();
场景=新场景(根、颜色、亚麻布);
stage.setTitle(“世界其他地区工厂演示”);
舞台设置宽度(500);
舞台设置高度(300);
舞台场景;
stage.show();
configureTable();
}
公共静态void main(字符串[]args){
应用程序启动(args);
}
静态int i=0;
静态int k=0;
静态int j=0;
@抑制警告(“未选中”)
私有void configureTable(){
最终ObservableList数据=FXCollections.observableArrayList();
int-id=1;
对于(int i=1;i而不是-fx背景色
,偶数行使用-fx控件内部背景
,奇数行使用-fx控件内部背景alt
:
String style = "-fx-control-inner-background: linear-gradient(#007F0E 0%, #FFFFFF 90%, #eaeaea 90%);"
+ "-fx-control-inner-background-alt: linear-gradient(#007F0E 0%, #FFFFFF 90%, #eaeaea 90%);";
另外,在外部css文件中执行此操作要干净得多:
application.css:
.table-view .table-row-cell {
-fx-control-inner-background: linear-gradient(#007F0E 0%, #FFFFFF 90%, #eaeaea 90%);
-fx-control-inner-background-alt: linear-gradient(#007F0E 0%, #FFFFFF 90%, #eaeaea 90%);
}
然后
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
现在您可以完全摆脱行工厂。使用JDK 8u60 EA,这会导致许多异常,如:警告:捕获的'java.lang.ClassCastException:javafx.scene.paint.LinearGradient无法转换为javafx.scene.paint.Color',同时转换样式表jar中规则'*.table row cell'中'-fx text fill'的值:file:/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/jre/lib/ext/jfxrt.jar!/com/sun/javafx/scene/control/skin/modena/modena.bssAh,是的:的确如此。(尽管如此,它似乎仍然有效。)我很懒,只是用固定的颜色而不是渐变来测试。对于渐变,除了为:selected:focused
、:selected
、和:focused
重新创建值之外,可能没有其他方法可以做到这一点,这有点乏味。让我想想。。。