Results 1 to 10 of 10
  1. #1
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default Remove an item from listbox

    Does anyone know the command for removing a selected item from a listbox by using a simpel button?

    i can't find the syntax for it anywhere. and API isnt helping much.

    thx in advance,

    Dieter

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

  3. #3
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    or is it called scrollpane in java:p sorry i keep mixing vb and java sometimes.

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

    Default

    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?

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

    Default

    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.

  6. #6
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    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

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

    Default

    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!

  8. #8
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    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:

    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());
                }
            }
        }
    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.

    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 05:32 PM.

  9. #9
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    The one i ment is a JList
    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.

  10. #10
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    been there done that. i forgot my project at school, will post it tmorrow.

Similar Threads

  1. Extract item from a string
    By firewalll in forum New To Java
    Replies: 2
    Last Post: 09-02-2009, 06:00 AM
  2. [SOLVED] ArrayList to listbox
    By DanielS in forum AWT / Swing
    Replies: 12
    Last Post: 12-01-2008, 04:50 AM
  3. To highlight multiple selected values in listbox
    By swapnanair in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 05-06-2008, 12:17 PM
  4. Listbox Add/Delete
    By Rageagainst20 in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 05:49 PM
  5. grabbing an item from an arraylist.
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-17-2008, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •