2016-07-25 4 views
0

Long 값을 JavaFX로 TableView에 포맷하려고했습니다.JavaFX 형식 TableView의 SimpleLongProperty

가 나는 테이블에 표시 할 행을 저장하기 위해 다음과 같은 클래스가 :

다음
import java.text.DecimalFormat; 

    import javafx.beans.property.SimpleDoubleProperty; 
    import javafx.beans.property.SimpleLongProperty; 
    import javafx.beans.property.SimpleStringProperty; 

    public class DataByCurrencyPairRow { 

     private DecimalFormat integerFormat = new DecimalFormat("#,###"); 

     private SimpleStringProperty currencyPair = new SimpleStringProperty(""); 
     private SimpleDoubleProperty shareOfTotalVolume = new SimpleDoubleProperty(0); 
     private SimpleLongProperty totalVolume = new SimpleLongProperty(0); 
     private SimpleLongProperty currencyBought = new SimpleLongProperty(0); 
     private SimpleLongProperty currencySold = new SimpleLongProperty(0); 
     private SimpleLongProperty monthlyAverage = new SimpleLongProperty(0); 

     public DataByCurrencyPairRow() { 
      currencyPair.set(""); 
      shareOfTotalVolume.set(0); 
      totalVolume.set(0); 
      currencyBought.set(0); 
      currencySold.set(0); 
      monthlyAverage.set(0); 
     } 

     public String getCurrencyPair() { 
      return currencyPair.getValue(); 
     } 

     public void setCurrencyPair(String currencyPair) { 
      this.currencyPair.setValue(currencyPair); 
     } 

     public Long getMonthlyAverage() { 
      return monthlyAverage.getValue(); 
     } 

     public void setMonthlyAverage(Long monthlyAverage) { 
      this.monthlyAverage.setValue(monthlyAverage); 
     } 

     public Long getCurrencySold() { 
      return currencySold.getValue(); 
     } 

     public void setCurrencySold(Long currencySold) { 
      this.currencySold.setValue(currencySold); 
     } 

     public Long getCurrencyBought() { 
      return currencyBought.getValue(); 
     } 

     public void setCurrencyBought(Long currencyBought) { 
      this.currencyBought.setValue(currencyBought); 
     } 

     public Long getTotalVolume() {  
      return totalVolume.getValue(); 
     } 

     public void setTotalVolume(Long totalVolume) { 
      this.totalVolume.setValue(totalVolume); 
     } 

     public Double getShareOfTotalVolume() { 
      return shareOfTotalVolume.getValue(); 
     } 

     public void setShareOfTotalVolume(Double shareOfTotalVolume) { 
      this.shareOfTotalVolume.setValue(shareOfTotalVolume); 
     } 

    } 

내가 가진 내가 표를 얻기 위해 updateItem 메소드를 오버라이드 (override)하는 것을 시도하고있다 initialize 방법 컨트롤러 천 개 쉼표를 구분 표시하려면

public class MainController { 

    private static final String DEFAULT_TIME_HORIZON = new String("0"); 
    private final NumberFormat integerFormat = new DecimalFormat("#,###"); 


    @FXML 
    TableView<DataByCurrencyPairRow> tableTransactionsByCurrencyPair; 

    @FXML 
    TableColumn<DataByCurrencyPairRow, Long> columnTotal; 


    @FXML 
    void initialize() { 

     columnTotal.setCellFactory(
       new Callback<TableColumn<DataByCurrencyPairRow, SimpleLongProperty>, TableCell<DataByCurrencyPairRow, SimpleLongProperty>>() { 

        @Override 
        public TableCell<DataByCurrencyPairRow, SimpleLongProperty> call(TableColumn<DataByCurrencyPairRow, SimpleLongProperty> param 
        ) { 
         return new TableCell<DataByCurrencyPairRow, SimpleLongProperty>() { 

          @Override 
          protected void updateItem(SimpleLongProperty item, boolean empty) { 
           super.updateItem(item, empty); 
           if (item == null || empty) { 
            setText("0"); 
            setStyle(""); 
           } else { 
            setText(integerFormat.format(item.longValue())); 
           } 
          } 

         }; 
        } 
       } 
     ); 

그리고 이것은 TableView 채우는 방법이다

01,238을

어떻게하는지 보여주는 방법으로 도와주세요 !! 나는 SimpleLongProperty 대신 updateItem 메서드를 Long으로 대체하려고 시도했지만 코드에서 내 IDE가 코드를 받아 들였지만 여전히 숫자가 테이블에서 포맷되지 않았습니다.

미리 감사드립니다.

+0

:

여기 Long의와 포맷 컬럼의 간단한 예입니다. 즉 총 무엇? – yamenk

+0

셀 값 공장이 무엇인지, 실제로 보았는지 설명되어 있지 않습니다. –

답변

1

LongPropertyObservableValue<Number>가 아닌 ObservableValue<Long> (또는 ObservableValue<SimpleLongProperty>)를 구현한다. 따라서 테이블 열은 TableColumn<DataByCurrencyPair, Number>이어야하며 셀 팩터는 해당 유형을 적절하게 일치시켜야합니다. 이 칼럼에 표시 할 값이 무엇

import java.text.DecimalFormat; 
import java.text.NumberFormat; 
import java.util.Random; 

import javafx.application.Application; 
import javafx.beans.property.LongProperty; 
import javafx.beans.property.SimpleLongProperty; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.property.StringProperty; 
import javafx.scene.Scene; 
import javafx.scene.control.TableCell; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 
import javafx.stage.Stage; 

public class TableWithFormattedLong extends Application { 

    private final NumberFormat integerFormat = new DecimalFormat("#,###"); 

    @Override 
    public void start(Stage primaryStage) { 
     TableView<Item> table = new TableView<>(); 
     TableColumn<Item, String> itemColumn = new TableColumn<>("Item"); 
     itemColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty()); 

     TableColumn<Item, Number> valueColumn = new TableColumn<>("Value"); 
     valueColumn.setCellValueFactory(cellData -> cellData.getValue().valueProperty()); 

     valueColumn.setCellFactory(tc -> new TableCell<Item, Number>() { 
      @Override 
      protected void updateItem(Number value, boolean empty) { 
       super.updateItem(value, empty); 
       if (value == null || empty) { 
        setText(""); 
       } else { 
        setText(integerFormat.format(value)); 
       } 
      } 
     }); 

     table.getColumns().add(itemColumn); 
     table.getColumns().add(valueColumn); 

     Random rng = new Random(); 

     for (int i = 1 ; i <= 20 ; i++) { 
      table.getItems().add(new Item("Item "+i, rng.nextLong())); 
     } 

     primaryStage.setScene(new Scene(table, 600, 600)); 
     primaryStage.show(); 
    } 

    public static class Item { 
     private final StringProperty name = new SimpleStringProperty(); 
     private final LongProperty value = new SimpleLongProperty(); 

     public Item(String name, long value) { 
      setName(name); 
      setValue(value); 
     } 

     public final StringProperty nameProperty() { 
      return this.name; 
     } 


     public final String getName() { 
      return this.nameProperty().get(); 
     } 


     public final void setName(final String name) { 
      this.nameProperty().set(name); 
     } 


     public final LongProperty valueProperty() { 
      return this.value; 
     } 


     public final long getValue() { 
      return this.valueProperty().get(); 
     } 


     public final void setValue(final long value) { 
      this.valueProperty().set(value); 
     } 



    } 

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

매력처럼 작동하며 @ yamenK의 솔루션에 대해 의견을 말하면 cellValueFactory와 cellFactory의 차이점을 이해하는 데 도움이됩니다. 고맙습니다! – Hiltunea

0

Cellfactory를 설정할 필요가 없으며 CellValueFactory 만 설정하면됩니다.

TableColumn<DataByCurrencyPairRow, String> columnTotal; 
columnTotal.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<DataByCurrencyPairRow,String>, ObservableValue<String>>() { 

      @Override 
      public ObservableValue<String> call(CellDataFeatures<DataByCurrencyPairRow, String> param) { 
       DataByCurrencyPairRow value = param.getValue(); 
       return new ReadOnlyStringWrapper(NumberFormat.getNumberInstance(Locale.US).format(123123)); //replace the number with the calculated total 
      } 
     }); 
+1

일부 상황에서는 작동 할 수도 있지만 좋은 습관은 아닙니다. 실제로 'Long' 인 실제 모델과 모순되는'String' 데이터를 테이블 컬럼에 표시하고 있습니다. 예를 들어 이것을 편집 가능하게하고'TextFieldTableCell'을 사용하고자한다면 편집을 커밋 할 때 데이터를 올바르게 업데이트 할 수 없습니다. 의도 된 사용은'cellValueFactory'가 표시되는 데이터를 정의하고'cellFactory'가 그것을 정의하는 * how *를 정의한다는 것입니다. 이 유스 케이스의 경우,'cellFactory'가 의도 된 해결책입니다. –

관련 문제