首页 > 解决方案 > 如何显示/隐藏 JavaFX 列中的千位逗号?

问题描述

我在 JavaFX 中有一张桌子。我想控制千个逗号的显示/隐藏。目前,我可以通过 控制颜色column1.setStyle("-fx-text-fill: green"),但我如何显示千个逗号(然后在稍后阶段将它们隐藏起来),可能是通过类似的方法?


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

  @Override
  public void start(Stage primaryStage) {

    TableView tableView = new TableView();

    TableColumn<Integer, Person> column1 = new TableColumn<>("Salary");
    column1.setCellValueFactory(new PropertyValueFactory("salary"));

    tableView.getColumns().add(column1);
    tableView.getItems().add(new Person(27000));
    tableView.getItems().add(new Person(48000));
    column1.setStyle("-fx-text-fill: green");   

    VBox vbox = new VBox(tableView);
    Scene scene = new Scene(vbox);
    primaryStage.setScene(scene);
    primaryStage.show();
  }

}

人:

import javafx.beans.property.SimpleIntegerProperty;

public class Person {

    private SimpleIntegerProperty salaryProperty;

    public Person() {
    }

    public Person(int salary) {
        this.salaryProperty = new SimpleIntegerProperty(salary);

    }

    public int getSalary() {
        return salaryProperty.get();
    }

    public void setSalary(int salary) {
        this.salaryProperty = new SimpleIntegerProperty(salary);
    }
}

标签: javacssjavafxformattingformat

解决方案


I think that it is not possible to do with css. You need to use some kind of NumberFormat like in this example:

App:

package formatter;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.text.NumberFormat;
import java.util.Locale;


public class App extends Application {

    @Override
    public void start(Stage primaryStage) {

        // Create the table view and its column (like you already did):
        TableView<Person> tableView = new TableView<>();
        TableColumn<Person, Integer> salaryColumn = new TableColumn<>("Salary");
        salaryColumn.setCellValueFactory(new PropertyValueFactory<>("salary"));
        tableView.getColumns().add(salaryColumn);

        // Using a check box in this example to change between formats::
        CheckBox useGroupingCheckBox = new CheckBox("use grouping");

        // Create a currency formatter with a locale which is important for internationalization:
        NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.CANADA);
        formatter.setMaximumFractionDigits(0);

        // Create a custom cell:
        salaryColumn.setCellFactory(column -> new TableCell<>() {
            @Override
            protected void updateItem(Integer item, boolean empty) {
                super.updateItem(item, empty);
                if (item == null || empty) {
                    setText("");
                } else {
                    // Use grouping when check box selected, don't when not selected:
                    formatter.setGroupingUsed(useGroupingCheckBox.isSelected());
                    setText(formatter.format(item));
                }
            }
        });

        // Refresh table on check box action:
        useGroupingCheckBox.setOnAction(event -> tableView.refresh());

        // Add some test data:
        tableView.getItems().add(new Person(27000));
        tableView.getItems().add(new Person(48000));

        // Prepare scene and stage:
        VBox vbox = new VBox(useGroupingCheckBox, tableView);
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

Person class:

package formatter;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;

public class Person {

    private IntegerProperty salary;

    public Person() {
        salary = new SimpleIntegerProperty();
    }

    public Person(int salary) {
        this();
        this.salary.set(salary);
    }

    public Integer getSalary() {
        return salary.get();
    }

    public IntegerProperty salaryProperty() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary.set(salary);
    }
}

Preview:

enter image description here


推荐阅读