/* * File: ShowFile.java * * * This Java applet divides the applet area into two parts: an area * with controls such as buttons and choices, called controlPanelNorth, * and an area called dataPanel which has a TextArea to display * data from a file. The dataPanel is a separate class. * * The data points are read from a file which has the following format: * an integer which is the number of items in the file * items: each item has an integer id no., a name, and a floating * point price * * The data is read once from the file into a set of arrays. Then the * data can be displayed in different ways depending on the controls. * */ import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; public class ShowFile extends java.applet.Applet implements ItemListener, ActionListener { Font f = new Font("TimesRoman", Font.ITALIC, 18); DisplayPanel dataPanel; // the display panel, from the class below String inname; // name of the data file Choice dataChoice; // control for type of data Button showfile; // display the data under current controls Label title; // label of the display public void init() { InputStream instream = null; Panel controlPanelNorth = new Panel(); setLayout(new BorderLayout()); // initialize the input stream and data panel try { inname = getParameter("inputfile"); instream = new URL(getDocumentBase(), inname).openStream(); dataPanel = new DisplayPanel(instream); } catch (IOException e) { System.err.println ("I/O Exception: probably bad filename or bad connection." + e.getMessage()); } // make the control panel controlPanelNorth.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5)); title = new Label("Grocery Items"); title.setFont(f); dataChoice = new Choice(); dataChoice.setFont(f); dataChoice.addItem("Fruits"); dataChoice.addItem("Veggies"); dataChoice.addItemListener(this); showfile = new Button("Show Items"); showfile.setFont(f); showfile.addActionListener(this); controlPanelNorth.add(title); controlPanelNorth.add(dataChoice); controlPanelNorth.add(showfile); // add the two components to the applet area add(controlPanelNorth, BorderLayout.NORTH); add(dataPanel, BorderLayout.CENTER); } public void itemStateChanged(ItemEvent e) { if (e.getSource() == dataChoice) { dataPanel.settype (dataChoice.getSelectedItem()); } } public void actionPerformed(ActionEvent e) { if (e.getSource() == showfile) { dataPanel.displayfile(); } } } class DisplayPanel extends Panel { String datatype = "Fruits"; // either "Fruits" or "Veggies" StringBuffer buff; Font f = new Font("Monospaced", Font.ITALIC, 18); TextArea ta; // variables to hold the data from the file int numdata; int itemnumbers[]; String itemnames[]; double itemprices[]; int maxlength; InputStreamReader isr; public DisplayPanel(InputStream instream) { setLayout(new BorderLayout()); ta = new TextArea("Grocery Item Display Area . . .", 20, 30); add( ta, BorderLayout.CENTER ); try { isr = new InputStreamReader(instream); StreamTokenizer intokens = new StreamTokenizer(isr); intokens.parseNumbers(); intokens.slashSlashComments(true); /* read the number of data points and allocate data arrays */ int tok = intokens.nextToken(); numdata = (int) intokens.nval; itemnumbers = new int[numdata]; itemnames = new String[numdata]; itemprices = new double[numdata]; // read data items maxlength = 0; for (int i=0; i1000) appendtext(vegcolor, itemnumbers[i], itemnames[i], itemprices[i]); } ta.setText(buff.toString()); } public void appendtext (Color c, int n, String s, double d) { ta.setForeground (c); int diff = maxlength - s.length(); buff.append( n + " " + s); for (int i=0; i