Donnerstag, 26. Juni 2008

Problems setting up a NIS Slave Server

When I first trying to start a NIS slave server in my network, it complained:

Can't enumerate maps from server.example.com. Please check that it is running.


Appearantly, ypinit failed to enumerate maps using yphelper. The source of the problem turned out to be the DNS configuration. server.example.com had several DNS entries. I had to specify the name that the reverse lookup of its IP returned and then ypinit worked.

Mittwoch, 18. Juni 2008

Cleaning up animated swing controls

In its swing library, Java provides a timer class that can be used to animate controls. For example, one could create a blinking label as follows:

class BlinkLabel extends JLabel {
private static final int BLINK_INTERVAL = 500;

public BlinkLabel(String text) {
super(text);
ActionListener listener =
new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(!isVisible());
}
};
Timer timer = new Timer(BLINK_INTERVAL, listener);
timer.start();
}
}


The problem with this approach is that the label will continue blinking even after its parent window has been disposed. Also, the label will never be garbage collected, since the timer still maintains a reference to it.

Here's the proper way to do it.

class BlinkLabel extends JLabel {
private static final int BLINK_INTERVAL = 500;

private Timer timer;
private ActionListener listener;

public BlinkLabel(String text) {
super(text);
listener =
new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(!isVisible());
}
};

}

public void addNotify() {
super.addNotify();
timer = new Timer(BLINK_INTERVAL, listener);
timer.start();
}

public void removeNotify() {
super.removeNotify();
timer.stop();
timer.removeActionListener(listener);
timer = null;
}
}