Java JTextPane auto-scroll stops working after selecting text

Peter Davidson

So this issue has been bugging me for a long time now, and it effectively renders my game unplayable if it is triggered. The situation is that I have four items in my GUI:

private JPanel panel;
private JTextPane content;
private JScrollPane scroll;
private JTextField input;

The whole thing is setup in a BorderLayout, with a caret update policy that automatically scrolls the screen whenever text reaches the bottom. However, if I select any text in the JTextPane, all of a sudden the autoscroll stops working, and any new text added to the pane remains invisible until the user manually scrolls the scrollbar. I've attempted reapplying the caret update policy every time text is appended but that didn't work. I haven't a clue how to fix this, and attempts to google the problem have been fruitless. For reference, here is the relevant code from the constructor:

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(width, height);
setResizable(false);
panel = new JPanel();

input = new JTextField(30);
input.setBackground(Color.BLACK);
input.setForeground(Color.GREEN);
input.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, width / 40));
input.addActionListener(this);

content = new JTextPane();
scroll = new JScrollPane(content);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setPreferredSize(new Dimension(width, height - 80));
scroll.setMinimumSize(new Dimension(640, 480));
scroll.setBorder(null);

content.setBackground(Color.BLACK);
content.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, width / 40));
content.setEditable(false);

DefaultCaret caret = (DefaultCaret) content.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

panel.setLayout(new BorderLayout());
panel.add(scroll, BorderLayout.NORTH);
panel.add(input, BorderLayout.SOUTH);
panel.setBackground(Color.BLACK);

getContentPane().add(panel);

setVisible(true);

Is there a possible solution to this, or is this a limitation of Java AWT?

camickr

I've attempted reapplying the caret update policy every time text is appended but that didn't work.

You also need to reset the caret to the end of the Document:

textPane.getDocument().insertString(...);
textPane.setCaretPosition(textArea.getDocument().getLength());

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Vscode auto format code stops working after few runs

分類Dev

Android app stops working after moving to using creating the layout with java

分類Dev

Touchpad Problem - shortly after login, it stops working

分類Dev

XFCE 4.10 panel auto hide feature stops working on occasions

分類Dev

App auto closes after selecting list view item

分類Dev

Text cursor for JTextPane with HTML

分類Dev

Selecting ComboBox option on key press. I would like the selection model to auto scroll/jump to the selected option

分類Dev

selenium chrome driver stops working after publish application

分類Dev

NativeScript background service stops working after app closes

分類Dev

Zurb Foundation Offcanvas Menu stops working after toggling programmatically

分類Dev

Calling VBScript procedure stops working after adding jQuery to HTA

分類Dev

Linewrap is not working in JTextPane with arabic letters

分類Dev

Jquery not working after page one on website with Infinite Scroll

分類Dev

Node pipe stops working

分類Dev

Program compiles, but then stops working

分類Dev

16.04, Network stops working

分類Dev

Auto selecting a row in kendo grid

分類Dev

Cyborg R.A.T 3 gaming mouse stops working after a while and/or misbehaves

分類Dev

Java Swing JTextPane text / htmlが無効な属性を受け入れない

分類Dev

Scraping an infinite scroll page stops without scrolling

分類Dev

Search stops working for "Entire Solution"

分類Dev

Search stops working for "Entire Solution"

分類Dev

Search stops working for "Entire Solution"

分類Dev

Javascript stops working with form tag

分類Dev

Reading from file stops working

分類Dev

Changes textbox text value from the array after selecting item from listview

分類Dev

Auto scroll div does not work

分類Dev

For Loop Stops after First Iteration

分類Dev

Skype stops responding after a while

Related 関連記事

  1. 1

    Vscode auto format code stops working after few runs

  2. 2

    Android app stops working after moving to using creating the layout with java

  3. 3

    Touchpad Problem - shortly after login, it stops working

  4. 4

    XFCE 4.10 panel auto hide feature stops working on occasions

  5. 5

    App auto closes after selecting list view item

  6. 6

    Text cursor for JTextPane with HTML

  7. 7

    Selecting ComboBox option on key press. I would like the selection model to auto scroll/jump to the selected option

  8. 8

    selenium chrome driver stops working after publish application

  9. 9

    NativeScript background service stops working after app closes

  10. 10

    Zurb Foundation Offcanvas Menu stops working after toggling programmatically

  11. 11

    Calling VBScript procedure stops working after adding jQuery to HTA

  12. 12

    Linewrap is not working in JTextPane with arabic letters

  13. 13

    Jquery not working after page one on website with Infinite Scroll

  14. 14

    Node pipe stops working

  15. 15

    Program compiles, but then stops working

  16. 16

    16.04, Network stops working

  17. 17

    Auto selecting a row in kendo grid

  18. 18

    Cyborg R.A.T 3 gaming mouse stops working after a while and/or misbehaves

  19. 19

    Java Swing JTextPane text / htmlが無効な属性を受け入れない

  20. 20

    Scraping an infinite scroll page stops without scrolling

  21. 21

    Search stops working for "Entire Solution"

  22. 22

    Search stops working for "Entire Solution"

  23. 23

    Search stops working for "Entire Solution"

  24. 24

    Javascript stops working with form tag

  25. 25

    Reading from file stops working

  26. 26

    Changes textbox text value from the array after selecting item from listview

  27. 27

    Auto scroll div does not work

  28. 28

    For Loop Stops after First Iteration

  29. 29

    Skype stops responding after a while

ホットタグ

アーカイブ