Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: need help understanding how to add to jlist

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need help understanding how to add to jlist

    Hello all, I'm new to this forum and fairly new to java as well so dumb it down for me if you can. I'm having trouble figuring this out. I have 3 jlists and each one has a textfield and a jbutton so the user can enter text to be added as a new list item. I have read probably a dozen webpages or forum posts on this and all have pretty much the same code advice for doing this. The problem I'm having is that none have said or shown how the code actually links the button to the jlist1, jlist2, and jlist3 respectively. What am I missing?? Here is the code I have and not sure what to change or add.

       private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                                
            String addName = (jTextField1.getText());        
            DefaultListModel names = new DefaultListModel();                
            names.addElement(addName);
            JList nameList = new JList( names );
            jTextField1.setText("");
        }                                        
     
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                                
            String addPlace = (jTextField2.getText());
            DefaultListModel places = new DefaultListModel();                
            places.addElement(addPlace);
            JList placeList = new JList( places );
            jTextField2.setText("");
        }                                        
     
        private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                               
            String addDay = (jTextField3.getText());
            DefaultListModel days = new DefaultListModel();                
            days.addElement(addDay);
            JList dayList = new JList( days );
            jTextField3.setText("");
        }

    Thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: need help understanding how to add to jlist

    how the code actually links the button to the jlist1,
    Not sure what you mean by "links".
    You add an action listener to a button. When the user presses the button, the code in the action listener method is called where you do what you want to do to respond to the button that was pressed.
    If you want to change the contents of a component like a JList, you need a reference to the JList object that you can use to call one of its methods.


    NOTE: I see you are defining variables inside of the mehtods. When the method exits, those definitions will go away. You need to define the variables outside of the methods at the class level where they will stay around for the life of the class object.

Similar Threads

  1. Not really Understanding JPanel..
    By Emperor_Xyn in forum Java Theory & Questions
    Replies: 7
    Last Post: January 1st, 2012, 05:48 AM
  2. Need Help Understanding Where I went wrong
    By basedoverlord12 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 1st, 2011, 06:40 PM
  3. Help me Understanding Please...
    By Jabetha in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 17th, 2011, 01:55 PM
  4. [SOLVED] Help with understanding the following commented codes!
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: April 2nd, 2011, 04:26 PM
  5. Help understanding this code
    By Zepx in forum Java Theory & Questions
    Replies: 2
    Last Post: October 20th, 2009, 10:18 AM

Tags for this Thread