Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 SWING: Adding components during runtime
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Aziz




PostPosted: Mon Aug 15, 2005 6:08 pm   Post subject: SWING: Adding components during runtime

I want to be able to have a JFrame, and then have a text field or two, then have the option for the user to add more text areas, and then more components. like this

code:

[label] [textfield]
[label] [textfield] [add button]
        [added textfield 1] [add moved down here now] [remove]
        [added textfield 2] [add moved down here now] [remove]
[label] [textfield]
Sponsor
Sponsor
Sponsor
sponsor
Hikaru79




PostPosted: Mon Aug 15, 2005 9:09 pm   Post subject: (No subject)

JFrame's can both add(Component) and remove(Component). Simply have the removal/addition of these components (and any neccesary resizing) be done in the event listners registered to whatever buttons they press to add/remove buttons.
Aziz




PostPosted: Thu Aug 18, 2005 8:59 am   Post subject: (No subject)

So what layout would be best for this? Don't certain layouts, like flow layout, just add to the end? I want to be adding/remove somewhere in the middle...? or should I use JPanels?
1of42




PostPosted: Sun Aug 28, 2005 8:48 pm   Post subject: (No subject)

Using layered JPanels with various layout managers would be your best bet.
Aziz




PostPosted: Mon Sep 05, 2005 10:40 am   Post subject: (No subject)

Well folks, this topic was old news, thought everything was going to work out fine, but nope! I've come to the point where the program is almost complete. Now, for these fields, I can get them to go away (by removing them) but I cannot add them! Could someone help me? I've attached the code. The problem lies in RecipeManager.java

I know it's a bit to sort through but here the rundown...

the add button fires the action command ADD. so in actionPerformed(ActionEvent e) at the bottom of the if statement under:

else if (cmd.equals(ADD))

is where my attempt is. I know its kind of confusing, but I don't have much time right now to explain much more. If you have any question just post them and I'll check back tommorow night (going to the lady's in a little bit then school starts tommorow :'()

Thanks a lot for all those who ATTEMPT to help!
rizzix




PostPosted: Mon Sep 05, 2005 12:33 pm   Post subject: (No subject)

hmm instead of calling update.. call setVisible(true).. that will automatically update everything.. i mean everything.. the whole hierarchy... which is exactly what you need to do... (note the changes i've made... denoted by the "//<---- here" comments)

Java:
else if (cmd.equals(REMOVE)) //Remove ingredient button
        {
                //Remove button that was clicked
                JButton button = (JButton)e.getSource();
                int ingNum = removeButtons.indexOf(button);
               
                //Remove
                        ingFields.remove(ingNum); //Remove Textfield from ArrayList
                        removeButtons.remove(ingNum); //Remove JButton from ArrayList
                        ingPanes.remove(ingNum); //Remove JPanel from ArrayList
                        ingsPane.remove(ingNum); //Remove them from JPanel
                       
                saved = false;
                frame.setTitle("Recipe Manager [unsaved document]");
               
                        //If they deleted the last one, add a new button 
                if (ingNum == 0)
                {
                        addButton.doClick();
                }
                       
                        frame.setVisible(true); // <------- here
        }
        else if (cmd.equals(ADD)) //Add ingredient button
        {
                int ingCount = ingPanes.size();
               
                System.out.println(ingPanes.size());
               
                //Add new ArrayList elements
                ingFields.add(new JTextField(20));
                removeButtons.add(makeRemoveButton());
                ingPanes.add(new JPanel());             
                       
                        //Add ingredient field and remove button to new panel
                        ingPanes.get(ingCount).add(ingFields.get(ingCount));
                        ingPanes.get(ingCount).add(removeButtons.get(ingCount));
                       
                        //Set panel restrictions
                        ingPanes.get(ingCount).setMinimumSize(
                                ingPanes.get(ingCount).getPreferredSize());
                        ingPanes.get(ingCount).setMaximumSize(
                                ingPanes.get(ingCount).getPreferredSize());
                               
                        //Add new panel to ingredient pane
                        ingsPane.add(ingPanes.get(ingCount));
                       
                        frame.setVisible(true); // <------- and here
                       
                System.out.println(ingPanes.size());
        }
       
    }


NOTE: the method you chose to use to go about with such functionality is not the ideal way of going about with it... the ideal way would involve you creating a new container component... and adding in the functionality your self.. of course now that's a lot of work, comparitively, but it will allow you to reuse the mechanism elsewhere easily... another way of doing this.. and probably the preferable way,, would invovle using the JList component... nevertheless your current solution "will" work..
Aziz




PostPosted: Tue Sep 06, 2005 9:38 am   Post subject: (No subject)

And that is why you're the master and I am the student Razz Well I never would have thought about that. But I still don't quite understand. If I can figure it out I'd prefer to do it that way. But what do you mean with a JList component?
rizzix




PostPosted: Wed Sep 07, 2005 12:05 am   Post subject: (No subject)

my mistake.. apparently the JList does not support in-cell-editing.. i.e you can put a button in there for a cell but you can't expect it to receive events... in other words.. it will render the button but the button will not function... hmm..

i know the JTable component allows in-cell-editing.. i'll look into how such functionality can be accomplished using the Jtable component, and come back to you later..
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: