This issue presents tips, techniques, and sample code for the following topics:
Using Scrollpane to scroll GUI objects.
ScrollPane is a JDK 1.1 class that supports scrolling. Before JDK 1.1
was released you had to manage scroll bars in terms of capturing and
managing scrolling by yourself. ScrollPane handles these tasks
automatically for some common usages.
An example of using ScrollPane looks like this:
import java.awt.*;
class Box extends Component {
public Dimension getPreferredSize()
{
return new Dimension(300, 300);
}
public void paint(Graphics gr)
{
gr.setColor(Color.blue);
gr.drawLine(5, 5, 295, 5);
gr.drawLine(295, 5, 295, 295);
gr.drawLine(295, 295, 5, 295);
gr.drawLine(5, 295, 5, 5);
}
}
public class scroll {
public static void main(String args[])
{
Frame fr = new Frame("ScrollPane test");
ScrollPane pane = new ScrollPane();
pane.setSize(200, 200);
pane.add(new Box());
fr.add(pane);
fr.pack();
fr.setVisible(true);
Adjustable vadj = pane.getVAdjustable();
Adjustable hadj = pane.getHAdjustable();
vadj.setUnitIncrement(5);
hadj.setUnitIncrement(5);
}
}
In the example is a custom component named Box of size 300 x 300. The
component contains a blue outlined box drawn within it. An instance of
this component is added to a ScrollPane of size 200 x 200, which, in turn,
is added to the top-level frame. The ScrollPane object therefore is
smaller than the Box and displays only a portion of the box at one time.
ScrollPane supplies scroll bars automatically.
In this example, the vertical and horizontal increments for the scroll bar
are also set so that each click on the scroll bar scrolls by 5 units
instead of 1, which is the default.
Faster I/O using JDK 1.1 classes.
With JDK 1.0, a standard way of reading and writing text lines was to
use the DataInputStream class and the readLine method. JDK 1.1 offers
a faster way of performing line-oriented text I/O using the classes
FileReader, BufferedReader, FileWriter, and BufferedWriter.
This program gives a simple example of how these classes are used.
import java.io.*;
public class readwrite {
public static void main(String args[])
{
if (args.length != 2) {
System.err.println("usage: infile outfile\n");
System.exit(1);
}
String in_file = args[0];
String out_file = args[1];
try {
FileReader reader = new FileReader(in_file);
BufferedReader buf_reader =
new BufferedReader(reader);
FileWriter writer = new FileWriter(out_file);
BufferedWriter buf_writer =
new BufferedWriter(writer);
String ln = null;
while ((ln = buf_reader.readLine()) != null){
buf_writer.write(ln);
buf_writer.newLine();
}
buf_reader.close();
buf_writer.close();
}
catch (IOException e) {
System.err.println(e);
System.exit(1);
}
}
}
The program copies its input file to the output file. It runs
about twice as fast as with the JDK 1.0 method of copying. One
major reason for the difference in speed is because method call
overhead to read underlying characters is eliminated.
Note: this approach is oriented toward text lines
represented as Strings. Faster but more primitive I/O methods are
also available, such as FileInputStream.
Using class literals.
The Java language supports dynamic loading of classes. For example,
you can use:
Class c = Class.forName("java.lang.String");
to retrieve a Class object representing the class java.lang.String,
which is a special class used to represent classes. There is one
Class object for each loaded class known to the Java runtime system.
JDK 1.1 offers an alternative form of this feature that looks like the
following:
public class literal {
public static void main(String args[])
{
Class c1 = java.lang.String.class;
Class c2 = double.class;
Class c3 = Double.TYPE;
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
}
}
The output of this program is:
class java.lang.String
double
double
c1 in the program represents the loaded class java.lang.String, while c2
and c3 represent the primitive data type double. Double is a wrapper class
for double, and TYPE a special member within the class that represents its
underlying primitive type.
Using this approach, a class must be known at compile time (that is,
referred to via CLASSPATH), otherwise a compile error is generated.
|