Results 1 to 8 of 8
  1. #1
    kewlgeye is offline Member
    Join Date
    Apr 2008
    Posts
    34
    Rep Power
    0

    Question Adding to JList with Netbeans

    Hello,

    I am having a really difficult time trying to add a jTextField name and last name to the jList with netbeans, I know that for exit, I can create an action by right mouse clicking and creating an event. This is simple enough, however I have an add button that I am using and I have no idea how to code the add button with netbeans. Can someone please explain to me in detail how to do this.

    I looked for tutorials on using netbeans to use the add button to add to a jlist and I found nothing. if someone could give me the instructions on how to do this, or show me the page for netbeans that shows the steps in netbeans to do this I would really appreciate it. Thank you.

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Not clear,

    You want to add the contents of the textField to JList?

    I looked for tutorials on using netbeans to use the add button to add to a jlist and I found nothing.
    NetBeans doesn't support that feature(The programmer should do it)... it only initializes the contents when the ClassLoader loads that class.
    Not dynamic...

    try to use DefaultListModel, have experiments on it....
    THere is a JList constructor that has a parameter ListModel...
    You can pass that DefaultListModel to that parameter....

    You may play with that.....
    freedom exists in the world of ideas

  3. #3
    kewlgeye is offline Member
    Join Date
    Apr 2008
    Posts
    34
    Rep Power
    0

    Question Adding to JList with Netbeans

    Ok, I don't really understand where this defaultlistmodel goes, and

    THere is a JList constructor that has a parameter ListModel...
    I read about this but I keep forgetting what constructors are and a parameter?

    do I do something like this

    public class defaultlist {
    public void defaultListModel() {
    listmodel.setjlist1.(jTextField1)
    listmodel.setjlist1.(jTextField2)
    }
    }

    and then in the add button actionperformed event
    do this

    new defaultListModel();

  4. #4
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    No....

    Something like,

    Java Code:
    class MyMainClass{
          DefaultListModel dlm = new DefaultListModel();
          JList jlist = new JList( dlm );
    
          //invoke this when add button fired
          private static final void addList(){
                dlm.addElement((Object)jTextField1); // add name
                dlm.addElement((Object)jTextField2); // add lastname
          }
    }
    and observe what happens to jlist...
    Last edited by sukatoa; 06-03-2008 at 08:49 AM.
    freedom exists in the world of ideas

  5. #5
    kewlgeye is offline Member
    Join Date
    Apr 2008
    Posts
    34
    Rep Power
    0

    Default

    Ok I put this code in the jbutton section and I kept receiving an error that said "Illegal start of expression" I don't know enough to see anything wrong with this. Netbeans didn't say anything else.

    Java Code:
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
         //invoke this when add button fired
          private static final void addList(){
                dlm.addElement((Object)jTextField1); // add name
                dlm.addElement((Object)jTextField2); // add lastname
          }
        }
    and this code I put at the very top of the code, their is more like the rest of the GUI, but I didn't paste it because its too long.


    Java Code:
    package editor;
    
    import javax.swing.JList;
    import javax.swing.*;
    /**
     *
     * @author  Philip
     */
    
    class MyMainClass{
          DefaultListModel dlm = new DefaultListModel();
          JList jlist = new JList( dlm );
    }
    
    
    public class NewJFrame extends javax.swing.JFrame {
        
        /** Creates new form NewJFrame */
        public NewJFrame() {
            initComponents();
        }

  6. #6
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    How then could i do, such a wicked thing, and sin against the netbeans... :(

    after the button clicked, the method
    jButton1ActionPerformed
    should be invoked....

    And? i didn't say put that sample method to that actionPerformed...

    Java Code:
    eg.
    
     private void jButton1ActionPerformed(netbeans style) {
          addList();
        }
    put that addList beside those existing methods in your main class

    What i mean to MyMainClass is the class you've coded where the main method exists....
    I assumed you textFields are also in that main class...
    Last edited by sukatoa; 06-03-2008 at 08:13 PM.
    freedom exists in the world of ideas

  7. #7
    kewlgeye is offline Member
    Join Date
    Apr 2008
    Posts
    34
    Rep Power
    0

    Smile

    Thank you for your help. Let me show you what I did.



    I declared private DefaultListModel lm = newDefaultListModel(); with the rest of my declared buttons and textfields etc...

    Then I went to the jlist properties section and clicked on the model section and typed in lm after selecting custom code. and then just added the elements to the add button after creating an event actionperformed by right clicking on the add button, selecting events, then actionperformed and typing in the following code.

    lm.addElement(jTextField1.getText()); etc... for the rest of the textfields

    I tryed doing this yestereday but I was trying to select component and then I was trying to bind. I go it now though, so if any other newbies like myself need help with something similar hopefully they will find my post, since I was looking for hours for a post like this.


    Thank you again for your effort.

  8. #8
    Rogue45 is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: Adding to JList with Netbeans

    I searched forever for this!!! Thank you kewlgeye!!!

Similar Threads

  1. JList and JPanels
    By JetsYanks in forum New To Java
    Replies: 8
    Last Post: 12-25-2009, 02:11 PM
  2. searching within a JList
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-10-2008, 12:12 AM
  3. Adding Business Methods to an EJB in NetBeans
    By JavaForums in forum NetBeans
    Replies: 0
    Last Post: 07-30-2007, 11:13 PM
  4. Help with JList
    By Albert in forum NetBeans
    Replies: 1
    Last Post: 07-13-2007, 03:42 PM
  5. add a jlist column
    By Alan in forum JCreator
    Replies: 1
    Last Post: 05-28-2007, 04:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •