0

I have a problem with the JList. Whenever i remove items from it, the list does not update it's appearance so items remain there and become uncheckable.

Here is code:

DefaultListModel listModel = new DefaultListModel();

JList figureListBox = new JList(listModel);
figureListBox.setBounds(5, 20, 240, 300);
figureListBox.setBackground(Color.WHITE);
figureListBox.setBorder(BorderFactory.createLineBorder(Color.BLACK));
figureListBox.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
figureListBox.setLayoutOrientation(JList.VERTICAL);
figureListBox.setVisibleRowCount(10);

JButton deleteFigureButton = new JButton("Delete");
deleteFigureButton.setBounds(5, 305, 240, 25);
deleteFigureButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        if(currentFigure != -1) {
            listModel.remove(currentFigure);
            currentFigure = -1;
        }
    }
});`

Repaint and revalidate doesn't work, as well as updateUI()

Here is a screenshot of how it looks like

Alex M
  • 19
  • 1
  • did you ever update `currentFigure` to a value >= 0 ? And the whole `repaint` stuff shouldn't be necessary – UninformedUser Dec 11 '18 at 13:59
  • `currentFigure` = checked index of list box. The point is that I forgot to mention that list does update after moving a window and selected item that should be removed remove. – Alex M Dec 11 '18 at 14:00
  • Is this code running in the AWT event dispatch thread? See [javax.swing](https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/package-summary.html) and https://docs.oracle.com/javase/tutorial/uiswing/concurrency/. – VGR Dec 11 '18 at 15:49
  • Then try to use `SwingUtilities.invokeLater` although I don't understand why it doesn't work for you. For me it works as expected – UninformedUser Dec 11 '18 at 16:13
  • I run my application with `invokeLater` from the `main()` method. – Alex M Dec 11 '18 at 18:01
  • For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Dec 11 '18 at 23:17

2 Answers2

0

Here is the NetBeans code -->

//set data to jlist

 public void setList() {

    String[] listData = {"list1", "list2", "list3", "list3", "list3", "list3", "list3", "list3"};
    jList1.setListData(listData);

}

//delete data from jlist

public String[] delList() {

    ListModel<String> beforDeleteDataList = jList1.getModel();
    String[] newDataList = new String[beforDeleteDataList.getSize() - 1];  
    int beforDeleteDataListIndex = 1;
    try {
        for (int i = 0; i < (beforDeleteDataList.getSize()); i++) {

            newDataList[i] = 
            beforDeleteDataList.getElementAt(beforDeleteDataListIndex);
            beforDeleteDataListIndex++;
        }

    } catch (Exception e) {
         e.printStackTrace();
    }
return newDataList;}

//delete button action

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    jList1.setListData(delList());

}  
nusaK
  • 1
  • 2
0

From your comment:

list does update after moving a window.

Verify whether you call setVisible() after adding components to the JPanel(if using it).

Check this if it relates to your problem. JPanel doesn't update until resize Jframe

Also post your full code.

Dhyey Shah
  • 582
  • 7
  • 23