This post originated from an RSS feed registered with Java Buzz
by Marc Logemann.
Original Post: Focus in J2SE -> oh boy
Feed Title: Marc's Java Blog
Feed URL: http://www.logemann.org/day/index_java.xml
Feed Description: Java related topics for all major areas. So you will see J2ME, J2SE and J2EE issues here.
There are a lot of nice, modular and intuitive parts within J2SE. Most of them seem to be well designed and easy to understand. But not with the Focus API in J2SE. First of all, everyday problems seem to generate complicated code. Did you ever wanted to set a default focus of a component inside a Window? Then face it, you have to write windowListeners and stuff like this:
// Create frame and three buttons
JFrame frame = new JFrame();
JButton component1 = new JButton("1");
JButton component2 = new JButton("2");
JButton component3 = new JButton("3");
// Set component with initial focus
// Must be done before the frame is made visible
InitialFocusSetter.setInitialFocus(frame, component2);
class InitialFocusSetter {
public static void setInitialFocus(Window w, Component c) {
w.addWindowListener(new FocusSetter(c));
}
public static class FocusSetter extends WindowAdapter {
Component initComp;
FocusSetter(Component c) {
initComp = c;
}
public void windowOpened(WindowEvent e) {
initComp.requestFocus();
// Since this listener is no longer needed, remove it
e.getWindow().removeWindowListener(this);
}
}
}
Perhaps i am still dreaming for a better world, but this is just too complicated for such a common task. I found some other crazy behaviors when working with requestFocus() under some circumstances which i cant easily demonstrate because of the space i would need for this. But for me, Focus API in Java is still braindamaged, and this even after a rework from Sun.