{"id":10423,"date":"2015-08-28T00:14:03","date_gmt":"2015-08-28T04:14:03","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/?p=10423"},"modified":"2015-08-24T10:05:45","modified_gmt":"2015-08-24T14:05:45","slug":"an-applet-that-reads-arrays-of-strings-packaged-inside-a-querycollection-and-places-them-in-a-scrolling-textarea","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=10423","title":{"rendered":"An applet that reads arrays of strings packaged inside a QueryCollection and places them in a scrolling TextArea."},"content":{"rendered":"<pre>import java.applet.Applet;\r\nimport java.awt.*;\r\nimport java.awt.event.*;\r\nimport java.net.*;\r\n\r\n\/** Applet reads arrays of strings packaged inside\r\n\u00a0*\u00a0 a QueryCollection and places them in a scrolling\r\n\u00a0*\u00a0 TextArea. The QueryCollection obtains the strings\r\n\u00a0*\u00a0 by means of a serialized object input stream\r\n\u00a0*\u00a0 connected to the QueryGenerator servlet.\r\n\u00a0* \u00a0\r\n\r\n\r\n\u00a0*\u00a0 Taken from Core Web Programming Java 2 Edition\r\n\u00a0*\u00a0 from Prentice Hall and Sun Microsystems Press,\r\n\u00a0 *\u00a0 May be freely used or adapted.\r\n\u00a0*\/\r\n\r\npublic class ShowQueries extends Applet\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 implements ActionListener, Runnable {\r\n\u00a0 private TextArea queryArea;\r\n\u00a0 private Button startButton, stopButton, clearButton;\r\n\u00a0 private QueryCollection currentQueries;\r\n\u00a0 private QueryCollection nextQueries;\r\n\u00a0 private boolean isRunning = false;\r\n\u00a0 private String address =\r\n\u00a0\u00a0\u00a0 \"\/servlet\/cwp.QueryGenerator\";\r\n\u00a0 private URL currentPage;\r\n\u00a0 \r\n\u00a0 public void init() {\r\n\u00a0\u00a0\u00a0 setBackground(Color.white);\r\n\u00a0\u00a0\u00a0 setLayout(new BorderLayout());\r\n\u00a0\u00a0\u00a0 queryArea = new TextArea();\r\n\u00a0\u00a0\u00a0 queryArea.setFont(new Font(\"Serif\", Font.PLAIN, 14));\r\n\u00a0\u00a0\u00a0 add(queryArea, BorderLayout.CENTER);\r\n\u00a0\u00a0\u00a0 Panel buttonPanel = new Panel();\r\n\u00a0\u00a0\u00a0 Font buttonFont = new Font(\"SansSerif\", Font.BOLD, 16);\r\n\u00a0\u00a0\u00a0 startButton = new Button(\"Start\");\r\n\u00a0\u00a0\u00a0 startButton.setFont(buttonFont);\r\n\u00a0\u00a0\u00a0 startButton.addActionListener(this);\r\n\u00a0\u00a0\u00a0 buttonPanel.add(startButton);\r\n\u00a0\u00a0\u00a0 stopButton = new Button(\"Stop\");\r\n\u00a0\u00a0\u00a0 stopButton.setFont(buttonFont);\r\n\u00a0\u00a0\u00a0 stopButton.addActionListener(this);\r\n\u00a0\u00a0\u00a0 buttonPanel.add(stopButton);\r\n\u00a0\u00a0\u00a0 clearButton = new Button(\"Clear TextArea\");\r\n\u00a0\u00a0\u00a0 clearButton.setFont(buttonFont);\r\n\u00a0\u00a0\u00a0 clearButton.addActionListener(this);\r\n\u00a0\u00a0\u00a0 buttonPanel.add(clearButton);\r\n\u00a0\u00a0\u00a0 add(buttonPanel, BorderLayout.SOUTH);\r\n\u00a0\u00a0\u00a0 currentPage = getCodeBase();\r\n\u00a0\u00a0\u00a0 \/\/ Request a set of sample queries. They\r\n\u00a0\u00a0\u00a0 \/\/ are loaded in a background thread, and\r\n\u00a0\u00a0\u00a0 \/\/ the applet checks to see if they have finished\r\n\u00a0\u00a0\u00a0 \/\/ loading before trying to extract the strings.\r\n\u00a0\u00a0\u00a0 currentQueries = new QueryCollection(address, currentPage);\r\n\u00a0\u00a0\u00a0 nextQueries = new QueryCollection(address, currentPage);\r\n\u00a0 }\r\n\r\n\u00a0 \/** If you press the \"Start\" button, the system\r\n\u00a0\u00a0 *\u00a0 starts a background thread that displays\r\n\u00a0\u00a0 *\u00a0 the queries in the TextArea. Pressing \"Stop\"\r\n\u00a0\u00a0 *\u00a0 halts the process, and \"Clear\" empties the\r\n\u00a0\u00a0 *\u00a0 TextArea.\r\n\u00a0\u00a0 *\/\r\n\u00a0\r\n\u00a0 public void actionPerformed(ActionEvent event) {\r\n\u00a0\u00a0\u00a0 if (event.getSource() == startButton) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 if (!isRunning) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Thread queryDisplayer = new Thread(this);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 isRunning = true;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 queryArea.setText(\"\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 queryDisplayer.start();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 showStatus(\"Started display thread...\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 } else {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 showStatus(\"Display thread already running...\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 } else if (event.getSource() == stopButton) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 isRunning = false;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 showStatus(\"Stopped display thread...\");\r\n\u00a0\u00a0\u00a0 } else if (event.getSource() == clearButton) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 queryArea.setText(\"\");\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0 }\r\n\r\n\u00a0 \/** The background thread takes the currentQueries\r\n\u00a0\u00a0 *\u00a0 object and every half-second places one of the queries\r\n\u00a0\u00a0 *\u00a0 the object holds into the bottom of the TextArea. When\r\n\u00a0\u00a0 *\u00a0 all of the queries have been shown, the thread copies\r\n\u00a0\u00a0 *\u00a0 the value of the nextQueries object into\r\n\u00a0\u00a0 *\u00a0 currentQueries, sends a new request to the server\r\n\u00a0\u00a0 *\u00a0 in order to repopulate nextQueries, and repeats\r\n\u00a0\u00a0 *\u00a0 the process.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public void run() {\r\n\u00a0\u00a0\u00a0 while(isRunning) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 showQueries(currentQueries);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 currentQueries = nextQueries;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 nextQueries = new QueryCollection(address, currentPage);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0 }\r\n\r\n\u00a0 private void showQueries(QueryCollection queryEntry) {\r\n\u00a0\u00a0\u00a0 \/\/ If request has been sent to server but the result\r\n\u00a0\u00a0\u00a0 \/\/ isn't back yet, poll every second. This should\r\n\u00a0\u00a0\u00a0 \/\/ happen rarely but is possible with a slow network\r\n\u00a0\u00a0\u00a0 \/\/ connection or an overloaded server.\r\n\u00a0\u00a0\u00a0 while(!queryEntry.isDone()) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 showStatus(\"Waiting for data from server...\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 pause(1);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 showStatus(\"Received data from server...\");\r\n\u00a0\u00a0\u00a0 String[] queries = queryEntry.getQueries();\r\n\u00a0\u00a0\u00a0 String linefeed = \"\\n\";\r\n\u00a0\u00a0\u00a0 \/\/ Put a string into TextArea every half-second.\r\n\u00a0\u00a0\u00a0 for(int i=0; i\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.net.*; \/** Applet reads arrays of strings packaged inside \u00a0*\u00a0 a QueryCollection and places them in a scrolling \u00a0*\u00a0 TextArea. The QueryCollection obtains the strings \u00a0*\u00a0 by means of a serialized object input stream \u00a0*\u00a0 connected to the QueryGenerator servlet. \u00a0* \u00a0 \u00a0*\u00a0 Taken from Core Web Programming &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=10423\">Continue reading<\/a><\/p>\n","protected":false},"author":130,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1417,1424],"tags":[706,308,285],"class_list":["post-10423","post","type-post","status-publish","format-standard","hentry","category-code-programming-samples--","category-javaj2eej2me","tag-code","tag-java","tag-285","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":27062,"url":"http:\/\/bangla.sitestree.com\/?p=27062","url_meta":{"origin":10423,"position":0},"title":"An applet that reads arrays of strings packaged inside a QueryCollection and places them in a scrolling TextArea. #Programming Code Examples #Java\/J2EE\/J2ME #Applets and Basic Graphics","author":"Author-Check- Article-or-Video","date":"May 9, 2021","format":false,"excerpt":"import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.net.*; \/** Applet reads arrays of strings packaged inside * a QueryCollection and places them in a scrolling * TextArea. The QueryCollection obtains the strings * by means of a serialized object input stream * connected to the QueryGenerator servlet. * * Taken\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10425,"url":"http:\/\/bangla.sitestree.com\/?p=10425","url_meta":{"origin":10423,"position":1},"title":"The class that actually gets the strings over the network by means of an ObjectInputStream via HTTP tunneling.","author":"","date":"August 28, 2015","format":false,"excerpt":"import java.net.*; import java.io.*; \/** When this class is built, it returns a value \u00a0*\u00a0 immediately, but this value returns false for isDone \u00a0*\u00a0 and null for getQueries. Meanwhile, it starts a Thread \u00a0*\u00a0 to request an array of query strings from the server, \u00a0*\u00a0 reading them in one fell\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27064,"url":"http:\/\/bangla.sitestree.com\/?p=27064","url_meta":{"origin":10423,"position":2},"title":"The class that actually gets the strings over the network by means of an ObjectInputStream via HTTP tunneling. #Programming Code Examples #Java\/J2EE\/J2ME #Applets and Basic Graphics","author":"Author-Check- Article-or-Video","date":"May 9, 2021","format":false,"excerpt":"import java.net.*; import java.io.*; \/** When this class is built, it returns a value * immediately, but this value returns false for isDone * and null for getQueries. Meanwhile, it starts a Thread * to request an array of query strings from the server, * reading them in one fell\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10308,"url":"http:\/\/bangla.sitestree.com\/?p=10308","url_meta":{"origin":10423,"position":3},"title":"Message.java Applet that reads customization parameters from an HTML file","author":"","date":"August 26, 2015","format":false,"excerpt":"******************* Message.java Applet that reads customization parameters from an HTML file ******************* import java.applet.Applet; import java.awt.*; **************** \u00a0 public class Message extends Applet { \u00a0 private int fontSize; \u00a0 private String message; \u00a0 \u00a0 public void init() { \u00a0\u00a0\u00a0 setBackground(Color.black); \u00a0\u00a0\u00a0 setForeground(Color.white); \u00a0\u00a0 \u00a0 \u00a0\u00a0\u00a0 \/\/ Base font size on\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27249,"url":"http:\/\/bangla.sitestree.com\/?p=27249","url_meta":{"origin":10423,"position":4},"title":"Message.java Applet that reads customization parameters from an HTML file #Programming Code Examples #Java\/J2EE\/J2ME #Advanced Swing","author":"Author-Check- Article-or-Video","date":"May 15, 2021","format":false,"excerpt":"******************* Message.java Applet that reads customization parameters from an HTML file ******************* import java.applet.Applet; import java.awt.*; **************** public class Message extends Applet { private int fontSize; private String message; public void init() { setBackground(Color.black); setForeground(Color.white); \/\/ Base font size on window height. fontSize = getSize().height - 10; setFont(new Font(\"SansSerif\", Font.BOLD,\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10361,"url":"http:\/\/bangla.sitestree.com\/?p=10361","url_meta":{"origin":10423,"position":5},"title":"TextAreas","author":"","date":"August 27, 2015","format":false,"excerpt":"TextAreas.java ************** import java.applet.Applet; import java.awt.*; \/.\/.\/.\/.\/.\/.\/,\/.\/.\/.\/.\/ public class TextAreas extends Applet { \u00a0 public void init() { \u00a0\u00a0\u00a0 setBackground(Color.lightGray); \u00a0\u00a0\u00a0 add(new TextArea(3, 10)); \u00a0\u00a0\u00a0 add(new TextArea(\"Some\\nInitial\\nText\", 3, 10)); \u00a0 } }","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/130"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10423"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10423\/revisions"}],"predecessor-version":[{"id":10424,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10423\/revisions\/10424"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10423"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}