简体   繁体   中英

How do I delete a row from a JList without leaving a blank space in its place?

How do I delete a row from a JList without leaving a blank space in its place? Right now it will delete the parts I need it to, but will then leave a blank space in the jlist in its place.

Here are some parts of my code:

private final DefaultListModel model = new DefaultListModel();
protected JList myList = new JList(model);;    
protected static String employee[] = new String[100];       
protected String list[];    


public EmployeeGUI()
{           
    scrollPane.setViewportView(myList);
    populate();
    myList = new JList(list); 
    myList.setFixedCellHeight(30);  
    myList.setFixedCellWidth(100);  
    myList.addListSelectionListener(this); 




public int getIndex()
{
    int idx = myList.getSelectedIndex();
    return idx;
}

public void populate() 
{
    list = new String [Employee.count];          
    for(int i = 0; i < Employee.count; i++)
    {        
        if(Employee.employeeArray[i] != null)                       
        list[i] = Employee.employeeArray[i].getName();            
    }               
}

@Override
public void actionPerformed(ActionEvent e) 
{

     else if(e.getSource() ==jbtDelete)
    {            
        String k = JOptionPane.showInputDialog(null, "Are you sure you want"
                + " to delete?\n1: Yes\n2: No", "Delete",2);
        switch(k)
        {
            case "1":
            {                                              
                int idx = getIndex();                                                                                                
                list[idx] = null;      
                Employee.deleteEmployee(idx);   

                //populate();
                myList.setListData(list);                    
                if (idx<0) return;                                                           
                text2.setText(null);
                text3.setText(null);
                text4.setText(null);
                text5.setText(null);
                text6.setText(null);
                text7.setText(null);
                text10.setText(null);
                type.setSelectedItem("No Type");
                insurance.setSelectedItem("None");                                           
                break;
            }
            case "2":                    
                break;
            default:
                JOptionPane.showMessageDialog(null, "Invalid Selection!", 
                        "INVALID",1);
        }
    }
}
}

public static void deleteEmployee(int a)
{  
    employeeArray[a] = null;        
    //count--;          
    sortEmployees();
}  

You will need a custom ListModel , this will allow you to maintain a "virtual" count of the number of available items

When you delete an item (and I would manage this from the ListModel , as you will need to trigger an update event anyway), you will need to collapse the array, moving all the elements before a down one slot, for example...

String list[] = new String[]{"A", "B", "C", "D", "E", "F", "G", "H"};
int a = 4;

System.out.println(Arrays.toString(list));

System.arraycopy(list, a + 1, list, a, list.length - a - 1);
list[list.length - 1] = null;

System.out.println(Arrays.toString(list));

Which outputs...

[A, B, C, D, E, F, G, H]
[A, B, C, D, F, G, H, null]

You will then need to decrease the "virtual" count of the List model so that it reports the correct number of items...

Or you could just bite the bullet and use a List and DefaultListModel which handles all this kind of stuff...

Or you could take control and make your own dedicated ListModel ...

public class EmployeeListModel extends AbstractListModel<Employee> {

    private List<Employee> employees;

    public EmployeeListModel(List<Employee> employees) {
        this.employees = employees;
    }

    @Override
    public int getSize() {
        return employees.size();
    }

    @Override
    public Employee getElementAt(int index) {
        return employees.get(index);
    }

    public void delete(Employee employee) {
        delete(employees.indexOf(employee));
    }

    public void delete(int index) {
        employees.remove(index);
        fireIntervalRemoved(this, index, index);
    }

}

See How to Use Lists for more details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM