user1410223
user1410223

Reputation:

Change attributes of all objects of a certain type in Java

I have a class that includes javax.swing.JButton, javax.swing.JTextField

But, after the class is instantiated, I need to change the attributes only of certain type.

I was trying with this.getComponents() but, is returning null.

Here my code:

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package test;
    import java.awt.Component;
    import java.awt.Font;

    /**
     *
     * @author Usuario
     */
    public class NewJFrame extends javax.swing.JFrame {

        /**
         * Creates new form NewJFrame
         */
        public NewJFrame() {
          initComponents();
          Component[] component = this.getComponents();
          System.out.println("this.getName():"+this.getName());
          System.out.println("this.getClass().getName():"+this.getClass().getName());
          System.out.println("component.length:"+component.length);
          for(int i=0; i<component.length; i++) {
            System.out.println("--component[i].getName()"+component[i].getName());
            if (component[i] instanceof javax.swing.JTextField) {
              javax.swing.JTextField jtf = (javax.swing.JTextField)component[i];
              System.out.println(jtf.getName());
              jtf.setFont(new Font("Serif",Font.PLAIN, 36));
            }
          }
          UIManager.getLookAndFeelDefaults().put("TextField.font", new Font("Serif",Font.PLAIN, 36));
          //UIManager.getDefaults().put("TextField.font", new Font("Serif",Font.PLAIN, 36));
          //UIManager.put("TextField.font", new Font("Serif",Font.PLAIN, 36));
          //this.updateUI();  // Is not suitable for javax.swing.JFrame
          this.pack();
        }

        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {

            jtpBot = new javax.swing.JTabbedPane();
            jpOne = new javax.swing.JPanel();
            jpOneInner = new javax.swing.JPanel();
            jbFirst = new javax.swing.JButton();
            jtfOne = new javax.swing.JTextField();
            jpOnePlot = new javax.swing.JPanel();
            jpTwo = new javax.swing.JPanel();
            jpTwoInner = new javax.swing.JPanel();
            jbTwo = new javax.swing.JButton();
            jtfTwo = new javax.swing.JTextField();
            jpTwoPlot = new javax.swing.JPanel();
            jpThree = new javax.swing.JPanel();
            jpThreeInner = new javax.swing.JPanel();
            jbThree = new javax.swing.JButton();
            jtfThree = new javax.swing.JTextField();
            jpThreePlot = new javax.swing.JPanel();

            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setBackground(new java.awt.Color(255, 255, 255));

            jtpBot.setBackground(new java.awt.Color(0, 0, 0));
            jtpBot.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

            jpOne.setBackground(new java.awt.Color(0, 191, 191));

            jpOneInner.setBackground(new java.awt.Color(255, 0, 0));
            jpOneInner.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

            jbFirst.setText("First");
            jbFirst.setMaximumSize(new java.awt.Dimension(70, 23));
            jbFirst.setMinimumSize(new java.awt.Dimension(70, 23));
            jbFirst.setPreferredSize(new java.awt.Dimension(70, 23));

            jtfOne.setMaximumSize(new java.awt.Dimension(80, 20));
            jtfOne.setMinimumSize(new java.awt.Dimension(80, 20));
            jtfOne.setPreferredSize(new java.awt.Dimension(80, 20));

            jpOnePlot.setBackground(new java.awt.Color(191, 191, 191));
            jpOnePlot.setBorder(javax.swing.BorderFactory.createEtchedBorder());

            javax.swing.GroupLayout jpOnePlotLayout = new javax.swing.GroupLayout(jpOnePlot);
            jpOnePlot.setLayout(jpOnePlotLayout);
            jpOnePlotLayout.setHorizontalGroup(
                jpOnePlotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 181, Short.MAX_VALUE)
            );
            jpOnePlotLayout.setVerticalGroup(
                jpOnePlotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 0, Short.MAX_VALUE)
            );

            javax.swing.GroupLayout jpOneInnerLayout = new javax.swing.GroupLayout(jpOneInner);
            jpOneInner.setLayout(jpOneInnerLayout);
            jpOneInnerLayout.setHorizontalGroup(
                jpOneInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jpOneInnerLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jbFirst, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jtfOne, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jpOnePlot, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );
            jpOneInnerLayout.setVerticalGroup(
                jpOneInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jpOneInnerLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jpOneInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jpOneInnerLayout.createSequentialGroup()
                            .addGroup(jpOneInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jbFirst, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jtfOne, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(0, 117, Short.MAX_VALUE))
                        .addComponent(jpOnePlot, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addContainerGap())
            );

            javax.swing.GroupLayout jpOneLayout = new javax.swing.GroupLayout(jpOne);
            jpOne.setLayout(jpOneLayout);
            jpOneLayout.setHorizontalGroup(
                jpOneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jpOneLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jpOneInner, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );
            jpOneLayout.setVerticalGroup(
                jpOneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpOneLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jpOneInner, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );

            jtpBot.addTab("One", jpOne);

            jpTwo.setBackground(new java.awt.Color(191, 0, 191));

            jpTwoInner.setBackground(new java.awt.Color(0, 255, 0));
            jpTwoInner.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

            jbTwo.setText("Two");
            jbTwo.setMaximumSize(new java.awt.Dimension(70, 23));
            jbTwo.setMinimumSize(new java.awt.Dimension(70, 23));
            jbTwo.setPreferredSize(new java.awt.Dimension(70, 23));

            jtfTwo.setMaximumSize(new java.awt.Dimension(80, 20));
            jtfTwo.setMinimumSize(new java.awt.Dimension(80, 20));
            jtfTwo.setPreferredSize(new java.awt.Dimension(80, 20));

            jpTwoPlot.setBackground(new java.awt.Color(191, 191, 191));
            jpTwoPlot.setBorder(javax.swing.BorderFactory.createEtchedBorder());

            javax.swing.GroupLayout jpTwoPlotLayout = new javax.swing.GroupLayout(jpTwoPlot);
            jpTwoPlot.setLayout(jpTwoPlotLayout);
            jpTwoPlotLayout.setHorizontalGroup(
                jpTwoPlotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 181, Short.MAX_VALUE)
            );
            jpTwoPlotLayout.setVerticalGroup(
                jpTwoPlotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 0, Short.MAX_VALUE)
            );

            javax.swing.GroupLayout jpTwoInnerLayout = new javax.swing.GroupLayout(jpTwoInner);
            jpTwoInner.setLayout(jpTwoInnerLayout);
            jpTwoInnerLayout.setHorizontalGroup(
                jpTwoInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jpTwoInnerLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jbTwo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jtfTwo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jpTwoPlot, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );
            jpTwoInnerLayout.setVerticalGroup(
                jpTwoInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jpTwoInnerLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jpTwoInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jpTwoPlot, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jpTwoInnerLayout.createSequentialGroup()
                            .addGroup(jpTwoInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jbTwo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jtfTwo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(0, 117, Short.MAX_VALUE)))
                    .addContainerGap())
            );

            javax.swing.GroupLayout jpTwoLayout = new javax.swing.GroupLayout(jpTwo);
            jpTwo.setLayout(jpTwoLayout);
            jpTwoLayout.setHorizontalGroup(
                jpTwoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jpTwoLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jpTwoInner, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );
            jpTwoLayout.setVerticalGroup(
                jpTwoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpTwoLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jpTwoInner, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );

            jtpBot.addTab("Two", jpTwo);

            jpThree.setBackground(new java.awt.Color(191, 191, 0));

            jpThreeInner.setBackground(new java.awt.Color(0, 0, 255));
            jpThreeInner.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

            jbThree.setText("Three");
            jbThree.setMaximumSize(new java.awt.Dimension(70, 23));
            jbThree.setMinimumSize(new java.awt.Dimension(70, 23));
            jbThree.setPreferredSize(new java.awt.Dimension(70, 23));

            jtfThree.setMaximumSize(new java.awt.Dimension(80, 20));
            jtfThree.setMinimumSize(new java.awt.Dimension(80, 20));
            jtfThree.setPreferredSize(new java.awt.Dimension(80, 20));

            jpThreePlot.setBackground(new java.awt.Color(191, 191, 191));
            jpThreePlot.setBorder(javax.swing.BorderFactory.createEtchedBorder());

            javax.swing.GroupLayout jpThreePlotLayout = new javax.swing.GroupLayout(jpThreePlot);
            jpThreePlot.setLayout(jpThreePlotLayout);
            jpThreePlotLayout.setHorizontalGroup(
                jpThreePlotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 181, Short.MAX_VALUE)
            );
            jpThreePlotLayout.setVerticalGroup(
                jpThreePlotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 0, Short.MAX_VALUE)
            );

            javax.swing.GroupLayout jpThreeInnerLayout = new javax.swing.GroupLayout(jpThreeInner);
            jpThreeInner.setLayout(jpThreeInnerLayout);
            jpThreeInnerLayout.setHorizontalGroup(
                jpThreeInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jpThreeInnerLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jbThree, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jtfThree, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jpThreePlot, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );
            jpThreeInnerLayout.setVerticalGroup(
                jpThreeInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jpThreeInnerLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jpThreeInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jpThreePlot, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jpThreeInnerLayout.createSequentialGroup()
                            .addGroup(jpThreeInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jbThree, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jtfThree, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(0, 117, Short.MAX_VALUE)))
                    .addContainerGap())
            );

            javax.swing.GroupLayout jpThreeLayout = new javax.swing.GroupLayout(jpThree);
            jpThree.setLayout(jpThreeLayout);
            jpThreeLayout.setHorizontalGroup(
                jpThreeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jpThreeLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jpThreeInner, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );
            jpThreeLayout.setVerticalGroup(
                jpThreeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpThreeLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jpThreeInner, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );

            jtpBot.addTab("Three", jpThree);

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jtpBot)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(69, 69, 69)
                    .addComponent(jtpBot)
                    .addContainerGap())
            );

            pack();
        }// </editor-fold>                        

        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>

            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NewJFrame().setVisible(true);
                }
            });
        }

        boolean bCanIuseOne = false;
        boolean bCanIuseTwo = false;
        boolean bThreadFree = true;
        boolean bMouseFree = false;

        // Variables declaration - do not modify                     
        private javax.swing.JButton jbFirst;
        private javax.swing.JButton jbThree;
        private javax.swing.JButton jbTwo;
        private javax.swing.JPanel jpOne;
        private javax.swing.JPanel jpOneInner;
        private javax.swing.JPanel jpOnePlot;
        private javax.swing.JPanel jpThree;
        private javax.swing.JPanel jpThreeInner;
        private javax.swing.JPanel jpThreePlot;
        private javax.swing.JPanel jpTwo;
        private javax.swing.JPanel jpTwoInner;
        private javax.swing.JPanel jpTwoPlot;
        private javax.swing.JTextField jtfOne;
        private javax.swing.JTextField jtfThree;
        private javax.swing.JTextField jtfTwo;
        private javax.swing.JTabbedPane jtpBot;
        // End of variables declaration                   
    }

Output:

    run:
    this.getName():frame0
    this.getClass().getName():test.NewJFrame
    component.length:1
    --component[i].getName()null
    BUILD SUCCESSFUL (total time: 19 seconds)

I want to obtain something like:

javax.swing.JTextField jtfOne;
javax.swing.JTextField jtfThree;
javax.swing.JTextField jtfTwo;
javax.swing.JButton jbFirst;
javax.swing.JButton jbThree;
javax.swing.JButton jbTwo;

How?

PD: I'm working with Netbeans...

EDIT 1: The VGR's suggestion was attempted

EDIT 2: The Solution by WillShackleford, Thank you!!!:

public NewJFrame() {
  Color ACTIVE_COLOUR = Color.DARK_GRAY;
  Color INACTIVE_COLOUR = Color.LIGHT_GRAY;
  Font theFont = new Font("Serif",Font.PLAIN, 11);
  UIManager.put("TextField.font", theFont);
  UIManager.put("Button.font", theFont);
  UIManager.put("ComboBox.font", theFont);
  //UIManager.put("TextField.background", new ColorUIResource(ACTIVE_COLOUR));
  UIManager.put("TextField.background", ACTIVE_COLOUR);
  UIManager.put("TextField.foreground", INACTIVE_COLOUR);

  initComponents();

  List<Component> l = new ArrayList<>();
  addComponents(this, l);
  for(int i = 0; i < l.size(); i++) {
    if (l.get(i) instanceof javax.swing.JTextField) {
      javax.swing.JTextField jtf = (javax.swing.JTextField)l.get(i);
      jtf.setFont(new Font("Serif",Font.PLAIN, 24));
      jtf.setPreferredSize(new Dimension(jtf.getWidth(), jtf.getHeight()+10));
      System.out.println("JTextField:"+", Width:"+jtf.getWidth()+", Height:"+jtf.getHeight());
    }
    if (l.get(i) instanceof javax.swing.JButton) {
      javax.swing.JButton jb = (javax.swing.JButton)l.get(i);
      jb.setPreferredSize(new Dimension(jb.getWidth(), jb.getHeight()+10));
      System.out.println("JButton:"+", Width:"+jb.getWidth()+", Height:"+jb.getHeight());
    }
  }
}

public void addComponents(Container c, List<Component> l) {
  Component ca[] = c.getComponents();
  l.addAll(Arrays.asList(ca));
  for (int i = 0; i < ca.length; i++) {
    Component component = ca[i];
    if (Container.class.isAssignableFrom(component.getClass())) {
        addComponents((Container) component, l);
    }
  }
}

Upvotes: 0

Views: 596

Answers (1)

WillShackleford
WillShackleford

Reputation: 7018

The GUI editor in Netbeans makes it quite easy to select any set of components and change all their properties in one step.

  1. Go to the Design view for the Frame or Panel.
  2. Select all the components you want to change. This can be done either with a graphical selection rectangle in the editor window or with in the Navigator window with a tree view in the lower left. In the navigator window you can see the names and types to quickly select all of a given type. Normal selection rules apply. Hold-shift to select all components between that last selected and the next click or hold control to add/subtract one element from the set.
  3. Right-click for the pop-up menu and choose properties.
  4. With multiple components selected only properties they have in common will be visible.
  5. Change any property you want and all selected components will have that property set.

If you still want a list of all components this seems to work for me:

public NewJFrame() {
    initComponents();
    List<Component> l = new ArrayList<>();
    addComponents(this, l);
    System.out.println("l = " + l);
}

public void addComponents(Container c, List<Component> l) {
    Component ca[] = c.getComponents();
    l.addAll(Arrays.asList(ca));
    for (int i = 0; i < ca.length; i++) {
        Component component = ca[i];
        if (Container.class.isAssignableFrom(component.getClass())) {
            addComponents((Container) component, l);
        }
    }
}

Upvotes: 2

Related Questions