0

I am using JtextPane as a JTextField to make use of Html for styling..but I cant implement prompt text functionality..here is my code...

    JTextPane txtNm = new JTextPane();
    txtNm.setContentType("text/html");
    txtNm.addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(FocusEvent arg0) {
            if(txtNm.getText().equals("<html><font face='Tw Cen MT' size='4' color='GRAY'>&nbsp;NAME</font><font color='red'>&nbsp;*</font></html>")){
                txtNm.setText("");
                //System.out.println("in txtnmfocus");
                txtNm.setForeground(Color.decode("#003366"));
            }
        }
        @Override
        public void focusLost(FocusEvent e) {
            if(txtNm.getText().isEmpty()){
                txtNm.setText("<html><font face='Tw Cen MT' size='4' color='GRAY'>&nbsp;NAME</font><font color='red'>&nbsp;*</font></html>");
            }
        }
    });

2 Answers2

0
txtNm.getText().isEmpty()

Is never empty. For HTMLEditorKit it returns at least <html><body></body></html> so is never empty.

Check what is really returned in both cases. You can try to analyse txtNm.getDocument().getLength() checking whether it's empty or not

StanislavL
  • 56,971
  • 9
  • 68
  • 98
0

Text returned by getText() should be treated with an HTML parser like Jsoup. This is because...

"<html><body></body></html>"

...and...

"<html>
    <body>
    </body>
</html>"

...for instance, correspond both to what you would call an "empty string". Though, the string aren't equal to each-other.

Sharcoux
  • 5,546
  • 7
  • 45
  • 78