1

I'm building a classic form GUI with a variable amount and order of JTextFields, JCheckBoxes and JComboBoxes, with separate JPanels containing several groups of them.

For now I just build them in a builder method that adds a JLabel + the appropriate input Element to a JPanel and returns the JPanel to the main initialize method.

Therefore, I can only access the elements by

Component[] panels = frame.getContentPane().getComponents();  
JPanel panel = (JPanel) panels[f];  
Component[] components = panel.getComponents();  

[...] determining the element class  
JTextField field = (JTextField) components[u + 1]

Reading works fine with this method, but setting values is harder because I'm currently not accessing the elements directly. Am I doing something wrong or do I have to rebuild the panel from the start?

Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
  • What prevents you from storing the components in variables/fields? Is it a limitation of the UI builder you are using (which I doubt since this is a normal use case...) – Andreas Fester Oct 10 '12 at 12:46
  • 2
    Waaaait... I could just store the elements in lists. The whole variable amount of elements thing threw me off, and I didn't think about storing them the normal way anymore. I feel stupid now –  Oct 10 '12 at 12:47
  • *"with a variable amount and order of"* Where does the information on numbers of components originate? What is the information (e.g. DB query results)? – Andrew Thompson Oct 10 '12 at 12:48
  • @ Andrew: For now: Main method stores them in a list somewhere, later: DB query result. –  Oct 10 '12 at 12:49

2 Answers2

2

You are not doing it bad, you are on the right track. Actually, this is how it is usually done.

Your GUI is in some class - and you need to decide which elements of that GUI are "interactive" - i.e. there are some actions done on them later during execution. Those can be control elements such as text fields, buttons, etc... And there are elements that are not interactive and are not used after their creation. Those are labels, panels, etc... Their purpose is just be painted on the GUI.

So when you decide which elements are "important" ones, you just promote those elements from local variables to class fields. If you are using any decent IDE (such as NetBeans or Eclipse), that option is even offered to you undre the Refactoring tab. Nonetheless, the result should look like this:

public class GUI{
    public void create GUI{
        //code before...
        JTextField field = new JTextField();
        //code after...
    }
}

will be changed to:

public class GUI{
    private JTextField textField;

    public void create GUI{
        //code before...
        textField = new JTextField();
        //code after...
    }
}

or if you have variable amounts of those fields,you can declare them in an array or a Collection:

public class GUI{
    private JTextField[] textFields;

    public void create GUI{
        //code before...
        for(int i = 0; i < dbResultCount; i++){
            textFields[i] = new JTextField();
        }
        //code after...
    }
}
Jakub Zaverka
  • 8,816
  • 3
  • 32
  • 48
1

The approach you are using doesn't sound like a good idea. It will be really hard to maintain this application.

If you need access to some of your components then I suggest you declare them as instance variables.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • I was used to declaring important components directly, and this is the first time that I don't know how many there will be. I'm going to add all the components to a list now –  Oct 10 '12 at 12:51