[英]Is it possible to specify the caret position of JTextPane in terms of X and Y coordinates somehow?
JtextPane setCaretPosition()
將int作為參數,但是我需要根據x和y像素坐標指定插入符號的位置。 有什么方法可以實現這一目標?
編輯:我已經用一些原始代碼替換了以前的SSCCE,以顯示數據是如何輸入的,因為在對文檔字符串進行硬編碼時沒有問題。 代碼段:
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.StyledDocument;
public class Mock extends JDialog{
JFrame f = new JFrame("My mock ");
JTextPane contentPlaceHolder = new JTextPane();
StyledDocument doc = new DefaultStyledDocument();
String content = "This is line 1\n"+
"This is line 2\n" +
"This is line 3\n"+
"This is line 4\n" +
"This is line 5\n";
int x, y;
OutputStream out;
InputStream in;
KeyAdapter kl = new KeyAdapter()
{
public void keyTyped(KeyEvent e){
int c = e.getKeyChar();
try
{
out.write(c);
}
catch (Exception ex)
{
System.out.println("Exception occured in key Typed event:\n" + ex.getMessage());
}
e.consume();
}
};
private void initGUI() throws BadLocationException{
new RemoteConsumer().start();
contentPlaceHolder.addKeyListener(kl);
doc.insertString(0, content, null);
contentPlaceHolder.setDocument(doc);
f.getContentPane().add(contentPlaceHolder, BorderLayout.CENTER);
contentPlaceHolder.setVisible(true);
pack();
f.setSize(400, 300);
f.setVisible(true);
System.out.println("Should be visible");
new RemoteConsumer().start();
}
void startGUI()
{
Runnable r = new Runnable()
{
public void run()
{
try {
initGUI();
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
SwingUtilities.invokeLater(r);
}
class RemoteConsumer extends Thread
{
char[][] lines = new char[y][];
int posy = 0;
int posx = 0;
private void addText(byte[] data, int len) throws BadLocationException
{
for (int i = 0; i < len; i++)
{
char c = (char) (data[i] & 0xff);
/**
* based on the character, there will be positioning of cursor based on posx, posy.
I want to use this posx and posy as the coordinates for caret position
* after that, buffer is appended with newly arrived character
*/
if (lines[posy] == null)
{
lines[posy] = new char[x];
for (int k = 0; k < x; k++)
lines[posy][k] = ' ';
}
lines[posy][posx] = c;
posx++;
}
StringBuffer sb = new StringBuffer(x * y);
for (int i = 0; i < lines.length; i++)
{
if (i != 0)
sb.append('\n');
if (lines[i] != null)
{
sb.append(lines[i]);
}
}
doc.insertString(doc.getLength(), sb.toString(), null);
contentPlaceHolder.setDocument(doc);
}
/**
* This is the entry point from which the data comes in
* Now, it will give error, as no data has been sent by server
*/
public void run()
{
byte[] buff = new byte[8192];
try
{
while (true)
{
int len = in.read(buff);
if (len == -1)
return;
addText(buff, len);
}
}
catch (Exception e)
{
System.out.println("Error in reading value : " + e.getMessage());
return;
}
}
}
public static void main(String[] args) throws BadLocationException
{
Mock mk = new Mock();
mk.startGUI();
}
}
請注意,對doc的值進行硬編碼不會出現問題。 僅當在窗格上顯示鍵入的字符時,才會出現此問題[命令是out.write(character)
,其中out
是OutputStream
類型的]。
JTextComponent
有一個viewToModel
方法,該方法接受一個Point
並返回Document
的int
位置。 請參閱: https : //docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#viewToModel(java.awt.Point)
對於JTextPane可以,因為它擴展了JEditorPane,后者擴展了JTextComponent。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.