Java get JPanel Components

52

Solution 1

Well bear in mind they didn't get there by them selves ( I think a read some questions about dynamically creating these panels at runtime )

In the answers posted there, someone said you should kept reference to those textfields in an array. That's exactly what you need here:

List<JTextField> list = new ArrayLists<JTextField>();

// your code...
for (int i=0; i<maxPoints; i++) { 
    JTextField textField = new JTextField();
    points.add(textField);
    list.add( textField ); // keep a reference to those fields.
}

// Later

for( JTextField f : list ) { 
   System.out.println( f.getText() ) ;
}

Wasn't that easy?

Just remember to keep these kinds of artifacts ( list ) as private as possible. They are for your control only, I don't think they belong to the interface.

Let's say you want to get the array of texts, instead of

 public List<JTextField> getFields();

You should consider:

 public List<String> getTexts(); // get them from the textfields ... 

Solution 2

Every JPanel in Java is also an AWT container. Thus, you should be able to use getComponents to get the array of contained components in the panel, iterate over them, check their types (To make sure you didn't get other controls), and do whatever you need with them.

However, this is generally poor design. If you know that you will need to access specific components, it is better to maintain an array of those text fields and pass it around, even though they are visually contained within the container.

If this is a recurrent task or could be done from multiple points, it may even make sense to have a special class representing a panel with text fields, that will then provide through its interface means of accessing these fields.

Solution 3

This is what I did to recursively go through the container and get the textfields that are on the JPanels.

private void ClearAllFields(Container myContainer) {

    Component myComps[] = myContainer.getComponents();

    for (int i=0; i<myComps.length; i++) {
      if(myComps[i] instanceof JPanel) {
          JPanel myPanel = (JPanel) myComps[i];
          ClearAllFields(myPanel);
      }
      if(myComps[i] instanceof JTextField) {
        JTextField myTextField = (JTextField) myComps[i];
        myTextField.setText("");
      }
    }        
}

And then to use it, you call it this way

ClearAllFields([jdialog or jframe etc].getContentPane());

Solution 4

You should call the getComponents method this returns with an array of all elements on your JPanel. After you can iterate on the array and check if its equals with the sought after component.

List<JTextField> list = new ArrayLists<JTextField>();
Component[] components = panel.getComponents();

for (Component component : components) {
    if (component.getClass().equals(JTextField.class)) {
        list.add((JTextField)component);
    }
}

Solution 5

    //una forma de recorer todos los elementos dentro de un jpanel
    Component[] components = jPanelX.getComponents();

    for (int i = 0; i < components.length; i++) {

        if(components[i].getClass().getName().toString().equals("javax.swing.JTextField")){
            components[i].setEnabled(false);
        }
    }
Share:
52
user3769503
Author by

user3769503

Updated on July 09, 2022

Comments

  • user3769503
    user3769503 almost 2 years

    Im trying to load an iframe onto a container dynamically. there are two buttons a and b, on clicking each of them a different url should load onto the iframe.

    I have tried- 1.using two different containers instead of one as shown below..during which browser crashes. 2. i was not able to switch the content inside the container by using dom.clear. 3. should i consider using a different xtype?. since container positions the iframe right on the spot where i wanted it to.

    Ext.define('xxxxxx', { extend: 'yyyyy', width: '100%', height: 125, renderTo: Ext.getBody(), items: [{ xtype: 'radiogroup', columns: 2, width: 400, horizontal: true, name:'canvas', items: [
    { boxLabel: 'a', id: 'i1', inputValue: 'i1', handler: function () {

                                var iframe = new Ext.ux.IFrame({
                                    src: 'https://www.sencha.com',
                                    title: 'Hello',
                                    width: "100%",
                                    height: 1400,
                                    renderTo: 'canvas'
                                });
    
                            }
                        },
                        {
                            boxLabel: 'b',
                            id: 'i2',
                            inputValue: 'i2',
                            handler: function () {
                            var iframe = new Ext.ux.IFrame({
        src: 'https://www.sencha.com/web-application-lifecycle-management-sencha-platform/',
                                    width: "100%",
                                    height: 1400,
                                    renderTo: 'canvas'
                                });                                
                            }
                        }
                ]
    },
    {
        xtype: 'container',
        name: 'somename',
        id: 'canvas',       
    }]
    

    });