import javax.imageio.*; import java.awt.image.*; import java.awt.event.*; import java.awt.*; import java.security.spec.*; import java.security.*; import java.lang.management.*; import java.lang.ref.*; import java.lang.reflect.*; import java.net.*; import java.io.*; import javax.swing.table.*; import javax.swing.text.*; import javax.swing.event.*; import javax.swing.*; import java.util.concurrent.atomic.*; import java.util.concurrent.*; import java.util.regex.*; import java.util.List; import java.util.zip.*; import java.util.*; public class main { public static void main(String[] args) throws Exception { SwingUtilities.invokeLater(new Runnable() { public void run() { try { List frames = listAllFrames(); print(frames.size() + " frame(s) found."); List l = allSwingComponentsOfType(JList.class); print(l.size() + " list(s) found."); for (JList list : l) { if (!list.isShowing()) { print("Skipping list, not showing."); continue; } ListModel model = list.getModel(); print("List entries: " + model.getSize()); String myTag = "Kilroy was here! :)"; if (!(model instanceof DefaultListModel)) { //print("Cannot modify, not a DefaultListModel (" + model.getClass().getName() + ")"); convertToDefaultListModel(list); model = list.getModel(); } if (model.getSize() != 0 && eq(myTag, model.getElementAt(model.getSize()-1))) print("Kilroy was here already :)"); else { print("Trying to add a string (yes it may fail if it's not a string list) :)"); DefaultListModel m = (DefaultListModel) ( model); m.addElement(myTag); } } } catch (Exception _e) { throw _e instanceof RuntimeException ? (RuntimeException) _e : new RuntimeException(_e); } } }); } static void convertToDefaultListModel(JList list) { ListModel model = list.getModel(); DefaultListModel m = new DefaultListModel(); for (int i = 0; i < model.getSize(); i++) m.addElement(model.getElementAt(i)); list.setModel(m); } static List allSwingComponentsOfType(Class theClass) { List frames = listAllFrames(); List l = new ArrayList(); for (JFrame f : frames) scanForComponents(f, theClass, l); return l; } static int l(Object[] array) { return array == null ? 0 : array.length; } static int l(List list) { return list == null ? 0 : list.size(); } static int l(String s) { return s == null ? 0 : s.length(); } static List listAllFrames() { List l = new ArrayList(); for (Frame f : Frame.getFrames()) if (f instanceof JFrame) l.add((JFrame) f); return l; } static boolean eq(Object a, Object b) { return a == null ? b == null : a.equals(b); } static void print() { System.out.println(); } static void print(Object o) { System.out.println(o); } static void print(long i) { System.out.println(i); } static void scanForComponents(Component c, Class theClass, List l) { if (theClass.isInstance(c)) l.add((A) c); if (c instanceof Container) for (Component comp : ((Container) c).getComponents()) scanForComponents(comp, theClass, l); } }