4

次のように JavaFX で ListView を作成すると:

ObservableList<String> elements = FXCollections.observableArrayList("John", "Doe");
ListView<String> lView = new ListView<String>(elements);

私がやりたいのは、ListViewの行の終わりから始まる線を描くことです。たとえば、「ジョン」から

これを行うには、行「John」の場所 (x,y) が必要です。場所を取得することは可能ですか?

アップデート

これは、Swing と Piccolo2D を使用して取得したサンプル インターフェイスです。ただし、そのライブラリを使用するのは苦痛です。JavaFXで同じことができるかどうか疑問に思っています

ここに画像の説明を入力

4

1 に答える 1

2

それは可能ですが、あなたが望んでいたほど簡単ではないかもしれません. (または)Cell内の特定のレイアウト座標を決定するには、その特定のオブジェクトにアクセスする必要があります。最善の方法 (おそらく JavaFX 2.2 で唯一の方法) は、コンテナーにカスタム を提供し、それぞれを公開することです。をどのように公開するかは、線を引くためのトリガーが何であるかによって異なります。ListViewTableView/TreeViewCellCellCellFactoryCellCell

あなたのイラストに基づいて、ListViewsが入力されたら、各セルにアクセスする必要があります。List<ListCell<String>>のフィールドでこれを行うことができますCellFactory。ここで s について 1 つの注意事項を述べますListCell。は可能な限り sListViewSkinを再利用します。Cellつまり、最終的にスクロールするリストにデータを入力して接続しようとすると、行を正しい場所に維持するのがはるかに難しくなります。すべてのリスト項目が画面に収まるようにすることをお勧めします。

以下は、コメントにいくつかのメモを含む例です。描画するための正しい座標を取得するLineには、この例では行っていない SceneGraph のオフセットを計算する必要があることに注意してください。

package listviewcellposition;

import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import javafx.util.Callback;

public class ListViewCellPosition extends Application {

    // CustomCellFactory for creating CustomCells
    public class CustomCellFactory implements
            Callback<ListView<String>, ListCell<String>> {

        List<ListCell<String>> allCells = new ArrayList<>();

        @Override
        public ListCell<String> call(final ListView<String> p) {
            final CustomCell cell = new CustomCell();
            allCells.add(cell);
            return cell;
        }

        public List<ListCell<String>> getAllCells() {
            return allCells;
        }
    }

    // CustomCell is where the exposure occurs. Here, it's based on the
    // Cell being selected in the ListView. You could choose a different
    // trigger here but you'll need to explore.
    public class CustomCell extends ListCell<String> {
        // General display stuff
        @Override
        protected void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);
            if (empty) {
                setText(null);
                setGraphic(null);
            } else {
                setText(item == null ? "" : item);
                setGraphic(null);
            }
        }
    }

    @Override
    public void start(Stage primaryStage) {
        // This pane will contain the lines after they are created.
        // I set it into an AnchorPane to avoid having to deal with 
        // resizing.
        Pane linePane = new Pane();
        AnchorPane pane = new AnchorPane();
        pane.setPrefSize(100, 250);
        AnchorPane.setBottomAnchor(linePane, 0.0);
        AnchorPane.setLeftAnchor(linePane, 0.0);
        AnchorPane.setRightAnchor(linePane, 0.0);
        AnchorPane.setTopAnchor(linePane, 0.0);
        pane.getChildren().add(linePane);

        ListView<String> lView = new ListView<>();
        lView.setPrefSize(100, 250);
        CustomCellFactory lCellFactory = new CustomCellFactory();
        lView.setCellFactory(lCellFactory);

        ListView<String> rView = new ListView<>();
        rView.setPrefSize(100, 250);
        CustomCellFactory rCellFactory = new CustomCellFactory();
        rView.setCellFactory(rCellFactory);

        lView.getItems().addAll("Bill", "Doctor", "Steve", "Joanne");
        rView.getItems().addAll("Seuss", "Rowling", "King", "Shakespeare");

        HBox root = new HBox();
        root.getChildren().addAll(lView, pane, rView);

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

        connectCells(lCellFactory, "Bill", rCellFactory, "Shakespeare", linePane);
        connectCells(lCellFactory, "Doctor", rCellFactory, "Seuss", linePane);
        connectCells(lCellFactory, "Steve", rCellFactory, "King", linePane);
        connectCells(lCellFactory, "Joanne", rCellFactory, "Rowling", linePane);
    }

    // Looks up the ListCell<> for each String and creates a Line
    // with the coordinates from each Cell. The calculation is very 
    // contrived because I know that all the components have the same 
    // x-coordinate. You'll need more complicated calculations if your
    // containers are not aligned this way.
    private void connectCells(CustomCellFactory lCellFactory, String lVal,
            CustomCellFactory rCellFactory, String rVal, Pane linePane) {

        List<ListCell<String>> lList = lCellFactory.getAllCells();
        ListCell<String> lCell = null;

        for (ListCell<String> lc : lList) {
            if (lc.getItem() != null && lc.getItem().equals(lVal)) {
                lCell = lc;
                break;
            }
        }

        List<ListCell<String>> rList = rCellFactory.getAllCells();
        ListCell<String> rCell = null;

        for (ListCell<String> rc : rList) {
            if (rc.getItem() != null && rc.getItem().equals(rVal)) {
                rCell = rc;
                break;
            }
        }

        if (lCell != null && rCell != null) {
            double startY = lCell.getLayoutY() +
                    (lCell.getBoundsInLocal().getHeight() / 2);
            double endY = rCell.getLayoutY() +
                    (rCell.getBoundsInLocal().getHeight() / 2);

            Line line = new Line(0, startY, 
                    linePane.getBoundsInParent().getWidth(), endY);
            line.setStrokeWidth(2);
            line.setStroke(Color.BLACK);

            linePane.getChildren().add(line);
        }
    }   

    public static void main(String[] args) {
        launch(args);
    }
}
于 2013-09-26T14:33:18.257 に答える