0

Hello I am trying to display one String ("Character Count: ") and one dynamic character count on the bottom of JTextArea. When I run this code below, there is a panel that opens up without characterCountTitle. Only when I start typing, characterCountTitle displays and the number is correctly dynamic.

My goal is to show characterCountTitle (string + character count) as soon as the panel is open to users.

private void initComponents() {
    this.notePanel.getNoteDocument().addDocumentListener(new DocumentListener() {

        TitledBorder characterCountTitle;
        Border emptyBorder;

        public void insertUpdate(DocumentEvent e) {
            displayEditInfo(e);
        }

        public void removeUpdate(DocumentEvent e) {
            displayEditInfo(e);
        }

        public void changedUpdate(DocumentEvent e) {
            displayEditInfo(e);
        }

        private void displayEditInfo(DocumentEvent e) {
            Document document = e.getDocument();

            emptyBorder = BorderFactory.createEmptyBorder();

            //displays a string of "Character Count: " and another string of dynamic character count 
            characterCountTitle = BorderFactory.createTitledBorder(emptyBorder, "Character Count: " +  document.getLength());
            characterCountTitle.setTitlePosition(TitledBorder.BOTTOM);
            panel.setBorder(characterCountTitle);
        }
    });

    this.panel.add(notePanel, BorderLayout.CENTER);
    this.panel.add(navigation.buildPanel(), BorderLayout.SOUTH);
}

Due to this issue, I was trying to create two titles; one for string(outside of addDocumentListener) and one for character count (inside displayEditInfo method), but it messes up the variable scope.

I'd greatly appreciate your input!

In-young Choung
  • 779
  • 2
  • 8
  • 22

1 Answers1

3

You may simply create and add your border outside of the DocumentListener, and just change the title text on document events :

private void initComponents() {


        Border emptyBorder = BorderFactory.createEmptyBorder();
        final TitledBorder characterCountTitle = BorderFactory.createTitledBorder(emptyBorder, "Character Count:");
        characterCountTitle.setTitlePosition(TitledBorder.BOTTOM);
        panel.setBorder(characterCountTitle);

        this.notePanel.getNoteDocument().addDocumentListener(new DocumentListener()                 {

        public void insertUpdate(DocumentEvent e) {
            displayEditInfo(e);
        }

        public void removeUpdate(DocumentEvent e) {
            displayEditInfo(e);
        }

        public void changedUpdate(DocumentEvent e) {
            displayEditInfo(e);
        }

        private void displayEditInfo(DocumentEvent e) {
            Document document = e.getDocument();


            //displays a string and dynamic character count
            characterCountTitle.setTitle("Character Count: " +  document.getLength());
            panel.repaint();

        }
    });

    this.panel.add(notePanel, BorderLayout.CENTER);
    this.panel.add(navigation.buildPanel(), BorderLayout.SOUTH);
}
Arnaud
  • 17,229
  • 3
  • 31
  • 44
  • Thank you for the quick response. I tried the code you provided and it displays "Character Count: " as soon as the panel is open. However there is No dynamic character count; document.getLength() displayed both before and after typing. – In-young Choung Dec 21 '16 at 15:45
  • 1
    @In-youngChoung, The dynamic part worked before, so you must have changed something else. We can't tell the exact context of how your code is executed. So it is up to you to understand the suggestion and fully implement it properly. The suggestion was basically to do what you were doing before but add the Border to the panel as soon as the panel is created. Then you never need to recreate the Border, just change the text of the Border. If you need more help then post a proper [mcve] that demonstrates the problem, we can't guess exactly what you changed.. – camickr Dec 21 '16 at 15:53
  • 1
    Note, you may also need to invoke `repaint()` on the panel after the Border text is changed to make sure the Border get repainted. – camickr Dec 21 '16 at 15:56
  • 1
    @camickr : Thanks for the `repaint`, I was also about to add this part after having read this : http://stackoverflow.com/questions/6566612/delayed-titledborder-title-update-refresh-why – Arnaud Dec 21 '16 at 15:57
  • 1
    It was repaint method that was missing!! It works so beautifully now. Thank you both! :) – In-young Choung Dec 21 '16 at 16:19