Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Removing components from JPanel

807603Dec 30 2007 — edited Dec 31 2007
I have an application which opens a dialog box to add a record. The box contains a name field and a drop down box. When an entry in the drop down box is selected, the fields are updated to reflect the change in account type.

I need to remove all components except the first 3 (they're always the same), then re-add components to the container, but I'm having problems getting rid of eveything in the container first. This is what I have:
    private void showAddDialog(int index) {
        String frameTitle;
        if(index == -1) {
            if(types.size()>0) {
                editingAccount = new Account("",types.get(0).getName(),types.get(0));
                frameTitle = "Add Account";
            } else {
                //TODO: messagebox to say "no types"
                return;
            }
        } else {
            editingAccount = selectedAccount;
            frameTitle = "Edit Account";
        }
        adddialog = new JFrame(frameTitle);
        addcontainer = new JPanel();
//TODO: Add scroll pane
            adddialog.setContentPane(addcontainer);
            addcontainer.setLayout(new GridBagLayout());
        addconstraints = new GridBagConstraints();
            addconstraints.insets = new Insets(1,1,1,1);
        
        setgrid(addconstraints,0,0,1,1,GridBagConstraints.NONE,GridBagConstraints.NORTHWEST,0.5);
        JLabel lblaccname = new JLabel("Name:");
            addcontainer.add(lblaccname,addconstraints);
        txtaccname = new JTextField(12);
            if(index >= 0) {
                txtaccname.setText(editingAccount.getName());
            }
        setgrid(addconstraints,0,1,GridBagConstraints.REMAINDER,1,GridBagConstraints.HORIZONTAL,GridBagConstraints.WEST,1);
            addcontainer.add(txtaccname,addconstraints);
        
        setgrid(addconstraints,1,0,1,1,GridBagConstraints.NONE,GridBagConstraints.NORTHWEST,0.5);
        lblaccname = new JLabel("Type:");
            addcontainer.add(lblaccname,addconstraints);
        cacctype = new Choice();
            for(AccType opt : types) {
                cacctype.add(opt.getName());
            }
            cacctype.select(getTypeIndex(editingAccount.getType()));
        setgrid(addconstraints,1,1,GridBagConstraints.REMAINDER,1,GridBagConstraints.HORIZONTAL,GridBagConstraints.WEST,1);
            addcontainer.add(cacctype,addconstraints);
            cacctype.addItemListener(new AccTypeChoiceListener());
        
        gettextfields(cacctype.getSelectedIndex());
        
        adddialog.setVisible(true);
    }
    
    private void gettextfields(int index) {
        AccType type = types.get(index);
        
        int cury = addconstraints.gridy+1; //2
        for(int i=4;i<addcontainer.getComponentCount();i++) {
             addcontainer.remove(addcontainer.getComponent(i));
        }
        
        fieldmap = new LinkedHashMap<String,JTextField>();
        JTextField curfield;
        for(String name : type.fields) {
            setgrid(addconstraints,cury,0,1,1,GridBagConstraints.NONE,GridBagConstraints.NORTHWEST,0.5);
            JLabel lbl = new JLabel(name);
            addcontainer.add(lbl,addconstraints);
            
            curfield = new JTextField(22);
            if(!editingAccount.getName().equals("")) {
                curfield.setText(editingAccount.getField(name));
            }
            fieldmap.put(name,curfield);
            setgrid(addconstraints,cury,1,GridBagConstraints.REMAINDER,1,GridBagConstraints.HORIZONTAL,GridBagConstraints.WEST,1);
            addcontainer.add((JTextField)fieldmap.get(name),addconstraints);
            
            cury++;
        }
        adddialogbuttonbox = addbuttonbox();
        adddialog.add(adddialogbuttonbox,addconstraints);
        adddialog.pack();
    }
A few things which might help:
-types is an arraylist
-type.fields returns a string array,
-the linkedhashmap is used to retrieve the fields later on

The dialog box functions perfectly, but I can't get it to clear the old JTextFields from the container. The code for saving the user's entries is fine, it's just clearing the box. Oddly enough, removing the old labels doesn't seem to be a problem. The container is filled correctly the first time, but when the value in the drop down box is changed and the fields are refreshed, the JTextFields won't disappear.

Any help is appreciated.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 28 2008
Added on Dec 30 2007
7 comments
513 views