Aug 26

When specifying an email address using JavaMail you can not only specify actual email address of the person, but also their name if required. The InternetAddress class is used to represent an email address which includes support for specifying both the email address and the personal name.

There are two ways this can be done, firstly the RFC822 address syntax can be used to specify both in one string.

message.setFrom(new InternetAddress("Joe Smith <joe@acme.com>"));

Or alternatively a constructor is available to specify the two separately

message.setFrom(new InternetAddress("joe@acme.com", "Joe Smith"));

written by objects \\ tags: , ,

Aug 25

Typically String.split() or StringTokenizer class is user to break up a string into tokens. Problem with these methods is that they do not handle quoted text as you may require.

For example, consider the following string:

The mans name was "Big Fred"

Using split() or StringTokenizer on this would give us 6 tokens: (The) (mans) (name) (was) (“Big) (Fred”). Typically this is not what we want.

This is where the StreamTokenizer class comes in handy as it gives better control over the parsing process including identifying quoted text. The following example shows its usage:

String s = "The mans name was \"Big Fred\"";
StreamTokenizer st = new StreamTokenizer(new StringReader(s));
st.quoteChar('"');
while (st.nextToken() != StreamTokenizer.TT_EOF) {
     System.out.println(st.sval);
}

Now we get the 5 tokens as required: (The) (mans) (name) (was) (Big Fred)

written by objects \\ tags: , , , ,

Aug 13
		myTable.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				JTable table = (JTable) e.getSource();
				int column = table.columnAtPoint(e.getPoint());
				int row = table.rowAtPoint(e.getPoint());
				
				System.out.println("Column: "+column);
				System.out.println("Row: "+row);
			}
			
		});

written by objects \\ tags: ,