Using custom Caret in JTextField for accessibility

孤街醉人 提交于 2019-12-02 07:20:29

问题


I'm programming a Swing Application and I have a friend that read much the Bible. This app have searches by text or passage.

But my friend only can see big font sizes and contrasted colors. I change the color of caret for the textField with setCaretColor, where the user input the text to search.

The caret show like a rectangle with the same hight of font, but the width is little and I think that my friend cans not see.

Is there any way for change the width of the caret for show more bigger?


回答1:


Have you seen this example from the Oreilly Swing book that's floating all over the internet? It seems you can customize the caret a lot.

http://www.java2s.com/Code/Java/Swing-JFC/Fanciercustomcaretclass.htm




回答2:


maybe there are three ways how I can reproduce Caret and selection in th JTextComponents

import java.awt.Color;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.DefaultCaret;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;

public class TextFieldExample extends JFrame {

    private static final long serialVersionUID = 1L;

    public TextFieldExample() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        JPanel panel = new JPanel();
        JTextField tfield = new JTextField(10);
        tfield.setText("default text");
        final JTextField tfield2 = new JTextField(10);
        tfield2.setText("default text");
        tfield2.addFocusListener(new FocusListener() {

            public void focusGained(FocusEvent fe) {
                tfield2.setCaretPosition(tfield2.getDocument().getLength());
            }

            public void focusLost(FocusEvent fe) {
            }
        });
        JTextField tfield3 = new JTextField(10);
        tfield3.setText("default text");
        tfield3.setCaret(new HighlightCaret());
        panel.add(tfield);
        panel.add(tfield2);
        panel.add(tfield3);
        add(panel);
        pack();
        setVisible(true);
    }

    class HighlightCaret extends DefaultCaret {

        private final Highlighter.HighlightPainter unfocusedPainter =
                new DefaultHighlighter.DefaultHighlightPainter(new Color(230, 230, 210));
        private static final long serialVersionUID = 1L;
        private boolean isFocused;

        @Override
        protected Highlighter.HighlightPainter getSelectionPainter() {
            return isFocused ? super.getSelectionPainter() : unfocusedPainter;
        }

        @Override
        public void setSelectionVisible(boolean hasFocus) {
            if (hasFocus != isFocused) {
                isFocused = hasFocus;
                super.setSelectionVisible(false);
                super.setSelectionVisible(true);
            }
        }
    }

    public static void main(String... args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                TextFieldExample textFieldExample = new TextFieldExample();
            }
        });
    }
}



回答3:


May be this could help http://java-sl.com/tip_overwrite_mode.html



来源:https://stackoverflow.com/questions/10318728/using-custom-caret-in-jtextfield-for-accessibility

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!