2016-09-26 6 views
0

TableViewの特定のcellのサイズを変更しようとしています。具体的にはです。特定のRow-JAVAFX TableViewのセルサイズを大きくする

iが使用するコード、

import javafx.application.Application; 
import javafx.application.Platform; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.TableCell; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 
import javafx.stage.Stage; 
import javafx.util.Callback; 

public class TableColors extends Application { 

    TableView<String> tView = new TableView<>(); 

    @Override 
    public void start(Stage primaryStage) { 
     TableColumn<String, String> tcName = new TableColumn("name"); 
     TableColumn<String, String> tcName1 = new TableColumn("name1"); 
     ObservableList<String> items = FXCollections.observableArrayList("One", "Two", "Three", "Four"); 
     tView.setItems(items); 
     tView.getColumns().add(tcName); 
     tView.getColumns().add(tcName1); 

     tView.setColumnResizePolicy((param) -> true); 
     Platform.runLater(() -> customResize()); 

     tcName.setCellValueFactory((p) -> new SimpleStringProperty(p.getValue())); 
     tcName.setCellFactory(new Callback<TableColumn<String, String>, TableCell<String, String>>() { 
      @Override 
      public TableCell call(TableColumn p) { 
       return new TableCell<String, String>() { 
        @Override 
        public void updateItem(final String item, final boolean empty) { 
         super.updateItem(item, empty);//*don't forget! 
         if (item != null) { 
          setText(item); 
          setAlignment(Pos.CENTER); 
         } else { 
          setText(null); 
         } 
        } 
       }; 
      } 
     }); 

     Scene scene = new Scene(tView, 300, 250); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    private void customResize() { 
     try { 
      TableColumn<?, ?> col = tView.getColumns().get(0); 
      col.setPrefWidth(150); 
     } catch (Exception r) { 
      r.printStackTrace(); 
     } 
    } 

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

iが(予想通り)この完全nameカラムサイズが大きくなる実行。

代わりに、「3個」を含むセルのサイズだけを大きくしたいです。

わかりません。このように動作させるには、どのようなコードを組み込むべきですか。

どうすればこの問題を解決できますか?

+0

同じ列に異なる幅のセルが必要ですか?申し訳ありませんが、それは 'TableView'がサポートするものではありません。この効果を達成するために 'TableView''Skin'を大幅に変更する必要があります。' TableView'などの他のセルとは異なるサイズの 'graphic'を表示できますが、' TableView'は行のすべてのセルの高さが同じで、列内のすべてのセルの幅が同じであることを確認してください... – fabian

+0

しかし、これはどのように機能しますか?その特定の列は最も長いセルの幅を持ちますか?他の人にも気付かないでしょう – Elltz

+0

@Elltzセルの幅をテーブルの幅に拡大し、他の列のセルの幅を縮小することで、セルをマージするための回避策を実行しようとしています。私はそれが2つ以上の細胞をどのように融合させるかのように機能することを期待しています。 – user3164187

答えて

0

あなたは

あなたのセルDataHolderこの

public class DataHolder{ 
    boolean cellOverlaps = false; //tells the cell whether its string overlaps others 
    int rowPos = -1;// the vertical position 
    int columnPos =-1; //its column position 
    boolean startPoint = false;//whether its the start point 
    //in other words it begins from here hence its right border we care 
    String text = "" 
} 

のようなものを実装してみましょうこのロジックを試すことができますので、あなたは基本的に何をすべきか、あなたのupdateIndex()、またはupdateItem()方法やあなたの細胞のあなたのCommit()方法であります(コミット()を使用して)セルの幅をCell.prefWidth(-1)またはCell.getWidth()で計算すると、テキストが最初のセルで占有された後、そのセルを幅と比較してからテキストをトリミングして重複させます。ここで、あなたは今、あなたがcolumnPosと行getIndex()のセルを探し

dataHolder.cellOverlaps = true; 
dataHolder.startPoint = true;//other overlapping cell will have this false 
dataHolder.rowPost = getIndex(); 
dataHolder.columnPos = getTableView().getColumns().indexof(getTableColumn()); 

>にstartPoint

のようなものを持っており、透明ボーダー右の色を持っていると、あなたは、例えば、セルのスタイルに開始セルを変更します

-fx-border-color: black black black transparent; 

は今、他のoverlaping celssがあります左の透明

-fx-border-color: black transparent black black; 

と残りの文字列のテキストをセルに設定します。可能であれば、TableRowのバックグラウンドを変更してセルと一致するようにして、分割線を表示しないようにします。

デモを削除したいと思っていましたが、時間があまりありません。

希望があれば

関連する問題