java - removing recently add JLabel on JTextPane -
hi doing list of online users names using jlabel inside jtextpane.
i used jlabel because want names clickable able allign them horizontally using styleddocument problem
how can delete jlabel inserted ? tried remove method of jtextpane didnt work. need delete jlabel when user go offline.
my code:
public static void getusernames() { try{ string query = "select username members status = 'offline'"; ps3 = con.preparestatement(query); rs2 = ps3.executequery(); }catch(exception ex){ex.printstacktrace();} } public static void resultgetusername(jtextpane jtp,styleddocument sd) { try { while (rs2.next()) { final jlabel jl = new jlabel(rs2.getstring("username")); final string username = rs2.getstring("username"); border d = borderfactory.createemptyborder(1,10,1,10); border d2 = borderfactory.createlineborder(color.black); border d3 = borderfactory.createcompoundborder(d2,d); jl.setfont(new font("calibri",font.bold,16)); jl.setborder(d3); jl.setopaque(true); jl.setbackground(color.orange); jl.addmouselistener(new mouselistener(){ public void mouseclicked(mouseevent arg0) { } public void mouseentered(mouseevent arg0) { jl.setforeground(new color(30,144,255)); } public void mouseexited(mouseevent arg0) { jl.setforeground(color.black); } public void mousepressed(mouseevent e) { jl.setforeground(new color(210,105,30)); jl.setbackground(new color(154,205,50)); } public void mousereleased(mouseevent e) { jl.setbackground(color.orange); jl.setforeground(color.black); if(e.getclickcount() ==2) new onetoonechat(username); } }); cursor c = new cursor(cursor.hand_cursor); jl.setcursor(c); jtp.insertcomponent(jl); sd.insertstring(sd.getlength(), "\n", subpanel1.sas); } } catch (sqlexception e) { } catch (badlocationexception e) { } finally{ if (rs2 != null) { try { rs2.close(); } catch (sqlexception sqlex) { } rs2 = null; } if (ps3 != null) { try { ps3.close(); } catch (sqlexception sqlex) { } ps3 = null; } } }
you can remove labels jtextpane using settext("")
or getstyleddocument().remove(0, doc.getlength())
if need labels post help: get component jtextpane through javax.swing.text.element?
then add labels want jtextpane
Comments
Post a Comment