3

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
  • 321,443
  • 19
  • 166
  • 288
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Oct 31 '15 at 01:00
  • I'm new to this, so I don't quite know the rules, and embarrassingly I wouldn't call myself a particularly strong programmer despite it being my future occupation. When I change setPreferredSize to just setSize, it breaks my program completely as the scrollbar remains a fixed size that is about 30 pixels high regardless of the parameters I pass to the method. – Peter Davidson Oct 31 '15 at 17:25

1 Answers1

5

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());
camickr
  • 321,443
  • 19
  • 166
  • 288
  • In my append text method I added in: content.setCaretPosition(doc.getLength()); My attempts to break it have been fruitless which is great news. It seems very robust and I've attempted various ways to cause the issue like Alt-Tabbing, selecting areas of text, moving the scrollbar and none of them seem to work any more. Thank you for your help, its amazing that the fix was so simple! – Peter Davidson Oct 31 '15 at 17:15