Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-30-2007, 05:05 PM
Echilon's Avatar
Member
 
Join Date: Dec 2007
Location: England
Posts: 6
Echilon is on a distinguished road
Send a message via ICQ to Echilon
Removing components from JPanel
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:
Code:
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
image removing Triss New To Java 3 01-20-2008 09:27 PM
Removing characters kDude New To Java 3 12-03-2007 03:38 AM
removing duplicates from arrays bugger New To Java 3 11-13-2007 07:11 PM
removing an object from memory ravian New To Java 1 11-12-2007 10:23 PM
Help with removing somthing once inputed. fegiflu New To Java 8 07-26-2007 11:58 PM


All times are GMT +3. The time now is 04:20 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org