Results 1 to 6 of 6

Thread: About JList

  1. #1
    hungleon88 is offline Member
    Join Date
    Aug 2008
    Posts
    41
    Rep Power
    0

    Default About JList

    By Default, JList only display the value that already exist when create JList.

    Now I have an empty JList, how can i add value to the Jlist by checkBox, textField ? and how can i show the value to the jList in Frame??

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You should set your JList's model to be an object of DefaultListModel. Then since you have a reference to this DefaultListModel (let's call "model") you can simply add objects to it via the addElement(Object obj) method.

    You would do well to study the Sun Swing tutorials, List section where this is all explained with examples.

    Good luck!

  3. #3
    hungleon88 is offline Member
    Join Date
    Aug 2008
    Posts
    41
    Rep Power
    0

    Default

    oh yes,i alredy use addElement, but i can't display the Item(value) in a jList that added
    can you hint me a method to display that?, i try .show(), .list() ... but not work, forgive me, i'm so noob T_T

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    If you add an element to the currently displayed JList's model, it will display; nothing else need be done. For instance:
    Java Code:
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.DefaultListModel;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    
    public class JListAddElement
    {
      private static DefaultListModel listmodel; // here's our DefaultListModel
    
      private static void createAndShowUI()
      {
        final JFrame frame = new JFrame("JList AddElement");
        final JPanel mainPane = new JPanel();
        
        String[] items = {"Fe", "Fi", "Fo", "Fum", "What the F*ck?!?"};
        
        // create the list model and fill with Strings
        listmodel = new DefaultListModel();
        for (int i = 0; i < items.length; i++)
        {
          listmodel.addElement(items[i]);
        }
        // create JList and give it our defaultlistmodel
        JList myJList = new JList(listmodel);
        
        // button that allows  us to add an item to the list model
        JButton addItem = new JButton("Add Item");
        addItem.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            // call a joptionpane to get a string
            String text = JOptionPane.showInputDialog(frame, "Add Item to be Entered into List");
            if (text != null && !text.isEmpty())
            {
              // then add the string to the list's model here
              // note that nothing else needs to be done.  When the model changes
              // the list automatically shows these changes
              listmodel.addElement(text);
            }
          }
        });
        
        mainPane.setPreferredSize(new Dimension(400, 300));
        mainPane.add(new JScrollPane(myJList));
        mainPane.add(addItem);
    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPane);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowUI();
          }
        });
      }
    }
    Last edited by Fubarable; 08-30-2008 at 10:04 PM. Reason: show an example

  5. #5
    hungleon88 is offline Member
    Join Date
    Aug 2008
    Posts
    41
    Rep Power
    0

    Default

    No no,i means, i create an empty JList, doesn't have any value in that.
    Then, i want to get value from ComboBox, CheckBox and add to the JList.Thus, when i click on button Create , the value after get from ComboBox , CheckBox and add to JList will display on JList interface.
    And the display is what i want :(

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    It doesn't matter if the DefaultListModel starts out with data or not. The method for getting stuff into it is the same. If you need more help, then create an SSCCE like I did above. Please read the link. It will tell you that this program should be small, compilable and runnable, and should have no code that doesn't relate to your problem, but the code should demonstrate your problem.

    And remember: Do not post your whole program as no one will read it. Instead (again) post an SSCCE. Good luck!

Similar Threads

  1. JList and JPanels
    By JetsYanks in forum New To Java
    Replies: 8
    Last Post: 12-25-2009, 03:11 PM
  2. JList problem
    By zizou147 in forum Advanced Java
    Replies: 1
    Last Post: 04-17-2008, 09:50 AM
  3. searching within a JList
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-10-2008, 01:12 AM
  4. Help with JList
    By Albert in forum NetBeans
    Replies: 1
    Last Post: 07-13-2007, 04:42 PM
  5. add a jlist column
    By Alan in forum JCreator
    Replies: 1
    Last Post: 05-28-2007, 05: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
  •