Archive: Iphone

How to Use HTML in Swing Components

Many Swing components display a text string as part of their GUI. By default, a component’s text is displayed in a single font and color, all on one line. You can determine the font and color of a component’s text by invoking the component’s setFont and setForeground methods, respectively. For example, the following code creates a label and then sets its font and color:

label = new JLabel("A label");
label.setFont(new Font("Serif", Font.PLAIN, 14));
label.setForeground(new Color(0xffffdd));

If you want to mix fonts or colors within the text, or if you want formatting such as multiple lines, you can use HTML. HTML formatting can be used in all Swing buttons, menu items, labels, tool tips, and tabbed panes, as well as in components such as trees and tables that use labels to render text.

To specify that a component’s text has HTML formatting, just put the <html> tag at the beginning of the text, then use any valid HTML in the remainder. Here is an example of using HTML in a button’s text:

button = new JButton("<html><b><u>T</u>wo</b><br>lines</html>");
Here is the resulting button

An Example: HtmlDemo

An application called HtmlDemo lets you play with HTML formatting by setting the text on a label. You can find the entire code for this program in HtmlDemo.java. Here is a picture of the HtmlDemo example.


 

How to Change the File Date and Time in Java using Commons IO

This example shows how to change / update the date and time of a file. This is done thought the FileUtils class which is part of the Apache Commons IO library.
We can get the actual file time by using the core java.io.File class and calling its lastModified() method which returns the time as a datatype long.
The value is the number of milliseconds elapsed since January 1, 1970. To update the time we use the static touch() method of the FileUtils class which will open and close the file without modifying it, but update the file date and time.
Note that if the file that we are trying to update the time for doesn’t exist, it will be created through a call to the touch() method.

The example gets the timestamp for a certain file, calls touch and then gets the timestamp again and print out whether the second timestamp was larger than (i.e. after) the first timestamp.

package com.javadb.examples;

import java.io.IOException;
import org.apache.commons.io.FileUtils;

import java.io.File;

/**
*
* @author www.javadb.com
*/
public class Main {

public static void main(String[] args) {
try {
File file = new File(“pic.jpg”);
long lastModified1 = file.lastModified();
FileUtils.touch(file);
long lastModified2 = file.lastModified();
System.out.println(“File date / time was updated: “ + (lastModified2 > lastModified1));
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

The output from above is naturally:
File date / time was updated: true

 

 

Back to Top
UA-37233306-1