繁体   English   中英

如何从JList删除一行而不在其空白处留空白?

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

如何从JList删除一行而不在其空白处留空白? 现在,它将删除我需要的部分,但是将在jlist中的位置保留空白。

这是我的代码的某些部分:

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

您将需要一个自定义ListModel ,这将允许您维护可用项目数量的“虚拟”计数

当你删除一个项目(我会从管理这个ListModel ,因为你需要无论如何触发更新事件),则需要折叠阵列,之前移动所有的元素a下移一行,例如...

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));

哪个输出...

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

然后,您将需要减少List模型的“虚拟”计数,以便它报告正确的项目数...

或者,您也可以硬着头皮,使用ListDefaultListModel处理所有此类问题...

或者您可以控制并制作自己的专用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);
    }

}

有关更多详细信息,请参见如何使用列表

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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