Spostamento dei nodi JavaFX tra le fasi


Sto riscrivendo un'applicazione Swing in JavaFX, dove permetto agli utenti di presentare più aree di lavoro come finestre o schede. Tuttavia, il mio codice FX non mostrerà il contenuto spostato da più di una scheda in una nuova fase; solo il contenuto della scheda attualmente selezionata appare nelle mie nuove fasi. Ho distillato il mio codice in un piccolo esempio qui sotto. Qualcuno può dirmi cosa è andato storto?

package scenes;

import java.util.ArrayList;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;


public class StageSwapper extends Application {

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

    private TabPane tabs = new TabPane();    

    public void start(Stage stage) {
        stage.setTitle("Stage Swapper");
        BorderPane p = new BorderPane();
        p.setCenter(tabs);
        tabs.getTabs().addAll(new Swapee("First").createTab(), new Swapee("Second").createTab());
        Scene s = new Scene(p);
        stage.setScene(s);
        stage.show();
        launchSwap();
    }

    private void launchSwap() {
        new Thread() {
            public void run() {
                try {
                    sleep(10000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Platform.runLater(new Runnable() {
                    public void run() {
                        for (Swapee s : Swapee.list) {
                            createWindow(s);
                        }
                    }                   
                });
            }
        }.start();
    }

    public void createWindow(Swapee s) {
        Stage window = new Stage();
        window.setTitle("New Window");
        window.setY(200);
        window.setX(200);
        BorderPane p = new BorderPane();
        p.setCenter(s);
        window.setScene(new Scene(p));
        window.show();
    }

}

class Swapee extends Label {

    static private int count;
    static ArrayList<Swapee> list = new ArrayList<>();

    String name;

    Swapee(String name) {
        super("Swappable Item " + ++count);
        this.name = name;
        list.add(this);
    }

    Tab createTab() {
        Tab t = new Tab(name);
        t.setContent(this);
        return t;
    }

}
Author: user3439797, 2014-03-20

1 answers

Non è stata specificata la dimensione delle finestre che si sta creando. In questo momento hanno larghezza e lunghezza pari a 0. È possibile utilizzare il seguente approccio:

BorderPane p = new BorderPane();
p.setPrefSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE);
p.setCenter(s);

BorderPane verrà ridimensionata in base al suo contenuto e anche la finestra verrà ridimensionata.

 0
Author: xuesheng, 2014-03-21 17:25:44