Archive for the ‘Tip’ Category

How do I get a JScrollPane to always display scroll bars?

Java, Swing, Tip 6.11.2015 No Comments

You can achieve this by setting the scroll bar policy on both the verical and horizontal scroll bars.

Read More

How to copy a file using Java 6

Java, Tip 10.7.2015 No Comments

Prior to Java 7, Java did not provide a standard method to copy a file. To implement copying you needed to read all bytes from source file and write to destination. The read() method will return -1 when eof is reached, otherwise it returns the number of bytes read.

Read More

How do add a checkbox to items in a JList?

Java, Swing, Tip 30.6.2015 1 Comment

We often get asked about how to implement a list of checkboxes using Swing. Using a JList filled with JCheckbox’s seems the obvious solution, however JList does not support cell editors so this does not work. One possible solutions is to use a single column JTable and store boolean’s as the cell value (the default…

Read More

How to check if a Java String is an integer?

Java, Tip 5.6.2015 2 Comments

The Integer class has a number of static methods for parsing strings. For the case where we want to check if if a string contains a valid integer we can use the method Integer.parseInt() and catch the exception that is thrown when the number cannot be parsed.

Read More

Using Scanner to read words from text file

Java, Tip 20.11.2014 1 Comment

Often you need to read a file line by line. Alternatively sometimes you want to read text word by word (for example to count the occurrence of different words). The Scanner classes next() method can be used for this as shown in the following example. You can find an example of the use of the…

Read More

How to specify log4j configuration in spring application?

Java, Spring, Tip 6.5.2014 No Comments

If you do not want to store your log4j configuration in your classes directory then you need to tell log4j where it can find it. One possibility is to configure it in your applicationContext.xml as shown here If you are running your web application as an expanded war then another option is to use a…

Read More

How do I read a text file line by line?

Java, Tip 20.11.2013 No Comments

The readLine() method of java.io.BufferedReader class reads the next line from a text file. When it reaches the end of the file it will return null.

Read More
s2Member®