2016-09-26 57 views
0

我正在嘗試調整TableView中某個特定cell的大小,特定爲。增加特定行的單元格大小-JAVAFX TableView

我使用的代碼,

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); 
    } 
} 
當我運行這個完整 name 大小

增加(如預期)。

相反,我只想增加包含「三個」的單元格的大小。

我不確定,我應該整合哪些代碼才能使其以這種方式工作。

我該如何解決這個問題?

+0

你想在不同寬度的單元格在同一列嗎?對不起,但這不是'TableView'支持的。這需要對'TableView'' Skin'進行大量修改才能達到這種效果......當然,您可以在'TableView'或類似的東西中顯示一個與其他單元格大小不同的'graphic',但是'TableView'將會仍然確保一排中的所有單元具有相同的高度,並且一列中的所有單元具有相等的寬度... – fabian

+0

但您希望如何工作?該特定列將具有最長單元格的寬度?所以你甚至不會注意到與其他人一樣 – Elltz

+0

@Elltz我試圖通過增加單元格寬度到表格寬度和縮小其他列單元格的寬度來合併單元格。我期待它能夠像excel合併兩個或更多單元格一樣工作。 – 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()方法, (使用commit()),計算單元格的寬度Cell.prefWidth(-1)Cell.getWidth(),獲得文本在第一個單元格中佔據的空間後,將其與您的寬度進行比較,如果該寬度>則修剪文本並使其重疊。在這裏你有像

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

所以現在你看細胞getIndex()一行columnPos>:起點

,你改變風格,例如細胞的初始細胞與具有透明邊框正確的顏色

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

現在其他overlaping CELSS將具有左透明

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

並將其餘字符串的文本設置爲單元格。如果可能的話,改變TableRow的背景,使其與你的單元格相匹配,因此你不會看到分隔線。

試試看,想放棄演示,但沒有太多時間。

希望如果有幫助

相關問題