// // another example of the Java AWT, showing use of Scrollpane class. // see class definition and main method for details. // import java.awt.* ; import java.awt.event.* ; import java.io.* ; import java.util.* ; // // a ScrollExample object is a CloseableFrame containing a Scrollpane; // the Scrollpane in turn contains an n-by-1 array of Label objects // containing lines of text. the Scrollpane automatically provides // horizontal and vertical scrollbars if needed (depending on the // size of the window). // class ScrollExample extends CloseableFrame { ScrollExample(String[] textLines) { super("ScrollExample") ; setLayout(new BorderLayout()) ; // make a Label for each element of textLines. Panel p1 = new Panel(new GridLayout(0,1)) ; for (int i = 0 ; i < textLines.length ; i++) { Label l = new Label(textLines[i]) ; p1.add(l) ; } ScrollPane pane = new ScrollPane() ; pane.add(p1) ; add(pane, "Center") ; setSize(600, 400) ; show() ; } // // ---- main --------------------------------------------------- // // command-line arguments are file to read and display, and // (optionally) a number for expanding tabs (if not // specified, tabs are not expanded, with somewhat // odd-looking results). // (no claims are made about the efficiency of the code // used to convert the file into a String array -- // this is simply a quick way to provide a non-trivial // amount of text for the Scrollpane.) // public static void main(String[] args) { if (args.length < 1) { System.out.println("Arguments are filename " + "tab-expansion") ; System.exit(-1) ; } int tabSize = 0 ; if (args.length > 1) tabSize = Integer.parseInt(args[1]) ; String[] lines = new String[0] ; // array to pass to constructor try { // read file into a Vector (which can // grow as needed). BufferedReader br = new BufferedReader (new FileReader(args[0])) ; Vector sv = new Vector() ; String l = br.readLine() ; while (l != null) { sv.addElement(l) ; l = br.readLine() ; } br.close() ; // now that we know how many lines, copy // to array for constructor. lines = new String[sv.size()] ; for (int i = 0 ; i < lines.length ; i++) lines[i] = expandTabs(tabSize, (String) sv.elementAt(i)) ; } catch (IOException e) { System.out.println("Unable to read file " + args[0]) ; System.exit(-1) ; } if (lines.length == 0) { System.out.println("Empty file " + args[0]) ; System.exit(-1) ; } ScrollExample e = new ScrollExample(lines) ; } // returns result of expanding tab characters in "line". static String expandTabs(int tabSize, String line) { if (tabSize <= 0) return line ; StringBuffer sb = new StringBuffer() ; int scanPos = 0 ; while (scanPos < line.length()) { // scanPos = next position to scan for tab. // tabPos = position containing tab currently // being expanded. int tabPos = line.indexOf('\t', scanPos) ; if (tabPos < 0) { sb.append(line.substring(scanPos)) ; scanPos = line.length() ; } else { sb.append(line.substring(scanPos, tabPos)) ; int numSp = tabSize - (sb.length() % tabSize) ; for (int i = 0 ; i < numSp ; i++) sb.append(' ') ; scanPos = tabPos + 1 ; } } return sb.toString() ; } }