First of all, define the container that hosts the jformattedtextfield, and then simply use the JContainer API to move all the child components and filter all jformattedtextfield components using instanceof.
For instance:
public static int calcIfEnabled(Container container) { int finalResult = 0; for (Component c : container.getComponents()) { if (c instanceof JFormattedTextField && c.isEnabled() && ((JFormattedTextField) c).isEditable()) { finalResult += Integer.parseInt(((JFormattedTextField) c).getText()); } } return finalResult; }
UPD: Of course, you can enable the entire child component using recursion and pass in the main container (JFrame), but that would not be so good in terms of performance.
public static int calcIfEnabled(Container container) { int finalResult = 0; for (Component c : container.getComponents()) { if (c instanceof JFormattedTextField && c.isEnabled() && ((JFormattedTextField) c).isEditable()) { finalResult += Integer.parseInt(((JFormattedTextField) c).getText()); } else if (c instanceof Container) { finalResult += calcIfEnabled((Container) c); } } return finalResult; }
source share