JTextComponent: setNavigationFilter(NavigationFilter filter) : JTextComponent « javax.swing « Java by API






JTextComponent: setNavigationFilter(NavigationFilter filter)

  

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.NavigationFilter;
import javax.swing.text.Position;

public class MainClass {
  public static void main(String args[]) throws Exception {
    final String START_STRING = "Start\n";
    final int START_STRING_LENGTH = START_STRING.length();

    JFrame frame = new JFrame("Navigation Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextArea textArea = new JTextArea(START_STRING);
    textArea.setCaretPosition(START_STRING_LENGTH);
    JScrollPane scrollPane = new JScrollPane(textArea);
    frame.add(scrollPane, BorderLayout.CENTER);

    NavigationFilter filter = new NavigationFilter() {
      public void setDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias) {
        if (dot < START_STRING_LENGTH) {
          fb.setDot(START_STRING_LENGTH, bias);
        } else {
          fb.setDot(dot, bias);
        }
      }

      public void moveDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias) {
        if (dot < START_STRING_LENGTH) {
          fb.setDot(START_STRING_LENGTH, bias);
        } else {
          fb.setDot(dot, bias);
        }
      }
    };

    textArea.setNavigationFilter(filter);

    frame.setSize(250, 150);
    frame.setVisible(true);
  }
}

           
         
    
  








Related examples in the same category

1.JTextComponent.KeyBinding
2.JTextComponent: addCaretListener(CaretListener listener)
3.JTextComponent: getActions()
4.JTextComponent: getCaretPosition()
5.JTextComponent: getDocument()
6.JTextComponent: getHighlighter()
7.JTextComponent: getSelectedText()
8.JTextComponent: getText(int offs, int len)
9.JTextComponent: moveCaretPosition(int pos)
10.JTextComponent: print(MessageFormat headerFormat, MessageFormat footerFormat, boolean showPrintDialog, PrintService service, PrintRequestAttributeSet attributes, boolean interactive)
11.JTextComponent: read(Reader in, Object desc)
12.JTextComponent: replaceSelection(String content)
13.JTextComponent: select(int selectionStart, int selectionEnd)
14.JTextComponent: setDocument(Document doc)
15.JTextComponent: setDragEnabled(boolean b)
16.JTextComponent: setFocusAccelerator(char aKey)
17.JTextComponent: setSelectedTextColor(Color c)
18.JTextComponent: setSelectionColor(Color c)
19.JTextComponent: setSelectionEnd(int selectionEnd)
20.JTextComponent: setSelectionStart(int selectionStart)
21.JTextComponent: write(Writer out)