Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

31
LINES

< > BotCompany Repo | #1008677 // Test HighlightCaret (keeps showing selection of JTextField when not in focus)

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Libraryless. Click here for Pure Java version (2213L/15K).

1  
!7
2  
3  
p-awt {
4  
  JTextField tf1 = jtextfield();
5  
  JTextField tf2 = jtextfield();
6  
  for (JTextField tf : ll(tf1, tf2))
7  
    tf.setCaret(new HighlightCaret);
8  
  showFrame(vgrid(tf1, tf2));
9  
}
10  
11  
sclass HighlightCaret extends DefaultCaret {
12  
    private static final Highlighter.HighlightPainter unfocusedPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.RED);
13  
    private static final Highlighter.HighlightPainter focusedPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
14  
    private static final long serialVersionUID = 1L;
15  
    private boolean isFocused;
16  
17  
    @Override
18  
    protected Highlighter.HighlightPainter getSelectionPainter() {
19  
        setBlinkRate(500); // otherwise is disabled, stopped
20  
        return isFocused ? focusedPainter/*super.getSelectionPainter()*/ : unfocusedPainter;
21  
    }
22  
23  
    @Override
24  
    public void setSelectionVisible(boolean hasFocus) {
25  
        if (hasFocus != isFocused) {
26  
            isFocused = hasFocus;
27  
            super.setSelectionVisible(false);
28  
            super.setSelectionVisible(true);
29  
        }
30  
    }
31  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1008677
Snippet name: Test HighlightCaret (keeps showing selection of JTextField when not in focus)
Eternal ID of this version: #1008677/1
Text MD5: f1f4f3be686b71cb03c3584f0ea97500
Transpilation MD5: fcc95fbb5f5c2c7d582cf128385c2aae
Author: stefan
Category: javax / gui
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-05-28 07:03:50
Source code size: 1101 bytes / 31 lines
Pitched / IR pitched: No / No
Views / Downloads: 471 / 1112
Referenced in: [show references]