2013-05-15 58 views
1

您好我正在使用JTextPane內部的JLabel做在線用戶名列表。最近刪除JTextPane上的JLabel

我用的JLabel,因爲我想的名字是點擊我能對他們水平allign使用StyledDocument中的我現在的問題是

如何刪除這是最近插入JLabel的?我嘗試了JTextPane的remove方法,但它沒有奏效。我需要在用戶下線時刪除JLabel。

我的代碼:

public static void getUsernames() 
{ 
    try{ 
    String query = "SELECT username FROM members WHERE 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; 
     } 
    } 
} 
+0

它讓我感到更容易使用scrollpane行標題來實現同樣的事情 – MadProgrammer

+0

我會使用JList。 – camickr

+0

我該如何做一個滾動窗格行標題?請鏈接 –

回答