• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 999
  • Last Modified:

JList (swing)

How can I add elements,delete and update from an existing empty JList?
0
twg
Asked:
twg
  • 4
  • 3
1 Solution
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now