Archive | June, 2006

Event Queuing to get focus

15 Jun

You might need to do something like this if you want a component to get Focus but it has not been drawn yet (you can Focus on an undrawn/invisible component)

 

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Component compFocusOwner =
                      KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();

                  // null is returned if none of the windows in this application has the focus
                  Window windowFocusOwner =
                      KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
                    requestFocusInWindow();
            }
        });

Advertisements

Java

14 Jun

If you make an ArrayList of a certain type of Object and then try to convert it back to the Object type, you can run into ClassCast Exceptions.

 e.g. 

ArrayList  test = new ArrayList();

MyObject mine = new MyObject;

test.add(mine);

 To get the array of the MyObjects you need to do:

(MyObject[]) test.toArray(new MyObject[test.size()])