首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >删除最近在JTextPane上添加的JTextPane

删除最近在JTextPane上添加的JTextPane
EN

Stack Overflow用户
提问于 2013-05-15 02:39:54
回答 1查看 140关注 0票数 1

嗨,我正在做一个使用JLabel在JTextPane中使用在线用户名称的列表。

我使用JLabel是因为我希望名字是可点击的,现在我的问题是,我能够使用样式文档水平地将它们组合起来。

如何删除最近插入的JLabel?我尝试了JTextPane的删除方法,但没有起作用。当用户离线时,我需要删除JLabel。

我的代码:

代码语言:javascript
复制
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;
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2014-07-12 04:17:17

可以通过使用JTextPane或getStyledDocument().remove(0, doc.getLength())getStyledDocument().remove(0, doc.getLength())中删除标签。

如果你需要得到标签,这篇文章可能会有帮助:Get a component from a JTextPane through javax.swing.text.Element?

然后将您希望返回的标签添加到JTextPane中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16555894

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档