2015-07-05 15 views
1

晚上好, 我想知道,如何将背景颜色改为18岁以下的红色,是可以的? 自从星期一起我试图解决这个问题。有人能给我一些网站,而不是比oracle文档解释得更好吗? 我看到很多人,仍然在使用swing,我应该继续学习javafx还是开始学习swing?对不起,我的英语不好。我如何改变javafx中特定celldata的风格?

控制器

package tableview; 

    import java.net.URL; 
    import java.util.ResourceBundle; 
    import javafx.collections.FXCollections; 
    import javafx.collections.ObservableList; 
    import javafx.fxml.FXML; 
    import javafx.fxml.Initializable; 
    import javafx.scene.control.TableColumn; 
    import javafx.scene.control.TableView; 
    import javafx.scene.layout.AnchorPane; 

    public class LayoutController implements Initializable { 

     @Override 
     public void initialize(URL url, ResourceBundle rb) { 
      getPerson(); 
      columnFName.setCellValueFactory(celldata -> celldata.getValue().getfName()); 
      columnLName.setCellValueFactory(celldata -> celldata.getValue().getlName()); 
      columnAge.setCellValueFactory(celldata -> celldata.getValue().getAge()); 
      tableView.setItems(person); 
     }  

     @FXML 
     private AnchorPane layout; 

     //TABLE 
     @FXML 
     private TableView<Person> tableView; 
     @FXML 
     private TableColumn<Person, String> columnLName; 
     @FXML 
     private TableColumn<Person, String> columnFName; 
     @FXML 
     private TableColumn<Person, Number> columnAge; 
     //END 

     ObservableList person = FXCollections.observableArrayList(); 
     ObservableList getPerson() { 
      person.add(new Person("John", "Smith", 15)); 
      person.add(new Person("May", "Smith", 18)); 
      person.add(new Person("Sam", "Lucca", 21)); 
      person.add(new Person("Homer", "Simpson", 14)); 
      return person; 
     } 


    } 

Person类

package tableview; 

import javafx.beans.property.SimpleIntegerProperty; 
import javafx.beans.property.SimpleStringProperty; 

public class Person { 
    private SimpleStringProperty fName, lName; 
    private SimpleIntegerProperty age; 

    public Person() { 
     this("", "", 0); 
    } 

    public Person(String fName, String lName, int age) { 
     this.fName = new SimpleStringProperty(fName); 
     this.lName = new SimpleStringProperty(lName); 
     this.age = new SimpleIntegerProperty(age); 
    } 

    public SimpleStringProperty getfName() { 
     return fName; 
    } 

    public SimpleStringProperty getlName() { 
     return lName; 
    } 

    public SimpleIntegerProperty getAge() { 
     return age; 
    } 
} 

回答

0

设置你的表中的一行工厂。你想观察该行的itemProperty。管理背景颜色的最佳方法是使用外部CSS文件,并设置一个CSS伪类如果由该行所代表的人有<年龄18(你可以把你的控制器的initialize()方法验证码。)

PseudoClass minorPseudoClass = PseudoClass.getPseudoClass("minor"); 

    tableView.setRowFactory(tv -> { 
     TableRow<Person> row = new TableRow<>(); 
     row.itemProperty().addListener((obs, oldPerson, newPerson) -> { 
      if (newPerson != null) { 
       row.pseudoClassStateChanged(minorPseudoClass, newPerson.getAge() < 18); 
      } else { 
       row.pseudoClassStateChanged(minorPseudoClass, false); 
      } 
     }); 
     return row ; 
    }); 

然后为您创建的伪类合适的样式定义外部样式表:

.table-row-cell:minor { 
    -fx-control-inner-background: red ; 
    -fx-control-inner-background-alt: #cc0000 ; 
} 

注意,这个假设的年龄是固定的,每个人在表中。如果有在显示人变更的可能性,需要随着年龄属性作为该行内容更改注册和注销听众:

tableView.setRowFactory(tv -> { 
     TableRow<Person> row = new TableRow<>(); 
     ChangeListener<Number> ageListener = (obs, oldValue, newValue) -> { 
      row.pseudoClassStateChanged(minorPseudoClass, newValue.intValue() < 18); 
     }; 
     row.itemProperty().addListener((obs, oldPerson, newPerson) -> { 
      if (oldPerson != null) { 
       oldPerson.ageProperty().removeListener(ageListener); 
      } 
      if (newPerson != null) { 
       newPerson.ageProperty().addListener(ageListener); 
       row.pseudoClassStateChanged(minorPseudoClass, newPerson.getAge() < 18); 
      } else { 
       row.pseudoClassStateChanged(minorPseudoClass, false); 
      } 
     }); 
     return row ; 
    }); 

这里是一个SSCCE。这不使用FXML,但显然你可以做同样的事情,在控制器的initialize()方法中创建rowFactory

import java.util.function.Function; 

import javafx.application.Application; 
import javafx.beans.property.IntegerProperty; 
import javafx.beans.property.SimpleIntegerProperty; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.property.StringProperty; 
import javafx.beans.value.ChangeListener; 
import javafx.beans.value.ObservableValue; 
import javafx.css.PseudoClass; 
import javafx.scene.Scene; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableRow; 
import javafx.scene.control.TableView; 
import javafx.scene.layout.BorderPane; 
import javafx.stage.Stage; 

public class HighlightYoungPeopleTableExample extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     TableView<Person> tableView = new TableView<>(); 
     TableColumn<Person, String> firstNameColumn = column("First Name", Person::firstNameProperty, 150); 
     TableColumn<Person, String> lastNameColumn = column("Last Name", Person::lastNameProperty, 150); 
     TableColumn<Person, Integer> ageColumn = column("Age", person -> person.ageProperty().asObject(), 50); 

     PseudoClass minorPseudoClass = PseudoClass.getPseudoClass("minor"); 

     tableView.setRowFactory(tv -> { 
      TableRow<Person> row = new TableRow<>(); 
      ChangeListener<Number> ageListener = (obs, oldValue, newValue) -> { 
       row.pseudoClassStateChanged(minorPseudoClass, newValue.intValue() < 18); 
      }; 
      row.itemProperty().addListener((obs, oldPerson, newPerson) -> { 
       if (oldPerson != null) { 
        oldPerson.ageProperty().removeListener(ageListener); 
       } 
       if (newPerson != null) { 
        newPerson.ageProperty().addListener(ageListener); 
        row.pseudoClassStateChanged(minorPseudoClass, newPerson.getAge() < 18); 
       } else { 
        row.pseudoClassStateChanged(minorPseudoClass, false); 
       } 
      }); 
      return row ; 
     }); 

     tableView.getColumns().add(firstNameColumn); 
     tableView.getColumns().add(lastNameColumn); 
     tableView.getColumns().add(ageColumn); 

     tableView.getItems().addAll(
       new Person("John", "Smith", 15), 
       new Person("May", "Smith", 18), 
       new Person("Sam", "Lucca", 21), 
       new Person("Homer", "Simpson", 14)   
     ); 

     Scene scene = new Scene(new BorderPane(tableView), 800, 600); 
     scene.getStylesheets().add("highlight-young-people-table.css"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    private <S,T> TableColumn<S,T> column(String title, Function<S, ObservableValue<T>> property, double width) { 
     TableColumn<S,T> col = new TableColumn<>(title); 
     col.setPrefWidth(width); 
     col.setCellValueFactory(cellData -> property.apply(cellData.getValue())); 
     return col ; 
    } 

    public static class Person { 
     private final StringProperty firstName = new SimpleStringProperty() ; 
     private final StringProperty lastName = new SimpleStringProperty() ; 
     private final IntegerProperty age = new SimpleIntegerProperty(); 

     public Person(String firstName, String lastName, int age) { 
      setFirstName(firstName); 
      setLastName(lastName); 
      setAge(age); 
     } 

     public final StringProperty firstNameProperty() { 
      return this.firstName; 
     } 

     public final String getFirstName() { 
      return this.firstNameProperty().get(); 
     } 

     public final void setFirstName(final String firstName) { 
      this.firstNameProperty().set(firstName); 
     } 

     public final StringProperty lastNameProperty() { 
      return this.lastName; 
     } 

     public final String getLastName() { 
      return this.lastNameProperty().get(); 
     } 

     public final void setLastName(final String lastName) { 
      this.lastNameProperty().set(lastName); 
     } 

     public final IntegerProperty ageProperty() { 
      return this.age; 
     } 

     public final int getAge() { 
      return this.ageProperty().get(); 
     } 

     public final void setAge(final int age) { 
      this.ageProperty().set(age); 
     } 


    } 

    public static void main(String[] args) { 
     launch(args); 
    } 


} 

亮点,年轻人table.css

.table-row-cell:minor { 
    -fx-control-inner-background: red ; 
    -fx-control-inner-background-alt: #cc0000 ; 
} 
+0

我改变newPerson.getAge()<18 newPerson.getAge()的intValue()<18和工作正常,但可能我改变了单元格而不是所有的行? – Pedro

+0

改为在'TableColumn'上使用'cellFactory'。 –