Java Bytes
This section of the site is for displaying short and (hopefully) helpful Java snippets.
Universal Integer Reader/Writer
Submitted by jhallida on Thu, 08/12/2010 - 22:45There are a wide variety of ways to read and write integers to a file. This code fragment enhances Java's RandomAccessFile to encompass a wider variety of methods, including 1, 2 and 4 byte integers, and both signed and unsigned values.
Show a number in hex form
Submitted by jhallida on Sat, 05/08/2010 - 22:14In Java, the 'toHexString' method will convert a number into a string that represents that number in hex form. However, this string is not usually formatted in the way that most people would expect - it does not begin with '0x' and does not contain leading zeros. Here is a function that does just that.
Display a reasonable indicator of file size
Submitted by jhallida on Wed, 04/21/2010 - 12:48By default, a Java indicator of file size will tell you the number of bytes, but of course a number such as 1042354 isn't exactly user-friendly. Here's a small method to return a human-readable string indication of a number of bytes.
Making a JLabel with selectable text
Submitted by jhallida on Thu, 04/15/2010 - 22:07A Java JLabel works pretty well, but anyone who has used them is well aware that the user cannot select the text inside of them for copying and pasting. For certain applications, such as applications that display checksums, this can make manually copying the text quite tedious.
Here we will make something that looks and acts like a JLabel, but with fully selectable text. The 'trick' is that in actuality, the component is a JTextField, but we have modified its appearance to imitate a JLabel:
Here we will make something that looks and acts like a JLabel, but with fully selectable text. The 'trick' is that in actuality, the component is a JTextField, but we have modified its appearance to imitate a JLabel:
