Delete selection from a List

Asked

Viewed 188 times

0

In my java class I created a form and in it I created a button that clears all fields.

But I can’t get you to delete the selection of a list that I have in the form, it erases the textfield and radios Button but not the list.

Note: I am not using Jlist but List. I have tried this Nomecurso.clearSelection();` but the code turns red incorrect and does not deselect the item.

NomeCurso = new List(5, false);
Nome Curso.setSize(200, 170);
NomeCurso.setLocation(480, 145);
NomeCurso.addItem("Adm");
NomeCurso.addItem("Biomedicina");
NomeCurso.addItem("Ciencia da computacao");
NomeCurso.addItem("Farmacia");
NomeCurso.addItem("Direito");
NomeCurso.addItem("Educação física");

...

getContentPane().add(NomeCurso);
  • getListModel(). removerAllElements(); would not work?

  • Put the list package also to have more material to make an example here

  • Add a [mcve] of your code so it is possible to analyze the problem.

  • Not getListModel turned red

  • Pathname.removeAll(); You may have to give a repaint() later too

1 answer

1


For the package java.awt.List, in accordance with the Javadoc, to clear the selection you must use the method deselect(int index). The index can be given by the method getSelectedIndex() as in the example:

NomeCurso.deselect(NomeCurso.getSelectedIndex());

By the way, if you’re using Swing the ideal is to use the JList.

List.deselect

Deselects the item at the specified index.

In free translation:

Deselects the item specified in the index.


List.getSelectedItem

Gets the Selected item on this scrolling list.

In free translation:

Get the selected item from the scrolling list.

  • Not he erased all the elements leaving the List box blank, but I just wanted him to deselect the selected item

  • 1

    Ah tah, then try Nomecurso.deselect(Nomecurso.getSelectedIndex());

  • 1

    /it worked!!!! vlw

  • I’ll change the answer then to match the question

Browser other questions tagged

You are not signed in. Login or sign up in order to post.