在JavaFX中找到可能的Glitch

时间:2016-06-20 05:39:15

标签: java java-8 javafx-8 borderpane

很抱歉,标题含糊不清,但我认为我在JavaFX BorderPane课程中发现了一个小故障,但我并不积极。所以我正在做的是在一个Thread内部的javaFx Concurrent Task对象中运行此方法。此方法一直有效,直到它到达print语句。它打印出1然后不会移过root.setCenter方法。如果我注释掉它继续运行的代码,否则它会像在无限循环中那样卡在它上面。重要的是要注意根(boderpane对象)本地存储在JavaFX主线程中。谢谢你的任何建议。

  // will be used to store all the sites we still need to visit so we can do
  // a breadth first graph traversal of the hostsite
  Queue<URL> unvistedURLs = new LinkedList<>();
  LinkedList<Text> currentLevelText = new LinkedList<>();
  Queue<URL> levelCheckpoints = new LinkedList<>();
  int currentLevelHieght = 0;

  // the origional host
  String hostName = origin.getHost();

  // temporary objects
  HTMLLinks endHTMLLinks = null;
  try
  {
     endHTMLLinks = new HTMLLinks(origin);
  }
  catch (IOException e1)
  {
     // TODO Auto-generated catch block
     e1.printStackTrace();
  }

  HostSiteInfo hostSiteInfo = new HostSiteInfo();
  URL currentURL;
  Group displayArea = new Group();

  System.out.println(1);
  root.setCenter(displayArea);
  System.out.println(2);
  // imediatley input the host as a site we need to visit
  unvistedURLs.add(origin);
  levelCheckpoints.offer(origin);


@Override
public void start(Stage primaryStage)
{
  try
  {
     final BorderPane root = new BorderPane();
     Scene scene = new Scene(root, 1600, 1000);

     @SuppressWarnings("rawtypes")
     Thread renderThread = new Thread(new Task(){

        @Override
        protected Object call() throws Exception
        {
           try
           {

              WebSpider.traverseURLs(root,
                    new URL("http://www.georgefox.edu/"),
                    new PrintStream(System.out));
           }
           catch (MalformedURLException e)
           {
              // TODO Auto-generated catch block
              e.printStackTrace();
           }

           return null;
        }});

     renderThread.setDaemon(true);
     renderThread.start();

JavaFx应用程序的根目录在此start方法中初始化。

1 个答案:

答案 0 :(得分:0)

假设在Task内调用了Task(这在您的代码中不清楚),您将获得异常,因为您尝试从JavaFX UI线程以外的线程修改场景图。由于这是在root.setCenter(displayArea)内完成的,因此该任务会捕获异常并且您最终没有看到它。

要解决此问题,请将Platform.runLater(() -> root.cetCenter(displayArea)); 替换为

onFailed

this

为了能够解决此类问题,最好为Task设置Task<Void> myTask = new Task<>() { ... } myTask.setOnFailed(workerStateEvent -> { System.out.println("Something wrong happened..."); myTask.getException().printStackTrace(); // Or handle the problem however suits your application. }); 处理程序:

onkeypress