text selection conflict between JTextPane and JTextField

后端 未结 4 493
-上瘾入骨i
-上瘾入骨i 2021-01-27 03:43

Why the text in JTextPane cannot be selected programmatically if there is a JTextField present? Has something to do with focus i think. Thx.

import java.awt.Flow         


        
4条回答
  •  别那么骄傲
    2021-01-27 04:29

    Maybe you should be using a Highlighter so the highlighting appears in all text components:

    Highlighter.HighlightPainter yellow = 
        new DefaultHighlighter.DefaultHighlightPainter( Color.YELLOW );
    
    try
    {
        textPane.getHighlighter().addHighlight(2, 4, yellow);
    }
    catch(BadLocationException ble) { System.out.println(ble); }
    

提交回复
热议问题