如何从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