diff --git a/out/production/HierarchyComponents/hswing/HFrame.class b/out/production/HierarchyComponents/hswing/HFrame.class index df58c33..6becf76 100644 Binary files a/out/production/HierarchyComponents/hswing/HFrame.class and b/out/production/HierarchyComponents/hswing/HFrame.class differ diff --git a/out/production/HierarchyComponents/hswing/Navigator.class b/out/production/HierarchyComponents/hswing/Navigator.class index 0e45230..2586ad6 100644 Binary files a/out/production/HierarchyComponents/hswing/Navigator.class and b/out/production/HierarchyComponents/hswing/Navigator.class differ diff --git a/src/demo/NavigatorExample.java b/src/demo/NavigatorExample.java index aff7c31..47f69dd 100644 --- a/src/demo/NavigatorExample.java +++ b/src/demo/NavigatorExample.java @@ -22,17 +22,24 @@ public static void main(String[] args) { )) ).getNavigator(); + //this would throw a MoreThanOneComponentException + //navigator.getSingle("label"); + + //returns null because no components have the name 'label' and the class of a JButton + navigator.getSingleByClass("label", JButton.class); + //gets the one 'button1' with the class 'JButton' (automatically casts it from HButton -> JButton) navigator.getSingleByClass("button1", JButton.class).addActionListener(e -> { - //gets all the JLabels - navigator.getListByClass(JLabel.class).forEach(jLabel -> { + //gets all the JLabels with name 'label' + navigator.getListByClass("label", JLabel.class).forEach(jLabel -> { jLabel.setText(jLabel.getText().repeat(2)); }); + navigator.getFrame().pack(); }); - //getting a list based on just the class + //getting a list of JComponents based on just the class navigator.getListByClass(JLabel.class).forEach(label -> { - label.setBackground(Color.blue); + label.setBorder(BorderFactory.createLineBorder(Color.blue, 5, true)); }); AtomicInteger aint = new AtomicInteger(10); diff --git a/src/hswing/Navigator.java b/src/hswing/Navigator.java index 7c4a900..c419329 100644 --- a/src/hswing/Navigator.java +++ b/src/hswing/Navigator.java @@ -62,11 +62,11 @@ public JComponent getSingle(String name) throws MoreThanOneComponentException { /** * Gets a single T according to the name - * @param name the name of the {@link JComponent} to get - * @param t the class to automatically cast it to + * @param name the name of the T to get + * @param t the class to find and return * @param the class type to be returned - * @return the {@link JComponent} with the name. If the {@link JComponent} does not exist, it will return null - * @throws MoreThanOneComponentException if there are multiple {@link JComponent}s with the same name + * @return the T with the name. If the T does not exist, it will return null + * @throws MoreThanOneComponentException if there are multiple Ts with the same name */ public T getSingleByClass(String name, Class t) throws MoreThanOneComponentException { List componentsWithName = components.get(name); @@ -79,6 +79,9 @@ public T getSingleByClass(String name, Class t) throws MoreThanOneCompone if (componentsWithName.size() == 1) { return (T)componentsWithName.get(0); } + if (componentsWithName.size() == 0) { + return null; + } throw new MoreThanOneComponentException(name); }