1

Hi I was doing a list of online users names using JLabel inside the JTextPane.

I used JLabel because I want the names to be clickable I was able to allign them horizontally using the styleddocument now my problem is

How can I delete the JLabel that was recently inserted ? I tried the remove method of JTextPane but it didnt work. I need to delete the JLabel when a user go offline.

My code:

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;
        }
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Dasda Sdasd
  • 73
  • 1
  • 9
  • It strikes me that it would be easier to use a scrollpane row header to achieve the same thing – MadProgrammer May 15 '13 at 03:04
  • how can I do a scrollpane row header? link please – Dasda Sdasd May 15 '13 at 03:26
  • See *How to Use Scroll Panes*. – trashgod May 15 '13 at 04:10
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson May 15 '13 at 04:23
  • 1
    That [article](http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html) mentioned by @trashgod is excellent. He usually adds the link. It is easy to find via Google (top hit) but I just wanted to add the link is http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html – Andrew Thompson May 15 '13 at 04:25
  • I cant add object of JLabel to the DefaultList why is it like that? – Dasda Sdasd May 15 '13 at 04:54
  • The default JList implementation is designed to display a list of Strings. You don't need to add a JLabel. It already supports automatic selection when you use the arrow keys to move up/down in the list or when you select a row with the mouse. – camickr May 15 '13 at 05:05

1 Answers1

0

You can remove the labels from the JTextPane by using setText("") or getStyledDocument().remove(0, doc.getLength())

if you need to get the labels this post could help: Get a component from a JTextPane through javax.swing.text.Element?

Then add the labels you want back into the JTextPane

Community
  • 1
  • 1
dinamite
  • 231
  • 2
  • 5