javajavascriptjtextpanehtmleditorkit

Passing large amount of data from javascript to a java function


I have developed an htmleditor in java.Now i have installed that applet on my website to format data that would be coming from database.My question is that I am calling a java function from javascript,when i pass small amount of text to my java function callPanelToSetText(String data) it sets the jtextpane correctly.However when i pass large amount of text the applet hangs and does not display the text in jtextpane.

  <head>
    <title>Test page for launching the applet via JNLP</title>
</head>
<body>
    <h3>Test page for launching the applet via JNLP</h3>
    <script src="http://java.com/js/deployJava.js"></script>
    <script>
        var attributes = {

             code:       "researchtexteditor.EditorApplet",
            archive:    "ResearchHTMLEditor.jar, lib/jortho.jar",
            width:      600,
            height:     600,
              id:  'EditorValue'

        };
        var parameters = {jnlp_href:"launch.jnlp"}; <!-- Applet Parameters -->
        var version = "1.7"; <!-- Required Java Version -->
        deployJava.runApplet(attributes, parameters, version);
    </script>
    <!-- Or use the following applet element to launch the applet using jnlp_href -->
    <!--
    <applet width="300" height="300">
        <param name="jnlp_href" value="launch.jnlp"/>
    </applet>
    -->
</body>
  <p><a href="javascript:enterNums();">Launch Example</a></p>
<p><a href="javascript:enterNums_get();">Launch Example1</a></p>
</html>
<script language="javascript">
function enterNums(){
  var content='<?php echo $row['rep_contents'];?>';
   alert(content);
//document.write('Value from Jtextpane 11 '+content);
   EditorValue.callPanelToSetText(content);

 }
    function enterNums_get(){
   var TextVal=EditorValue.getTextData();

   document.write('Value from Jtextpane '+TextVal);
  } 

 <!-- ... -->

\

   Function callPanelToSetText(String value) from java is as below

    public static void callPanelToSetText(String value)
    {
           try {

                 SimpleAttributeSet attr=new SimpleAttributeSet();
                  StyleConstants.setFontFamily(attr,"Arial");
                        StyleConstants.setFontSize(attr,13);
                          StyleConstants.setForeground(attr,Color.BLACK);
                         StyleConstants.setBold(attr,false); 
                            StyleConstants.setItalic(attr,false); 
   editorPanel1.htmlDoc.insertString(editorPanel1.htmlDoc.getLength(),value,attr);
           } catch (BadLocationException ex) {
               Logger.getLogger(EditorApplet.class.getName()).log(Level.SEVERE, null, ex);
           }

    }

 The text that i wish to set on jtextpane is 

   String val="CHAPTER 1    INTRODUCTION    13\n" +
      "1.1  Report Description  13\n" +
      "1.2  Reason for doing the study  14\n" +
      "1.3  Key Benefits    14\n" +
      "1.4  Key Market Segments 15\n" +
      "1.5  Key Audiences   15\n" +
      "1.6  Research Methodology    15\n" +
      "1.6.1    Secondary research  16\n" +
      "1.6.2    Primary research    16\n" +
      "1.6.3    Analyst tools and models    18\n" +
      "CHAPTER 2    EXECUTIVE SUMMARY   19\n" +
      "2.1  Market beyond: what to expect by 2025   22\n" +
      "2.1.1    Moderate growth scenario    22\n" +
      "2.1.2    Rapid growth scenario   24\n" +
      "2.1.3    Diminishing growth scenario 26\n" +
      "CHAPTER 3    MARKET OVERVIEW 29\n" +
      "3.1  Market Definition and Scope 29\n" +
      "3.2  Key findings    30\n" +
      "3.2.1    Top Factors Impacting transparent conductive films market   30\n" +
      "3.2.1.1  Rising adoption of touch UI devices 30\n" +
      "3.2.1.2  Declining cost of smartphones   30\n" +
      "3.2.1.3  Low power consumption   30\n" +
      "3.2.1.4  Minimal reflection  30\n" +
      "3.2.1.5  Thinness    31\n" +
      "3.2.1.6  Flexibility/robustness  31\n" +
      "3.2.1.7  Lack of one-size-fits-all solution  31\n" +
      "3.2.1.8  The multiplicity of options is giving rise to market uncertainty and confusion  32\n" +
      "3.2.2    Top Investment Pockets  34\n" +
      "3.2.3    Top winning strategies  34\n" +
      "3.3  Porter’s five force analysis    35\n" +
      "3.3.1    Large number of suppliers leads to lower bargaining power of suppliers  36\n" +
      "3.3.2    Lower switching cost leads to high Buyer power  37\n" +
      "3.3.3    Unavailability of substitute lowers the may raise the threat of complete substitution   37\n" +
      "3.3.4    Economies of scale leads to low threat of new entrants  37\n" +
      "3.3.5    Numerous competitors lead to high rivalry   38\n" +
      "3.4  Value chain analysis    38\n" +
      "";

Can anyone please tell me where i am going wrong.Thanks and Regards in advance.


Solution

  • It's difficult to provide an answer based solely on those code fragments, but I suspect it's not the amount of data which is triggering this (but passing data from javascript to java (and the other way around) through LiveConnect has serious limitations, in the order of megabytes and not what you showed).

    I suggest these test steps:

    1) Please create a test method, let's call it testCallPanelToSetText, with identical signature (parameters).

    Change the javascript to pass the text to this method instead. On this method's first implementation you'll do nothing. See if the applet hangs. If it doesn't hang, you can eliminate that assumption (that the cause of the problem is the amount of data).

    2) On the second implementation, you'll test if you have a threading issue.

    LiveConnect (JS <-> Java bridge) has some specs on how multiple threads should interact, and swing has an even stricter requirement. Please read this: Swing and multiple threads and make sure you don't call any swing affected function from the LiveConnect thread, it'll probably differ from the swing thread. Make all methods called from javascript schedule the swing calls so that they are run later, on the swing thread, and not immediately (read the article, it's worthy).

    The fact that it works for you on few bytes has to do with the probabilistic behavior of most multithreading bugs, and you should be aware of that.

    After you implement that, it should work, if not, you have to keep in mind this is the required design anyway, as long as you have multiple threads in question.

    EDIT: try to rename your callPanelToSetText to doCallPanelToSetText and make callPanelToSetText a wrapper:

    public static void callPanelToSetText(final String value) {
      SwingUtilities.invokeLater(new Runnable() {
    
      @Override
        public void run() {
          doCallPanelToSetText(value);
        }
      });
    }
    

    and then make the JS call callPanelToSetText as before. See if that solves the problem.