在JavaFX 2中,使用CSS,是否可以使用2种颜色创建背景?想想如何一个TableCell
,高度为10像素。我想第一个2像素(垂直)是红色的,其余8像素(垂直)应该保持默认的背景颜色。这可能在JavaFX 2中使用CSS吗?怎么样?在JavaFX中有两种颜色的背景?
实施例:
原始背景:
期望的结果:
(上2个像素由红色取代)
感谢您的任何暗示在这!
在JavaFX 2中,使用CSS,是否可以使用2种颜色创建背景?想想如何一个TableCell
,高度为10像素。我想第一个2像素(垂直)是红色的,其余8像素(垂直)应该保持默认的背景颜色。这可能在JavaFX 2中使用CSS吗?怎么样?在JavaFX中有两种颜色的背景?
实施例:
原始背景:
期望的结果:
(上2个像素由红色取代)
感谢您的任何暗示在这!
看,如何理解CSSRef:
http://docs.oracle.com/javafx/2/api/javafx/scene/doc-files/cssref.html
看
-fx背景图像:
URI [,URI] *
一系列用逗号分隔的图像URI。
看
-fx-背景重复
重复式[,重复式] *
其中重复式=重复-X | repeat-y | [重复|空间|圆|拉伸| no-repeat] {1,2}
一系列用逗号分隔的值。该系列中的每个重复样式项目都适用于背景图像系列中的相应图像。
看: -fx-背景位置
BG-位置[,BG-位置] * 其中= [ [[大小|离开|中心|右] [大小|顶部|中心|底部]? ] | [[center | [left |合适的尺码? ] || [center | [top |底部]大小? ] ]
一系列用逗号分隔的值。系列中的每个bg位置项目都适用于背景图像系列中的相应图像。
所以,你可以看到:你应该描述2个图像,(每个2x2像素 - 一个红色和一个灰色) 两个bg位置,以及两个重复样式对应每个。
怎么样?
例如:
{
-fx-backdround-image : "path_to_red", "path_to_grey";
-fx-background-repeat : repeat-x, stretch;
-fx-background-position : 0px 0px, 0px 2px;
}
我不放弃对代码workness一个garantee,但这个想法似乎是正确的。
当使用插图时,可能仅使用颜色而不使用图像。从原始JavaFX的CSS实施例:
.table-row-cell:odd {
-fx-background-color: -fx-table-cell-border-color, derive(-fx-control-inner-background,-5%);
-fx-background-insets: 0, 0 0 1 0;
}
[6个字符...]
我使用的背景颜色的简单层,以产生红色高亮(类似的Stefan”建议的解决方案)。
/**
* file: table.css
* Place in same directory as TableViewPropertyEditorWithCSS.java.
* Have your build system copy this file to your build output directory.
**/
.highlighted-cell {
-fx-text-fill: -fx-text-inner-color;
-fx-background-color: firebrick, gainsboro;
-fx-background-insets: 0, 2 0 0 0;
}
对于像stackpane标准的区域,你真正需要做的是运用上述CSS(在-fx-text-fill
以下),以获得期望的结果。
下面是使用的梯度来定义颜色另一个棘手的方式:
-fx-background-color:
linear-gradient(
from 0px 0px to 0px 2px,
firebrick, firebrick 99%,
gainsboro
);
在下面的屏幕截图,该值的细胞被突出显示(通过具有施加给它们的highlighted-cell
css类)如果具有值false
。
高亮细胞样式类开关逻辑:
public void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
....
getStyleClass().remove("highlighted-cell");
} else {
if (getItem() instanceof Boolean && (Boolean.FALSE.equals((Boolean) getItem()))) {
getStyleClass().add("highlighted-cell");
} else {
getStyleClass().remove("highlighted-cell");
}
...
}
}
它看起来不错时(一个updateItem呼叫期间为自定义小区)施加到一个标准的表格单元的highlighted-cell
风格类但确实有一些缺点。桌面着色方案非常微妙和复杂。它具有奇数/偶数值的亮点,选定行的高亮显示,选定的悬停行的高亮显示,聚焦的行和单元格的高亮显示等。此外,它还具有上述所有功能的各种组合。只需在高亮单元类中直接设置背景颜色是一种蛮力的方式来达到你想要的效果,因为它没有考虑到所有这些其他细节,并且只是覆盖它们,所以使用这种方法突出显示的单元格风格总是看起来一样,无论什么临时的CSS应用级状态已经被应用到它。
确实很好,但更好的解决方案会根据伪级状态对突出显示的单元格进行不同的着色。虽然这是一件相当棘手的事情,你可能会浪费大量时间玩弄各种状态和CSS选择器组合,尝试获得不断变化的亮点。总之,对于这个例子来说,尽管它可能适合你,但对我来说这似乎不值得为此付出额外的努力。
测试程序(这个长度和复杂性的道歉,这对我的风格凸显的逻辑集成到现有的方案只是更容易):
import java.lang.reflect.*;
import java.util.logging.*;
import javafx.application.Application;
import javafx.beans.property.*;
import javafx.beans.value.*;
import javafx.collections.*;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.TableColumn.CellEditEvent;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.util.Callback;
// click in the value column (a couple of times) to edit the value in the column.
// property editors are defined only for String and Boolean properties.
// change focus to something else to commit the edit.
public class TableViewPropertyEditorWithCSS extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
final Person aPerson = new Person("Fred", false, false, "Much Ado About Nothing");
final Label currentObjectValue = new Label(aPerson.toString());
TableView<NamedProperty> table = new TableView();
table.setEditable(true);
table.setItems(createNamedProperties(aPerson));
TableColumn<NamedProperty, String> nameCol = new TableColumn("Name");
nameCol.setCellValueFactory(new PropertyValueFactory<NamedProperty, String>("name"));
TableColumn<NamedProperty, Object> valueCol = new TableColumn("Value");
valueCol.setCellValueFactory(new PropertyValueFactory<NamedProperty, Object>("value"));
valueCol.setCellFactory(new Callback<TableColumn<NamedProperty, Object>, TableCell<NamedProperty, Object>>() {
@Override
public TableCell<NamedProperty, Object> call(TableColumn<NamedProperty, Object> param) {
return new EditingCell();
}
});
valueCol.setOnEditCommit(
new EventHandler<CellEditEvent<NamedProperty, Object>>() {
@Override
public void handle(CellEditEvent<NamedProperty, Object> t) {
int row = t.getTablePosition().getRow();
NamedProperty property = (NamedProperty) t.getTableView().getItems().get(row);
property.setValue(t.getNewValue());
currentObjectValue.setText(aPerson.toString());
}
});
table.getColumns().setAll(nameCol, valueCol);
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
VBox layout = new VBox(10);
layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;");
layout.getChildren().setAll(
currentObjectValue,
table);
VBox.setVgrow(table, Priority.ALWAYS);
Scene scene = new Scene(layout, 650, 600);
scene.getStylesheets().add(getClass().getResource("table.css").toExternalForm());
stage.setScene(scene);
stage.show();
}
private ObservableList<NamedProperty> createNamedProperties(Object object) {
ObservableList<NamedProperty> properties = FXCollections.observableArrayList();
for (Method method : object.getClass().getMethods()) {
String name = method.getName();
Class type = method.getReturnType();
if (type.getName().endsWith("Property")) {
try {
properties.add(new NamedProperty(name, (Property) method.invoke(object)));
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
Logger.getLogger(TableViewPropertyEditorWithCSS.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
return properties;
}
public class NamedProperty {
public NamedProperty(String name, Property value) {
nameProperty.set(name);
valueProperty = value;
}
private StringProperty nameProperty = new SimpleStringProperty();
public StringProperty nameProperty() {
return nameProperty;
}
public StringProperty getName() {
return nameProperty;
}
public void setName(String name) {
nameProperty.set(name);
}
private Property valueProperty;
public Property valueProperty() {
return valueProperty;
}
public Object getValue() {
return valueProperty.getValue();
}
public void setValue(Object value) {
valueProperty.setValue(value);
}
}
public class Person {
private final SimpleStringProperty firstName;
private final SimpleBooleanProperty married;
private final SimpleBooleanProperty hasChildren;
private final SimpleStringProperty favoriteMovie;
private Person(String firstName, Boolean isMarried, Boolean hasChildren, String favoriteMovie) {
this.firstName = new SimpleStringProperty(firstName);
this.married = new SimpleBooleanProperty(isMarried);
this.hasChildren = new SimpleBooleanProperty(hasChildren);
this.favoriteMovie = new SimpleStringProperty(favoriteMovie);
}
public SimpleStringProperty firstNameProperty() {
return firstName;
}
public SimpleBooleanProperty marriedProperty() {
return married;
}
public SimpleBooleanProperty hasChildrenProperty() {
return hasChildren;
}
public SimpleStringProperty favoriteMovieProperty() {
return favoriteMovie;
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String fName) {
firstName.set(fName);
}
public Boolean getMarried() {
return married.get();
}
public void setMarried(Boolean isMarried) {
married.set(isMarried);
}
public Boolean getHasChildren() {
return hasChildren.get();
}
public void setHasChildren(Boolean hasChildren) {
this.hasChildren.set(hasChildren);
}
public String getFavoriteMovie() {
return favoriteMovie.get();
}
public void setFavoriteMovie(String movie) {
favoriteMovie.set(movie);
}
@Override
public String toString() {
return firstName.getValue() + ", isMarried? " + married.getValue() + ", hasChildren? " + hasChildren.getValue() + ", favoriteMovie: " + favoriteMovie.get();
}
}
class EditingCell extends TableCell<NamedProperty, Object> {
private TextField textField;
private CheckBox checkBox;
public EditingCell() {
}
@Override
public void startEdit() {
if (!isEmpty()) {
super.startEdit();
if (getItem() instanceof Boolean) {
createCheckBox();
setText(null);
setGraphic(checkBox);
} else {
createTextField();
setText(null);
setGraphic(textField);
textField.selectAll();
}
}
}
@Override
public void cancelEdit() {
super.cancelEdit();
if (getItem() instanceof Boolean) {
setText(getItem().toString());
} else {
setText((String) getItem());
}
setGraphic(null);
}
@Override
public void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
getStyleClass().remove("highlighted-cell");
} else {
if (getItem() instanceof Boolean && (Boolean.FALSE.equals((Boolean) getItem()))) {
getStyleClass().add("highlighted-cell");
} else {
getStyleClass().remove("highlighted-cell");
}
if (isEditing()) {
if (getItem() instanceof Boolean) {
if (checkBox != null) {
checkBox.setSelected(getBoolean());
}
setText(null);
setGraphic(checkBox);
} else {
if (textField != null) {
textField.setText(getString());
}
setText(null);
setGraphic(textField);
}
} else {
setText(getString());
setGraphic(null);
}
}
}
private void createTextField() {
textField = new TextField(getString());
textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if (!newValue) {
commitEdit(textField.getText());
}
}
});
}
private void createCheckBox() {
checkBox = new CheckBox();
checkBox.setSelected(getBoolean());
checkBox.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
checkBox.focusedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if (!newValue) {
commitEdit(checkBox.isSelected());
}
}
});
}
private String getString() {
return getItem() == null ? "" : getItem().toString();
}
private Boolean getBoolean() {
return getItem() == null ? false : (Boolean) getItem();
}
}
}
哇,感谢你在这里投入的大量工作! – 2013-04-25 12:46:08
非常好。谢谢。 – jkaufmann 2013-05-04 00:23:45
啊,我不知道,人们可以直接指定多个图像,但即使图像不需要,如果我没有错:-)感谢提示!还没有测试它,因为它更加小巧(.table-row-cell实际上没有边界,但是用背景来模拟边界的技巧 - 使事情变得更复杂一点),但我也会假设它是有效的。更新将随之而来。 – 2013-04-24 20:38:10
如果你认为,而不是图像,你可以使用背景颜色,但似乎你错了,因为颜色没有大小,它只是一种颜色,但图像的大小,所以它可以重复,等等...否则,你将不得不指定彩色水平尺寸.. – 2013-04-24 20:44:55
亚历山大,它可能仍然可以使用插图,看到你的帖子/答案(我编辑它)。你怎么看待这件事? – 2013-04-24 20:49:24