2014-11-14 40 views
-1

如何从JList中删除一行而不留下空白处? 现在它会删除我需要的部分,但会在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(); 
} 
+1

这是一个很大的代码请人这么短的问题涉水通过见How to Use Lists。你可以把这个提炼成一个[MCVE](http://stackoverflow.com/help/mcve)吗? – Air

+0

你也可以考虑使用'showConfirmDialog'或甚至'showOptionDialog'而不是'showInputDialog'。有关更多详细信息,请参阅[如何制作对话框](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) – MadProgrammer

回答

2

您将需要一个定制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); 
    } 

} 

更多细节

+0

+1 ==或者您可以咬住子弹并使用List和处理所有这些东西的DefaultListModel ... – mKorbel