簡體   English   中英

如何顯示/隱藏 JavaFX 列中的千位逗號?

[英]How to show/hide the thousand commas in the column of 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);
    }
}

我認為 css 是不可能的。 您需要使用某種NumberFormat ,如本例所示:

應用程序:

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();
    }
}

人 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);
    }
}

預習:

在此處輸入圖像描述

接受的答案很好,雖然有點臭,因為它需要在更改格式屬性時手動刷新表格。

另一種選擇是

  • 將格式 - 不可觀察 - 包裝成可觀察的東西
  • 實現一個自定義單元,該單元偵聽更改並根據需要進行自我更新。

如何實現第一個(用於分組狀態)的示例是下面代碼中的 FormattingHandler。 注意

  • 包裝屬性本身實現了包含格式的更新
  • NumberFormat 完全隱藏在處理程序中:盡最大努力不允許在處理程序腳下更改其屬性(顯然它並不完全是萬無一失的,因為外部代碼仍然可以保留對格式的引用並隨意更改它- 它的隔離級別與核心 ObservableList 實現中的后備列表類似)

如何實現第二個的一個例子是 FormattingCell。 它需要一個非空的 FormattingHandler,向分組屬性注冊一個偵聽器,並根據失效通知進行自我更新。 請注意,這可能會引入 memory 泄漏(即使偵聽器很弱!)如果可觀察對象根本沒有改變(不幸的是,這是弱偵聽器設計中的一個已知問題,不會改變) - 唯一的出路將偵聽移動到自定義單元格皮膚中並刪除皮膚處置中的偵聽器。

代碼(從 Anko 的答案中竊取的樣板:)

public class DynamicFormattingCellBinding extends Application {

    /**
     * Observable wrapper around NumberFormat.
     */
    public static class FormattingHandler {
        
        /*
         * Property controlling the grouping of the format.
         */
        private BooleanProperty useGrouping = new SimpleBooleanProperty(this, "useGrouping", false) {

            @Override
            protected void invalidated() {
                super.invalidated();
                groupingInvalidated();
            }
            
        };
        
        private NumberFormat formatter;
        
        public FormattingHandler(NumberFormat formatter) {
            this.formatter = formatter;
            setGrouping(formatter.isGroupingUsed());
        }
        
        public BooleanProperty groupingProperty() {
            return useGrouping;
        }
        
        public boolean isGrouping() {
            return groupingProperty().get();
        }
        
        public void setGrouping(boolean grouping) {
            groupingProperty().set(grouping);
        }
        
        public String format(Number number) {
            return formatter.format(number);
        }

        private void groupingInvalidated() {
            formatter.setGroupingUsed(isGrouping());
        }
    }
    
    public static class FormattingCell<T, S extends Number> extends TableCell<T, S> {
        
        private FormattingHandler formattingHandler;
        private InvalidationListener groupingListener = o -> updateItem(getItem(), isEmpty());

        public FormattingCell(FormattingHandler formattingHandler) {
            this.formattingHandler = Objects.requireNonNull(formattingHandler, "formatter must not be null");
            // Beware: a weak listener isn't entirely safe 
            // will introduce memory leaks if the observable doesn't change!
            formattingHandler.groupingProperty().addListener(new WeakInvalidationListener(groupingListener));
        }

        @Override
        protected void updateItem(S item, boolean empty) {
            super.updateItem(item, empty);
            if (item == null || empty) {
                setText("");
            } else {
                setText(formattingHandler.format(item));
            }
        }
        
    }
    
    @Override
    public void start(Stage primaryStage) {

        TableView<Person> tableView = new TableView<>();
        TableColumn<Person, Integer> salaryColumn = new TableColumn<>("Salary");
        salaryColumn.setCellValueFactory(cc -> cc.getValue().salaryProperty().asObject());
        tableView.getColumns().add(salaryColumn);

        // instantiate the formatting support and register bidi binding with a view element
        FormattingHandler formatter = new FormattingHandler(NumberFormat.getCurrencyInstance());
        CheckBox useGroupingCheckBox = new CheckBox("use grouping");
        useGroupingCheckBox.selectedProperty().bindBidirectional(formatter.groupingProperty());
        // install custom formatting cell
        salaryColumn.setCellFactory(column -> new FormattingCell<>(formatter));

        // 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();
    }

    private static class Person {
        // exact same as in the other answer
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM