swing - Java JTextPane auto-scroll stops working after selecting text -
so issue has been bugging me long time now, , renders game unplayable if triggered. situation have 4 items in gui:
private jpanel panel; private jtextpane content; private jscrollpane scroll; private jtextfield input;
the whole thing setup in borderlayout, caret update policy automatically scrolls screen whenever text reaches bottom. however, if select text in jtextpane, of sudden autoscroll stops working, , new text added pane remains invisible until user manually scrolls scrollbar. i've attempted reapplying caret update policy every time text appended didn't work. haven't clue how fix this, , attempts google problem have been fruitless. reference, here relevant code 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 possible solution this, or limitation of java awt?
i've attempted reapplying caret update policy every time text appended didn't work.
you need reset caret end of document:
textpane.getdocument().insertstring(...); textpane.setcaretposition(textarea.getdocument().getlength());
Comments
Post a Comment