The Random class provides a method that returns a random integer between 0 and n (exclusive).
Random wheel = new Random();
int random = wheel.nextInt(n);
By default it uses the current timestamp as the seed for the random generator, or alternately you can provide your own.
written by objects
\\ tags: int, random
Java provides the ZipInputStream class for reading from a zip file. Heres a general example of its usage
File file = new File("my.zip");
ZipInputStream zin = new ZipInputStream(new FileInputStream(file));
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
String filename = ze.getName();
if (!ze.isDirectory()) {
// Read file contents from 'zin'
// For example you could read from zin and write it to a FileOutputStream
// http://helpdesk.objects.com.au/java/how-do-i-copy-one-stream-to-another-using-java
}
zin.closeEntry();
}
zin.close();
The same can be used to read the contents of a jar file.
written by objects
\\ tags: decompress, file, stream, unzip, zip, ZipInputStream
When you add a JTextField to a BoxLayout managed panel it will expand vertically to take up available space.
To avoid this you can simply set the maximum size of the field to be the same as its preferred size.
textField.setMaximumSize( textField.getPreferredSize() );
written by objects
\\ tags: BoxLayout, JTextField, layout
Recent Comments