JtextPane setCaretPosition()
将int作为参数,但我需要根据x和y像素坐标指定插入位置。有什么方法可以实现这个目标吗?
编辑: 我用一些原始代码替换了之前的SSCCE,以显示数据是如何进入的,因为doc字符串是硬编码时没有问题。 代码段:
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
]。