Results 1 to 10 of 10
Thread: Remove an item from listbox
- 09-21-2009, 02:20 PM #1
-
- 09-21-2009, 02:43 PM #3
or is it called scrollpane in java:p sorry i keep mixing vb and java sometimes.
-
No, it can't be a scrollpane as that is an AWT component that does nothing but hold things that are meant to scroll. You may want to find out these things before asking your question. Much luck.
edit: could you be asking about a JList?
-
Assuming that it is a JList that you're interested in (sorry, but for now, I have no idea), to be able to add and remove things from a JList you need to have access to its model. In fact this is a general rule that most Swing components follow: to manipulate the content of a swing JComponent you manipulate the component's model. The JList uses objects that implement the ListModel interface as its model. Likely the best one for your to use is the DefaultListModel class whose API is here:
DefaultListModel API
I would suggest that you initialize a DefaultListModel object with the data that you want displayed in your JList, add it to the JList either as a parameter to the JList's constructor or via a setModel method call, and then add or remove elements from the model as needed. The API will tell you how to do that.
If a JList is not what you are looking for, but it's another Swing component, these general rules apply. We'll be happy to supply you with specifics once this issue is clarified.
Again, much luck.
- 09-21-2009, 03:47 PM #6
well ok my bad, i was in a hurry and looked at the wrong name. sorry for that.
The one i ment is a JList.
i created a new object by adding a new customer to the JList.
i'm trying to select an element wich the user selects with a mouseclick event and then just press the delete button.
i tried several things but can't find a way to do it.
Call me stupid call me dumb, but i can't find the logic to do it
-
You're not dumb. You're trying to learn something new which is good.
Have you tried using a DefaultListModel as suggested above? If not, give it a go. If so and it's not working, then post your code and let's have a look.
Much luck!
- 09-21-2009, 04:29 PM #8
Yea i am allready using it. Tho i deleted the code i was trying to work with.
for now i got this little code to add it to the JList. i don't know if you will understand everyting:
I'm at school atm and goign home in 15 mins. ones home i will try to work on it again and post the code i tried to work with.Java Code:import java.util.ArrayList; import javax.swing.DefaultListModel; import javax.swing.JOptionPane; public ArrayList<Baasje> baasjes = new ArrayList<Baasje>(); public DefaultListModel lijstBaasjes = new DefaultListModel(); private void btnAddBaasjeActionPerformed(java.awt.event.ActionEvent evt) { boolean exists = false; if(txtNaamBaasje.getText().equals("") || txtVoornaam.getText().equals("") || txtRijksNrBaasje.getText().equals("") || txtGemeente .getText().equals("") || txtLand.getText().equals("") || txtNr.getText().equals("") || txtPostcode.getText().equals("") || txtProvincie.getText().equals("") || txtStraat.getText().equals("")){ JOptionPane.showMessageDialog(this, "Gelieve alle verplichte velden in te vullen.", "Foutmelding", JOptionPane.WARNING_MESSAGE); }else{ for(int i=0; i<=lstBaasjes.getMaxSelectionIndex(); i++) { if((txtRijksNrBaasje.getText().equals(baasjes.get(i).getRijksNr()+"") && (lstBaasjes.getMaxSelectionIndex() >= 0))) { exists = true; Contactgegevens tempGeg = new Contactgegevens(txtStraat.getText(), txtNr.getText(), Integer.parseInt(txtPostcode.getText()), txtGemeente.getText(), txtProvincie.getText(), txtLand.getText(), txtTelefoon.getText(), txtGsm.getText(), txtEmail.getText()); baasjes.get(i).setContact(tempGeg); baasjes.get(i).setNaam(txtNaamBaasje.getText()); baasjes.get(i).setVoornaam(txtVoornaam.getText()); baasjes.get(i).setGeslacht(cmbGeslachtBaasje.getSelectedIndex()==0?true:false); baasjes.get(i).setRijksNr(Integer.parseInt(txtRijksNrBaasje.getText())); } } if(!exists) { Contactgegevens tempGeg = new Contactgegevens(txtStraat.getText(), txtNr.getText(), Integer.parseInt(txtPostcode.getText()), txtGemeente.getText(), txtProvincie.getText(), txtLand.getText(), txtTelefoon.getText(), txtGsm.getText(), txtEmail.getText()); Baasje tempBaasje = new Baasje(txtNaamBaasje.getText(), txtVoornaam.getText(), ((cmbGeslachtBaasje.getSelectedIndex()==0)?(true):(false)), Integer.parseInt(txtRijksNrBaasje.getText()), tempGeg); baasjes.add(tempBaasje); lijstBaasjes.addElement(tempBaasje); lstBaasjes.setModel(lijstBaasjes); cmbVoegToeAan.addItem(makeObj(tempBaasje.toString())); lstBaasjes.setSelectedIndex(lstBaasjes.getLastVisibleIndex()); } } }
Hope you get an idea on what i'm trying to do here, else if you want i will explain it to u ones home(19.30 GMT+1)Last edited by Dieter; 09-21-2009 at 04:32 PM.
- 09-21-2009, 06:28 PM #9
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Well, then read the JList API and you will find a link to the Swing tutorial which has a working example that does exactly what you want.The one i ment is a JList
- 09-21-2009, 10:40 PM #10
Similar Threads
-
Extract item from a string
By firewalll in forum New To JavaReplies: 2Last Post: 09-02-2009, 05:00 AM -
[SOLVED] ArrayList to listbox
By DanielS in forum AWT / SwingReplies: 12Last Post: 12-01-2008, 03:50 AM -
To highlight multiple selected values in listbox
By swapnanair in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 05-06-2008, 11:17 AM -
Listbox Add/Delete
By Rageagainst20 in forum New To JavaReplies: 2Last Post: 04-16-2008, 04:49 PM -
grabbing an item from an arraylist.
By newtojava7 in forum New To JavaReplies: 1Last Post: 03-17-2008, 08:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks