JList (swing)

How can I add elements,delete and update from an existing empty JList?
twgAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

shaveriCommented:
what is meant by this?
0
twgAuthor Commented:
I created an empty JList in my application.
In one of my methods I want to fill it with items (after it was created as an empty list).
How do I add elements to this list and after adding, how can I delete or update the items in the list?
0
PanchasheelCommented:
You can use a DefaultListModel for the JList and whenever you want to add, delete or update, just get all the elements from the ListModel and do the Changes.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

twgAuthor Commented:
Can you give me a code example?
0
PanchasheelCommented:
Hi

This is an applet, try running on 1.1.7 or 1.1.8
There are two methods comment addData and removeData, try this, call this method on add and delete.


import com.sun.java.swing.*;
import java.awt.*;

public class JApplet1 extends JApplet
{
      public void init()
      {
            // This line prevents the "Swing: checked access to system event queue" message seen in some browsers.
            getRootPane().putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);
            
            getContentPane().setLayout(null);
            setSize(606,528);
            getContentPane().add(JList1);
            JList1.setBounds(156,84,229,297);
            DefaultListModel model = new DefaultListModel();
            JList1.setModel(model);
            addData();
            removeData();
      }

      //{{DECLARE_CONTROLS
      com.sun.java.swing.JList JList1 = new com.sun.java.swing.JList();
      //}}
      void addData()
      {
          DefaultListModel model = (DefaultListModel)JList1.getModel();
          
          model.addElement("abc");
          model.addElement("xyz");
          
          JList1.revalidate();
      }
      void removeData()
      {
          DefaultListModel model = (DefaultListModel)JList1.getModel();
          
          model.removeElement("abc");
          
          JList1.revalidate();
          
      }

}
0
twgAuthor Commented:
Hi Panchasheel,
Thank you very much for your example. I tried it and it worked out. because I have alot of items to put into the list, I added
scrollbars with the following code:
JScrollPane scrollPaneList = new JScrollPane(MyList);
            scrollPaneList.setAlignmentX(LEFT_ALIGNMENT);
            scrollPaneList.setAlignmentY(TOP_ALIGNMENT);

jpanelMyList.add(scrollPaneList);
(jpanelMyList extends from JPanel and MyList is in it).

But as I run the program with this code , the list disapears.
What's wrong with my code, how do I add scrollbars to the list?
            
0
PanchasheelCommented:
Even with your code, the program runs fine here, can you send the full code.
0
twgAuthor Commented:
hello panchasheel. Sorry for not posting for a long time. Anyway, I still have that problem (the one I mentioned in my last comment).
The following is my code:
public class MyFrame extends JFrame {

//instance variables
public Jbuttons (a lot of JButtons)
public JPanels
public DefaultListModels
public JLists........etc, etc...

public MyFrame() {

//setup GUI
'
'
'
jpanelMyList.add(jMyList);
jMyList.setModel(modelMyList);  
JScrollPane scrollPaneList = new JScrollPane(jMyList);
 
scrollPaneList.setAlignmentX(LEFT_ALIGNMENT);
scrollPaneList.setAlignmentY(TOP_ALIGNMENT);
jpanelMyList.add(scrollPaneList);
(jpanelMyList extends from JPanel and MyList is in it).
'
'
'
}//end CTor
'
'
'
}//end class

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.