Log teknis programmer Java

Selasa, 05 Mei 2009

UI manager Look and Feel sama dengan sistem operasi

       try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
ex.printStackTrace();
}

Properties Factory buat nyimpen setingan file

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PropertyFactory {

public static void save(Properties p, String fileName, String comment) {
PrintWriter pw = null;
try {
pw = new PrintWriter(fileName);
} catch (FileNotFoundException ex) {
Logger.getLogger(PropertyFactory.class.getName()).log(Level.SEVERE, null, ex);
}
try {
p.store(pw, comment);
} catch (IOException ex) {
Logger.getLogger(PropertyFactory.class.getName()).log(Level.SEVERE, null, ex);
}
}

public static Properties read(String fileName) {
Properties p = new Properties();
Reader r = null;
try {
r = new FileReader(fileName);
} catch (FileNotFoundException ex) {
Logger.getLogger(PropertyFactory.class.getName()).log(Level.SEVERE, null, ex);
}
try {
p.load(r);
} catch (IOException ex) {
Logger.getLogger(PropertyFactory.class.getName()).log(Level.SEVERE, null, ex);
}
return p;
}
}

Senin, 04 Mei 2009

List available port in java

import javax.comm.*;
import java.util.Enumeration;

public class ListPorts {

public static void main(String args[]) {
Enumeration ports = CommPortIdentifier.getPortIdentifiers();
while (ports.hasMoreElements()) {
CommPortIdentifier port = (CommPortIdentifier) ports.nextElement();
String type;
switch (port.getPortType()) {
case CommPortIdentifier.PORT_PARALLEL:
type = "Parallel";
break;
case CommPortIdentifier.PORT_SERIAL:
type = "Serial";
break;
default:
type = "Unknown";
break;
}
System.out.println(port.getName() + ": " + type);
}
}
}