Note that there are some explanatory texts on larger screens.

plurals
  1. POSwingWorker (not working), loop ends after 1st iteration
    text
    copied!<p>I expected to see close to 200,000 lines (196,608) of output from the loop in this code. It only prints one line. Can anyone spot the blunder?</p> <pre><code>import java.awt.*; import java.util.*; import javax.swing.*; class SwingWorkerUnicodeTest { private String[] fontNameArray; private JLabel output = new JLabel("Processing.."); private JProgressBar progressBar = new JProgressBar(); class CodePointDetailWorker extends SwingWorker&lt;Object, Object&gt; { private ArrayList&lt;Character.UnicodeBlock&gt; unicodeBlockNames; private ArrayList&lt;Character.UnicodeScript&gt; unicodeScripts; private int[] glyphCount = new int[fontNameArray.length]; public CodePointDetailWorker() { progressBar.setVisible(true); Arrays.fill(glyphCount, 0); } @Override protected Void doInBackground() throws Exception { // Check for for the first 3 planes. The next 11 are unassigned int pS = 3*65536; for (int kk = 0; kk &lt; pS; kk++) { System.out.println("doInBackground " + kk + " " + pS); doForEveryCodePoint(kk); } return null; } @Override public void done() { output.setText("Done!"); } private final void doForEveryCodePoint(final int codePoint) { Character.UnicodeBlock block = Character.UnicodeBlock.of(codePoint); if (block != null &amp;&amp; !unicodeBlockNames.contains(block)) { unicodeBlockNames.add(block); } Character.UnicodeScript us = Character.UnicodeScript.of(codePoint); if (us == null || us.toString() == null) { } else { if (!unicodeScripts.contains(us)) { unicodeScripts.add(us); } } // fonts - test for points in all 6 defined blocks. for (int ii = 0; ii &lt; fontNameArray.length; ii++) { Font f = new Font(fontNameArray[ii], Font.PLAIN, 16); if (f.canDisplay(codePoint)) { glyphCount[ii]++; } } } } public SwingWorkerUnicodeTest() { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); fontNameArray = ge.getAvailableFontFamilyNames(); JPanel gui = new JPanel(new BorderLayout()); gui.add(progressBar, BorderLayout.CENTER); gui.add(output, BorderLayout.PAGE_END); CodePointDetailWorker cpdw = new CodePointDetailWorker(); cpdw.execute(); JOptionPane.showMessageDialog(null, gui); } public static void main(String[] args) { Runnable r = new Runnable() { @Override public void run() { new SwingWorkerUnicodeTest(); } }; // Swing GUIs should be created and updated on the EDT // http://docs.oracle.com/javase/tutorial/uiswing/concurrency SwingUtilities.invokeLater(r); } } </code></pre> <h2>Edit</h2> <p>Fixed code, based on the advice of the first 2 answers. </p> <p>It now both implements the overridden method that reports errors, but initializes the arrays <strike>for ..much output</strike> and shows progress in the progress bar. </p> <pre><code>import java.awt.*; import java.util.*; import javax.swing.*; class SwingWorkerUnicodeTest { private JLabel output = new JLabel("Processing.."); // Check for for the first 3 planes. The next 11 are unassigned int pS = 3 * 65536; private JProgressBar progressBar = new JProgressBar(0, pS); class CodePointDetailWorker extends SwingWorker&lt;Object, Object&gt; { private ArrayList&lt;Character.UnicodeBlock&gt; unicodeBlockNames; private ArrayList&lt;Character.UnicodeScript&gt; unicodeScripts; private int[] glyphCount; private String[] fontNameArray; public CodePointDetailWorker(String[] fontNameArray) { this.fontNameArray = fontNameArray; progressBar.setVisible(true); glyphCount = new int[fontNameArray.length]; Arrays.fill(glyphCount, 0); unicodeBlockNames = new ArrayList&lt;Character.UnicodeBlock&gt;(); unicodeScripts = new ArrayList&lt;Character.UnicodeScript&gt;(); } @Override protected Void doInBackground() throws Exception { for (int kk = 0; kk &lt; pS; kk++) { if (kk % 500 == 0) { progressBar.setValue(kk); } doForEveryCodePoint(kk); } progressBar.setValue(0); return null; } @Override public void done() { try { get(); output.setText("Done!"); } catch (Exception ex) { ex.printStackTrace(); output.setText("Bad: " + ex.getMessage()); } } private final void doForEveryCodePoint(final int codePoint) { Character.UnicodeBlock block = Character.UnicodeBlock.of(codePoint); if (block != null &amp;&amp; !unicodeBlockNames.contains(block)) { unicodeBlockNames.add(block); } Character.UnicodeScript us = Character.UnicodeScript.of(codePoint); if (us == null || us.toString() == null) { } else { if (!unicodeScripts.contains(us)) { unicodeScripts.add(us); } } // fonts - test for points in all 6 defined blocks. for (int ii = 0; ii &lt; fontNameArray.length; ii++) { Font f = new Font(fontNameArray[ii], Font.PLAIN, 16); if (f.canDisplay(codePoint)) { glyphCount[ii]++; } } } } public SwingWorkerUnicodeTest(String[] names) { JPanel gui = new JPanel(new BorderLayout()); gui.add(progressBar, BorderLayout.CENTER); gui.add(output, BorderLayout.PAGE_END); CodePointDetailWorker cpdw = new CodePointDetailWorker(names); cpdw.execute(); JOptionPane.showMessageDialog(null, gui); } public static void main(String[] args) { Runnable r = new Runnable() { @Override public void run() { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); String[] fontNames = ge.getAvailableFontFamilyNames(); new SwingWorkerUnicodeTest(fontNames); } }; // Swing GUIs should be created and updated on the EDT // http://docs.oracle.com/javase/tutorial/uiswing/concurrency SwingUtilities.invokeLater(r); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload