2013-05-09 30 views
0

在JAVAFX應用程序中有一個文本區域,我想在其中創建超鏈接,以便在單擊該超鏈接後,新的舞臺將在運行時打開(其中將包含一個文本區域)和正在進入主文本區域的文本將被轉發到新的舞臺的新文本區域。這是可以實現的嗎?任何建議?如何創建從文本區域到其他舞臺的超鏈接

我在我的應用程序中有下面的代碼,其中「actLogTArea」是我想給超鏈接/按鈕的文本區域,並且我想從其中將文本傳送到主文本區域到新的文本區域建議如何改變?

new Thread(new Runnable() { 
       protected Logger logger = Logger.getLogger(UnixBoxTask.class.getName()); 

       public void run() { 

        try { 

         String user = userName; 
         String pass = pwd; 
         String host = lanIP; 

         JSch jsch = new JSch(); 
         Session session = jsch.getSession(user, host, 22); 
         //session.setHostKeyAlias(sshHostKey); 

         java.util.Properties config = new java.util.Properties(); 
         config.put("StrictHostKeyChecking", "no"); 
         session.setConfig(config); 


         session.setPassword(pass); 
         session.connect(); 

         BufferedReader br = new BufferedReader(new FileReader(scriptPath)); 
         String line; 
         String command_cd = ""; 


         // Build unix command list separated by semicolon 
         while ((line = br.readLine()) != null) { 
          if (line.charAt(0) == '.' && line.charAt(1) == '/') { 
           line = ". " + line; 
          } 
          command_cd += line + ";"; 
         } 

         br.close(); 
         ArrayList nameofthreads = new ArrayList(); 

         StringBuilder outputFromUnix = new StringBuilder(); 

         this.logger.info("Command = " + command_cd); 
         Channel channel = session.openChannel("shell"); 

         if (taskName.equalsIgnoreCase(increseSRB) || taskName.equalsIgnoreCase(decreseSRB)) { 
          String keyValueFile = DeploymentTaskController.getInstance().scriptFilePath + "\\" + taskName + "_KeyValue.txt"; 
          buildParameterList(keyValueFile, taskName); 
          ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp"); 
          channelSftp.connect(); 
          copyFiles(channelSftp, new File(keyValueFile), GlobalValues.getValueFromProps(taskName, "Build Path", LoginController.environment) + "/" + taskName); 
          channelSftp.disconnect(); 
         } 

         channel.connect(); 
         PrintStream commander = new PrintStream(channel.getOutputStream(), true); 
         commander.println(command_cd); 
         commander.println("exit;"); 
         commander.close(); 
         BufferedWriter bw = null; 
         InputStream outputstream_from_the_channel = channel.getInputStream(); 
         BufferedReader reader = new BufferedReader(new InputStreamReader(outputstream_from_the_channel)); 
         bw = new BufferedWriter(new FileWriter(resultLogFile.getAbsolutePath(), true), 20000); 


         String jarOutput; 
         int count=0; 

         while ((jarOutput = reader.readLine()) != null) { 
          this.logger.info("Status Update = " + jarOutput); 

          bw.write(jarOutput); 
          if (jarOutput.contains("Test")) { 
           nameofthreads.add(jarOutput); 
           continue; 

          } 

           bw.newLine(); 
           bw.flush(); 
           outputFromUnix.append(jarOutput).append("\n"); 
           // Display in activity log area in realtime. 
           if (DeploymentTaskController.actLogTArea != null && !taskName.equalsIgnoreCase(connectBundle)) { 
            final String outputStr = outputFromUnix.toString(); 
            Platform.runLater(new Runnable() { 
             @Override 
             public void run() { 

              **DeploymentTaskController.actLogTArea.setText(outputStr); 
              DeploymentTaskController.actLogTArea.end();** 

             } 
            }); 
           } 

          } 




          bw.close(); 
          reader.close(); 


          do { 
           Thread.sleep(1000); 
          } while (!channel.isEOF()); 



          channel.disconnect(); 
          session.disconnect(); 

          Thread.sleep(1000); 

         } catch (JSchException jex) { 
         System.out.println("JSCH Exception : " + jex.getMessage()); 
        } catch (Exception ex) { 


         System.out.println("General Exception JSCH Block : " + ex.getMessage() + AppUtil.stack2string(ex)); 
        } 


       } 
      }).start(); 

回答

1

這很容易實現。實際上,超鏈接不是那種用來做這件事的東西。不要混淆javafx和html。

怎麼辦:

  1. 創建一個按鈕(超鏈接,如果你想)
  2. setOnAction(new EventHandler<ActionEvent>(){})和接下來的代碼添加到函數:
  3. new Stage(new Scene(new Group(new TextArea(ta.textProperty().bind(ta.textProperty()))))).show(),其中Ta - 是一個文本區域從你的第一個階段。

你要注意,JavaFX是一個面向對象的圖形用戶界面技術,您可以隨時創建新的JavaFX組件對象,並更新現有的在任何時候,當你擁有它的訪問或鏈接。另一個重要的概念,對你有用 - 屬性。屬性包含一段時間的值。並且屬性可以綁定 - 當一個屬性的值自動傳播到另一個綁定屬性時。每個javafx組件(控件/佈局)接口都基於屬性的使用。

Button b = new Button("Create new console"); 
b.setOnAction(new EventHandler<ActionEvent>(){ 
    ... action() { 
     new Stage(new Scene(new Group(new TextArea(DeploymentTaskController.actLogTArea.getText()))))).show(); 
    } 
}); 

相反DeploymentTaskController.actLogTArea,你將不得不創造一種散列映射,來決定,其文本區域選擇在添加新的內容:

DeploymentTaskController.actLogTAreaHashMap.get(<some key, to determine text area>); 

,並添加新的文本區域有,當你創建一個新的。

+0

你能建議我們如何在上面的代碼中實現它嗎? – Rohan 2013-05-09 15:21:50

+0

這段代碼片段有幫助嗎? – 2013-05-09 15:28:00

+0

好的,我會試試這個。但是我們在哪裏創建這個按鈕?我想創建其他活動區域,當我們點擊當前文本區域按鈕時,這個區域會被調用 – Rohan 2013-05-09 15:37:43

相關問題