如何從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();
}
這是一個很大的代碼請人這麼短的問題涉水通過見How to Use Lists。你可以把這個提煉成一個[MCVE](http://stackoverflow.com/help/mcve)嗎? – Air
你也可以考慮使用'showConfirmDialog'或甚至'showOptionDialog'而不是'showInputDialog'。有關更多詳細信息,請參閱[如何製作對話框](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) – MadProgrammer