2014-11-14 37 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