Thursday, 15 August 2013

java - How do I delete a row from a JList without leaving a blank space in its place? -



java - How do I delete a row from a JList without leaving a blank space in its place? -

how delete row jlist without leaving blank space in place? right delete parts need to, leave blank space in jlist in place.

here parts of code:

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(); homecoming idx; } public void populate() { list = new string [employee.count]; for(int = 0; < 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 sure want" + " 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(); }

you need custom listmodel, allow maintain "virtual" count of number of available items

when delete item (and manage listmodel, need trigger update event anyway), need collapse array, moving elements before a downwards 1 slot, example...

string list[] = new string[]{"a", "b", "c", "d", "e", "f", "g", "h"}; int = 4; system.out.println(arrays.tostring(list)); system.arraycopy(list, + 1, list, a, list.length - - 1); list[list.length - 1] = null; system.out.println(arrays.tostring(list));

which outputs...

[a, b, c, d, e, f, g, h] [a, b, c, d, f, g, h, null]

you need decrease "virtual" count of list model reports right number of items...

or bite bullet , utilize list , defaultlistmodel handles kind of stuff...

or take command , create own dedicated listmodel...

public class employeelistmodel extends abstractlistmodel<employee> { private list<employee> employees; public employeelistmodel(list<employee> employees) { this.employees = employees; } @override public int getsize() { homecoming employees.size(); } @override public employee getelementat(int index) { homecoming employees.get(index); } public void delete(employee employee) { delete(employees.indexof(employee)); } public void delete(int index) { employees.remove(index); fireintervalremoved(this, index, index); } }

see how utilize lists more details

java arrays swing jlist

No comments:

Post a Comment