4 Replies - 248 Views - Last Post: 04 May 2013 - 06:24 PM Rate Topic: ***-- 2 Votes

#1 mr.nacho  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 28-December 12

[HELP] JList Duplication

Posted 04 May 2013 - 07:12 AM

I'm really struggling here. I don't know what's wrong with this code?

 					for(int x = 0;x<model.getSize();x++)
					{
						if(cboGuitar.getSelectedItem(cboGuitar.getSelectedIndex())==model.getElementAt(x))
						{
							JOptionPane.showMessageDialog(null, "Already in the list");
						}
						else
						{
							model.addElement(cboGuitar.getItemAt(cboGuitar.getSelectedIndex()));
							btnRemove.setEnabled(true);
						}
					}




I want to avoid duplication with this but whenever I press the "add item", it just does nothing.

Thanks for the future help.

Is This A Good Question/Topic? 0
  • +

Replies To: [HELP] JList Duplication

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1278
  • View blog
  • Posts: 2,848
  • Joined: 05-April 11

Re: [HELP] JList Duplication

Posted 04 May 2013 - 09:09 AM

You mean JComboBox and not JList?

Also, what do you mean with "it just does nothing" ?

Anyway.. Neither JComboBox or JList has a getSelectedItem method which accepts an integer
//getSelectedItem(int) doesn't exist??
cboGuitar.getSelectedItem(cboGuitar.getSelectedIndex())


Was This Post Helpful? 0
  • +
  • -

#3 mr.nacho  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 28-December 12

Re: [HELP] JList Duplication

Posted 04 May 2013 - 09:26 AM

Oops, I was wrong there.

it should be cboGuitar.getItemAt(cboGuitar.getSelectedIndex())..

my problem is for JList. I'm adding its content from cboGuitar but if I added a for loop, it's not functioning at all. Nothing's happening.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: [HELP] JList Duplication

Posted 04 May 2013 - 01:48 PM

If you are using Object

getSelectedIndex())==model.g

you just can't use the == you will need to use this Object equals() method

Also, right now, using the for() loop yu will insert the Object as many time as there are element in your list

You need

    int i = 0;
    for(; i < list.size; ++i) {
      if(list.get(i).equals(elementToInsert))
         break;
    }
    if(i == list.size()) 
         ...element was not in the list, I can insert it



and perform this operation

cboGuitar.getItemAt(cboGuitar.getSelectedIndex())..

only once befor entring the loop. If the list has 500 elements, no need to call 500 times:
cboGuitar.getSelectedIndex() and 500 times
cboGuitar.getItemAt()

actually there is a getSelectedItem() that perform these 2 operations
Was This Post Helpful? 2
  • +
  • -

#5 mr.nacho  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 28-December 12

Re: [HELP] JList Duplication

Posted 04 May 2013 - 06:24 PM

View Postpbl, on 04 May 2013 - 01:48 PM, said:

If you are using Object

getSelectedIndex())==model.g

you just can't use the == you will need to use this Object equals() method

Also, right now, using the for() loop yu will insert the Object as many time as there are element in your list

You need

    int i = 0;
    for(; i < list.size; ++i) {
      if(list.get(i).equals(elementToInsert))
         break;
    }
    if(i == list.size()) 
         ...element was not in the list, I can insert it



and perform this operation

cboGuitar.getItemAt(cboGuitar.getSelectedIndex())..

only once befor entring the loop. If the list has 500 elements, no need to call 500 times:
cboGuitar.getSelectedIndex() and 500 times
cboGuitar.getItemAt()

actually there is a getSelectedItem() that perform these 2 operations


Sir, finally, I got it. :D Thanks. I used your idea and I did it this way.

if (cboGuitar.getSelectedIndex()!=0)
				{
					int confirmCase = 0;

					for(int x = 0;x<model.getSize();x++)
					{
						if(cboGuitar.getItemAt(cboGuitar.getSelectedIndex()).equals(model.getElementAt(x)))
						{
							JOptionPane.showMessageDialog(null, "Already in the list");
							confirmCase = 1;
							break;
						}

					}

						switch (confirmCase)
						{
							case 0:

								model.addElement(cboGuitar.getItemAt(cboGuitar.getSelectedIndex()));
								btnRemove.setEnabled(true);
						}


				}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1