javax.swing.text.JTextComponent.viewToModel()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(8.3k)|赞(0)|评价(0)|浏览(160)

本文整理了Java中javax.swing.text.JTextComponent.viewToModel()方法的一些代码示例,展示了JTextComponent.viewToModel()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JTextComponent.viewToModel()方法的具体详情如下:
包路径:javax.swing.text.JTextComponent
类名称:JTextComponent
方法名:viewToModel

JTextComponent.viewToModel介绍

暂无

代码示例

代码示例来源:origin: SonarSource/sonarqube

int rowStartOffset = component.viewToModel(new Point(0, clip.y));
int endOffset = component.viewToModel(new Point(0, clip.y + clip.height));

代码示例来源:origin: stackoverflow.com

System.out.println("Y : " + y);
System.out.println("Y (pos2) : " + startIndex.y);
textArea.setCaretPosition(textArea.viewToModel(new Point(startIndex.x, y)));
pane.scrollRectToVisible(new Rectangle(startIndex.x, y));

代码示例来源:origin: stackoverflow.com

System.out.println("X : " + x);
System.out.println("Y : " + y);
int startOffset = tarea.viewToModel(new Point(x, y));
System.out.println("Start Offset : " + startOffset);
String text = tarea.getText();

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-visualweb-extension-openide

/**
 * called to set the insertion location to match the current
 * mouse pointer coordinates.
 */
protected void updateInsertionLocation(JComponent comp, Point p) {
  JTextComponent c = (JTextComponent) comp;
  c.setCaretPosition(c.viewToModel(p));
}

代码示例来源:origin: org.orbisgis/sqlconsole-parser

@Override
public List getCompletionsAt(JTextComponent jTextComponent, Point point) {
  int pos = jTextComponent.viewToModel(point);
  if(pos==-1) {
    return null;
  } else {
    return getCompletionsAtIndex(jTextComponent, pos);
  }
}

代码示例来源:origin: orbisgis/orbisgis

@Override
public List getCompletionsAt(JTextComponent jTextComponent, Point point) {
  int pos = jTextComponent.viewToModel(point);
  if(pos==-1) {
    return null;
  } else {
    return getCompletionsAtIndex(jTextComponent, pos);
  }
}

代码示例来源:origin: de.sciss/syntaxpane

/**
 * Gets the column number at given position of editor.  The first column is
 * ZERO
 *
 * @return the 0 based column number
 * @throws javax.swing.text.BadLocationException
 */
public static int getColumnNumber(JTextComponent editor, int pos)
  throws BadLocationException {
  // speedup if the pos is 0
  if(pos == 0) {
    return 0;
  }
  Rectangle r = editor.modelToView(pos);
  int start = editor.viewToModel(new Point(0, r.y));
  int column = pos - start;
  return column;
}

代码示例来源:origin: de.sciss/jsyntaxpane

/**
 * Gets the column number at given position of editor.  The first column is
 * ZERO
 * @param editor
 * @param pos
 * @return the 0 based column number
 * @throws javax.swing.text.BadLocationException
 */
public static int getColumnNumber(JTextComponent editor, int pos)
  throws BadLocationException {
  // speedup if the pos is 0
  if(pos == 0) {
    return 0;
  }
  Rectangle r = editor.modelToView(pos);
  int start = editor.viewToModel(new Point(0, r.y));
  int column = pos - start;
  return column;
}

代码示例来源:origin: nz.ac.waikato.cms.weka.thirdparty/bounce

private int getLineNumber(int y) {
  int pos = editor.viewToModel(new Point(0, y));
  
  return editor.getDocument().getDefaultRootElement().getElementIndex(pos);
}

代码示例来源:origin: nz.ac.waikato.cms.weka.thirdparty/bounce

private int getLineNumber(int y) {
  int pos = editor.viewToModel(new Point(0, y));
  return editor.getDocument().getDefaultRootElement().getElementIndex(pos);
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-spellchecker

private int[] computeVisibleSpan() {
  Component parent = pane.getParent();
  if (parent instanceof JViewport) {
    JViewport vp = (JViewport) parent;
    Point start = vp.getViewPosition();
    Dimension size = vp.getExtentSize();
    Point end = new Point((int) (start.getX() + size.getWidth()), (int) (start.getY() + size.getHeight()));
    int startPosition = pane.viewToModel(start);
    int endPosition = pane.viewToModel(end);
    if (parentWithListener != vp) {
      vp.addChangeListener(WeakListeners.change(this, vp));
      parentWithListener = vp;
    }
    return new int[] {startPosition, endPosition};
  }
  return new int[] {0, pane.getDocument().getLength()};
}

代码示例来源:origin: stackoverflow.com

import java.awt.*;
import javax.swing.*;
import javax.swing.text.DefaultHighlighter;

public class TextAreaLine
{
  public static void main(String[] args) throws Exception
  {
    String string = "Lorem ipsum eum putant gubergren evertitur in, no assueverit vituperatoribus eum. Ea cibo offendit vim, est et vivendum qualisque prodesset. Vis doctus expetenda contentiones an, no ius mazim epicuri expetendis, saperet salutandi forensibus ne usu. Ex fugit alterum usu. His ignota cotidieque in, augue erroribus eam no.";
    JTextArea textArea = new JTextArea(string);
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    JScrollPane textAreaScroll = new JScrollPane(textArea);

    JFrame frame = new JFrame();
    frame.setLayout(new BorderLayout());
    frame.add(textAreaScroll, BorderLayout.CENTER);
    frame.setSize(400, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    String term = "qualisque";
    int termOffset = string.indexOf(term);
    Rectangle view = textArea.modelToView(termOffset);
    int startOffset = textArea.viewToModel(new Point(0, view.y));
    int endOffset = textArea.viewToModel(new Point(textArea.getSize().width, view.y) );
    textArea.getHighlighter().addHighlight(startOffset, endOffset, new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW));
  }
}

代码示例来源:origin: stackoverflow.com

int termOffset = string.indexOf(term);
Rectangle view = textArea.modelToView(termOffset);
int startOffset = textArea.viewToModel(new Point(0, view.y));
int endOffset = textArea.viewToModel(new Point(textArea.getSize().width, view.y));
textArea.getHighlighter().addHighlight(startOffset, endOffset, new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW));

代码示例来源:origin: de.sciss/jsyntaxpane

/**
 * Get the closest position within the document of the component that
 * has given line and column.
 * @param editor
 * @param line the first being 1
 * @param column the first being 1
 * @return the closest positon for the text component at given line and
 * column
 */
public static int getDocumentPosition(JTextComponent editor, int line,
  int column) {
  int lineHeight = editor.getFontMetrics(editor.getFont()).getHeight();
  int charWidth = editor.getFontMetrics(editor.getFont()).charWidth('m');
  int y = line * lineHeight;
  int x = column * charWidth;
  Point pt = new Point(x, y);
  int pos = editor.viewToModel(pt);
  return pos;
}

代码示例来源:origin: de.sciss/syntaxpane

/**
 * Get the closest position within the document of the component that
 * has given line and column.
 *
 * @param line the first being 1
 * @param column the first being 1
 * @return the closest position for the text component at given line and
 * column
 */
public static int getDocumentPosition(JTextComponent editor, int line,
  int column) {
  int lineHeight = editor.getFontMetrics(editor.getFont()).getHeight();
  int charWidth = editor.getFontMetrics(editor.getFont()).charWidth('m');
  int y = line * lineHeight;
  int x = column * charWidth;
  Point pt = new Point(x, y);
  int pos = editor.viewToModel(pt);
  return pos;
}

代码示例来源:origin: nz.ac.waikato.cms.weka.thirdparty/bounce

private void selectLineForOffset(int y) {
    int pos = editor.viewToModel(new Point(0, y));
    
    if (pos >= 0) {
      Element root = editor.getDocument().getDefaultRootElement();
      Element elem = root.getElement(root.getElementIndex(pos));
  
      if (elem != null) {
        int start = elem.getStartOffset();
        int end = elem.getEndOffset();
        
        editor.select(start, Math.max(end-1, 0));
      }
    }
  }
}

代码示例来源:origin: mucommander/mucommander

int rowStartOffset = component.viewToModel( new Point(0, clip.y) );
int endOffset = component.viewToModel( new Point(0, clip.y + clip.height) );

代码示例来源:origin: org.gosu-lang.gosu/gosu-editor

@Override
protected void handleRightClick( int iXPos, int iYPos, Component component )
{
 Point pt = new Point( iXPos, iYPos );
 int iPosition = _owner.getEditor().viewToModel( pt );
 if( iPosition < 0 )
 {
  iPosition = _owner.getEditor().getDocument().getLength() - 1;
 }
 if( !isInSelection( iPosition ) )
 {
  _owner.getEditor().setCaretPosition( iPosition );
 }
 _owner.getEditor().requestFocus();
 super.handleRightClick( iXPos, iYPos, component );
}

代码示例来源:origin: hneemann/Digital

int rowStartOffset = component.viewToModel(new Point(0, clip.y));
int endOffset = component.viewToModel(new Point(0, clip.y + clip.height));

代码示例来源:origin: freeplane/freeplane

protected void redispatchKeyEvents(final JTextComponent textComponent, final KeyEvent firstKeyEvent) {
  final EventBuffer keyEventDispatcher = MTextController.getController().getEventQueue();
  if (textComponent.hasFocus()) {
    keyEventDispatcher.deactivate();
    return;
  }
  keyEventDispatcher.activate();
  keyEventDispatcher.setTextComponent(textComponent);
  if (firstKeyEvent == null) {
    return;
  }
  if (firstKeyEvent.getKeyChar() == KeyEvent.CHAR_UNDEFINED) {
    switch (firstKeyEvent.getKeyCode()) {
      case KeyEvent.VK_HOME:
        final int modelIdx = textComponent.viewToModel(new Point(0, 0));
        if (modelIdx >= 0) // modelIdx is -1 for LaTeX formulas!
          textComponent.setCaretPosition(modelIdx);
        //					firstKeyEvent.consume();
        break;
      case KeyEvent.VK_END:
        textComponent.setCaretPosition(textComponent.getDocument().getLength());
        //					firstKeyEvent.consume();
        break;
    }
  }
  else {
    textComponent.selectAll();
  }
}

相关文章

JTextComponent类方法