Java 如何从JList中删除一行而不在其位置留下空白?
如何从JList中删除一行而不在其位置留下空白? 现在,它将删除我需要的部分,但会在jlist中的相应位置留下空白 以下是我的代码的一些部分:Java 如何从JList中删除一行而不在其位置留下空白?,java,arrays,swing,jlist,Java,Arrays,Swing,Jlist,如何从JList中删除一行而不在其位置留下空白? 现在,它将删除我需要的部分,但会在jlist中的相应位置留下空白 以下是我的代码的一些部分: private final DefaultListModel model = new DefaultListModel(); protected JList myList = new JList(model);; protected static String employee[] = new String[100]; protec
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中管理它,因为您无论如何都需要触发一个更新事件,您需要折叠数组,将所有元素移动到一个向下的插槽之前,例如
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和DefaultListModel来处理所有这类事情
或者你可以控制并制作自己的专用列表模型
有关更多详细信息,请参见,对于这样一个简短的问题,有很多代码可以让人们费尽心思来回答。你能把它分解成一个吗?你也可以考虑使用SaleRealdMulk对话框,甚至是SeopopTeXT对话框,而不是StutiPutCudio。请参阅以了解更多详细信息+1==或者您可以咬紧牙关,使用一个列表和DefaultListModel来处理所有此类内容。。。
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);
}
}