Removing an item from the JList using ListModel as model type?

RemoveSelectionInterval removes nothing from the model or the list except the selection interval. The list items remain unscathed. I'm afraid that you're either going to have to extend your ListModel and give it a removeItem(...) method as well as listeners and the ability to fire notifiers, etc... a la AbstractListModel -- quite a lot of work!

If it were my money, though, I'd go the easy route and simply use a DefaultListModel for my model as it is a lot safer to do it this way, a lot easier, and will take a lot less time. I know you state that you don't want to use these, but I think you'll find it a lot easier than your potential alternatives An example of an SSCCE is something like this: import java.awt.event. *; import javax.swing.

*; public class Foo1 { private String elements = {"Monday", "Tueday", "Wednesday"}; private javax.swing. JList made_list = new javax.swing.JList(); public Foo1() { made_list. SetModel(new DefaultListModel()); for (String element : elements) { ((DefaultListModel) made_list.getModel()).

AddElement(element); } JButton removeItemBtn = new JButton("Remove Item"); removeItemBtn. AddActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { removeActionPerformed(e); } }); JPanel panel = new JPanel(); panel. Add(new JScrollPane(made_list)); panel.

Add(removeItemBtn); JOptionPane. ShowMessageDialog(null, panel); } private void removeActionPerformed(ActionEvent e) { System.out. Println("made_list's model: " + made_list.getModel()); System.out.

Println("Model from a fresh JList: " + new JList().getModel()); DefaultListModel model = (DefaultListModel) made_list.getModel(); if (model.size() > 0) { model. Remove(0); } } public static void main(String args) { new Foo1(); } }.

RemoveSelectionInterval removes nothing from the model or the list except the selection interval. The list items remain unscathed. I'm afraid that you're either going to have to extend your ListModel and give it a removeItem(...) method as well as listeners and the ability to fire notifiers, etc... a la AbstractListModel -- quite a lot of work!

If it were my money, though, I'd go the easy route and simply use a DefaultListModel for my model as it is a lot safer to do it this way, a lot easier, and will take a lot less time. I know you state that you don't want to use these, but I think you'll find it a lot easier than your potential alternatives. An example of an SSCCE is something like this: import java.awt.event.

*; import javax.swing. *; public class Foo1 { private String elements = {"Monday", "Tueday", "Wednesday"}; private javax.swing. JList made_list = new javax.swing.JList(); public Foo1() { made_list.

SetModel(new DefaultListModel()); for (String element : elements) { ((DefaultListModel) made_list.getModel()). AddElement(element); } JButton removeItemBtn = new JButton("Remove Item"); removeItemBtn. AddActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { removeActionPerformed(e); } }); JPanel panel = new JPanel(); panel.

Add(new JScrollPane(made_list)); panel. Add(removeItemBtn); JOptionPane. ShowMessageDialog(null, panel); } private void removeActionPerformed(ActionEvent e) { System.out.

Println("made_list's model: " + made_list.getModel()); System.out. Println("Model from a fresh JList: " + new JList().getModel()); DefaultListModel model = (DefaultListModel) made_list.getModel(); if (model.size() > 0) { model. Remove(0); } } public static void main(String args) { new Foo1(); } }.

I have created the List as DefaultListModel and when I use this DefaultListModel model = (DefaultListModel)made_list.getModel(); to access the methods of DefaultListModel I am gettin the error – Deepak Apr 25 at 4:20 Exception in thread "AWT-EventQueue-0" java.lang. ClassCastException: javax.swing. JList$4 cannot be cast to javax.swing.

DefaultListModel – Deepak Apr 25 at 4:21 @Deepak: let's see more of the offending code as an edit to your original post in this thread. I suggest showing us where you set the list's model with your DefaultListModel object, and where you try to access this model and call it's methods. It looks to me as if you're trying to cast a JList, not its model as a DefaultListModel, but we'll know for sure what you're doing when we see your code.

– Hovercraft Full Of Eels Apr 25 at 4:22 check my edit now.. – Deepak Apr 25 at 4:27 @Deepak: are you sure the two JLists are one and the same, that you don't have one declared in the class and another (the one with the DefaultListModel for model) declared in a constructor or method? It will be hard to figure this out I think without compilable runnable code, but your whole program is likely too big and cumbersome to be much use to us. Consider creating and posting a small runnable program that shows your problem but has no extraneous code, an SSCCE.

– Hovercraft Full Of Eels Apr 25 at 4:39.

You've been given a link to different sections of the Swing tutorial in the past to help solve problems. This was done for a reason. It helps solve your current problem.It gives you a reference for future problems.

All you need to do is look at the Table of Contents for the Swing tutorial and you will find a section on "How to Use Lists" which has a working example that adds/removes items from a list. Please read the tutorial first. Or if you can't remember how to find the Swing tutorial then read the JList API where you will find a link to the same tutorial.

All you need to do is look at the Table of Contents for the Swing tutorial and you will find a section on "How to Use Lists" which has a working example that adds/removes items from a list. Please read the tutorial first. Or if you can't remember how to find the Swing tutorial then read the JList API where you will find a link to the same tutorial.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions