2016-09-21 12 views
1

JavaFxでリストビュー項目のテキストフォントサイズを変更するにはどうすればよいですか?各行テキストのサイズは異なります。私は細胞因子のプロパティを使用してみましたが、私はそれを使用する方法を知らない。誰も私をここで助けることができますか?リストビューのフォントサイズをJavaFXで変更

答えて

0

同様の質問はここにある:JavaFXの中で外部CSSを使用する方法

.list-cell:filled:selected:focused, .list-cell:filled:selected { 
    -fx-background-color: linear-gradient(#328BDB 0.0%, #207BCF 25.0%, #1973C9 75.0%, #0A65BF 100.0%); 
    -fx-text-fill: white; 
} 

.list-cell{ 
    -fx-font-size:15.0; 
} 

.list-cell:even { /* <=== changed to even */ 
    -fx-background-color: white; 
} 

.list-cell:filled:hover { 
    -fx-background-color: #0093ff; 
    -fx-text-fill: white; 
} 

How to change the font size in ListView in JavaFX?

また、あなたは以下の.cssで遊ぶことができますか?プログラムでJava8の前にテキスト?:

のサイズを変更する方法

How to use external css file to style a javafx application

listView.setCellFactory(new Callback<ListView<String>, ListCell<String>>() { 
      @Override 
      public ListCell<String> call(ListView<String> p) { 
       return new ListCell<String>() { 
        @Override 
        protected void updateItem(String item, boolean empty) { 
         super.updateItem(item, empty); 
         if (item != null) { 
          setText(item); 

          // decide to add a new styleClass 
          // getStyleClass().add("costume style"); 
          // decide the new font size 
          setFont(Font.font(16)); 
         } 
        } 
       }; 
      } 
     }); 

ラムダ式の場合:

listView.setCellFactory(cell -> { 
      return new ListCell<String>() { 
       @Override 
       protected void updateItem(String item, boolean empty) { 
        super.updateItem(item, empty); 
        if (item != null) { 
         setText(item); 

         // decide to add a new styleClass 
         // getStyleClass().add("costume style"); 
         // decide the new font size 
         setFont(Font.font(16)); 
        } 
       } 
      }; 
     }); 
+0

私はそれを見ましたが、これらのフォントサイズは修正されています。私は変数が欲しい。各行のサイズはいつでも変更できます –

+0

@ahmadファーガズ私は質問を編集して、それがあなたのために働くかどうかを教えてください。 – GOXR3PLUS

0

javaFxでは、CSSを使用してさまざまなオブジェクトのスタイルを設定できます。それはリストビューのオブジェクトに依存しますが、このようなものになります。アイテム・クラスの

ListView<Hyperlink> listview = .. 

      ObservableList<Hyperlink> lists = listview.getItems(); 

      for(Hyperlink link:lists) 
      { 
       link.setStyle("-fx-background-color:#ffffff"); 
      } 
+0

ListViewは文字列であり、その上で動作しません。 –

+0

@ahmadfaraz文字列はjavafxオブジェクトではありません。私は文字列を表すためにLabelを使用します –

1

ストアテキストとテキストサイズ

public class SizedText { 

    private final int textSize; 
    private final String text; 

    public SizedText(int textSize, String text) { 
     this.textSize = textSize; 
     this.text = text; 
    } 

    public int getTextSize() { 
     return textSize; 
    } 

    public String getText() { 
     return text; 
    } 
} 

そしてupdateItem方法でアイテムデータに係るテキストサイズを設定ListCell Sを返すcellFactoryを使用します。

@Override 
public void start(Stage primaryStage) { 
    ListView<SizedText> list = new ListView<>(FXCollections.observableArrayList(
      new SizedText(10, "10"), 
      new SizedText(15, "15"), 
      new SizedText(20, "20"), 
      new SizedText(25, "25"), 
      new SizedText(30, "30"), 
      new SizedText(35, "35"), 
      new SizedText(40, "40"), 
      new SizedText(50, "50"), 
      new SizedText(60, "60") 
    )); 

    list.setCellFactory(l -> new ListCell<SizedText>() { 

     @Override 
     protected void updateItem(SizedText item, boolean empty) { 
      super.updateItem(item, empty); 

      if (empty || item == null) { 
       setText(null); 
      } else { 
       setText(item.getText()); 
       setFont(Font.font(item.getTextSize())); 
      } 
     } 

    }); 

    Scene scene = new Scene(list); 

    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 
関連する問題