[英]How to read last word or latest word in JTextArea
I am making a Source code Editor using JtextArea.In that I want to do the following task." While user typing on Editor window(JtextArea),each and every time the updated text(last word) should compare with the set of words in the database, if it matches any one of the word then the definition will be open in new popup frame." 我正在使用JtextArea进行源代码编辑。我要执行以下任务。“在用户在“编辑器”窗口(JtextArea)上键入内容时,每一次更新后的文本(最后一个单词)应与其中的单词集进行比较数据库,如果它与单词中的任何一个匹配,则定义将在新的弹出框中打开。” My coding is like following 我的编码如下
String str = textarea.getText();
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
String stm="select url from pingatabl where functn=?";
PreparedStatement st = conn.prepareStatement(stm);
st.setString(1, str);
//Excuting Query
ResultSet rs = st.executeQuery();
if (rs.next()) {
String s = rs.getString(1);
//Sets Records in frame
JFrame fm = new JFrame();
fm.setVisible(true);
fm.setSize(500,750);
JEditorPane jm = new JEditorPane();
fm.add(jm);
jm.setPage(ClassLoader.getSystemResource(s));
In the above coding String str = textarea.getText();
在上面的编码中, String str = textarea.getText();
reads all the text in the textarea.. but i need to get last word only. 读取textarea中的所有文本。.但是我只需要得到最后一个单词。 How can i get latest word from JTextArea.. 我如何从JTextArea获取最新单词。
Use a DocumentListener
to monitor for changes to the text component and use javax.swing.text.Utilities
to calculate the start/end index of the word in the Document
, from which you can the extract the result 使用DocumentListener
监视文本组件的更改,并使用javax.swing.text.Utilities
计算Document
单词的开始/结束索引,从中可以提取结果
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Utilities;
public class TheLastWord {
public static void main(String[] args) {
new TheLastWord();
}
public TheLastWord() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout());
JTextArea ta = new JTextArea(10, 20);
add(new JScrollPane(ta));
JLabel lastWord = new JLabel("...");
add(lastWord, BorderLayout.SOUTH);
ta.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
checkLastWord();
}
@Override
public void removeUpdate(DocumentEvent e) {
checkLastWord();
}
@Override
public void changedUpdate(DocumentEvent e) {
checkLastWord();
}
protected void checkLastWord() {
try {
int start = Utilities.getWordStart(ta, ta.getCaretPosition());
int end = Utilities.getWordEnd(ta, ta.getCaretPosition());
String text = ta.getDocument().getText(start, end - start);
lastWord.setText(text);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
}
You can use following code block if you want to get the last word in the testarea 如果您想获得测试区域中的最后一个单词,可以使用以下代码块
String[] wordsArray = textarea.getText().split("\\s+");
String lastWord = wordsArray[wordsArray.length - 1];
But if you want to get the last updated word then you have to use a Listener for that. 但是,如果要获取最后更新的单词,则必须使用侦听器。 Check on DocumentListener
and DocumentEvent
http://docs.oracle.com/javase/7/docs/api/javax/swing/event/DocumentListener.html http://docs.oracle.com/javase/7/docs/api/javax/swing/event/DocumentEvent.html 检查DocumentListener
和DocumentEvent
http://docs.oracle.com/javase/7/docs/api/javax/swing/event/DocumentListener.html http://docs.oracle.com/javase/7/docs/api/使用javax /秋千/事件/ DocumentEvent.html
To get the last line in your JEditorPane, split the text in the editor on \\n as shown below: 要获得JEditorPane中的最后一行,请在编辑器中的\\ n上拆分文本,如下所示:
String text = editor.getText();
String[] lines = text.split("\n");
String lastLine = lines[lines.length-1]; 字符串lastLine = lines [lines.length-1]; System.out.println("Last line: " + lastLine); System.out.println(“最后一行:” + lastLine);
Similarly, to get the last word, split the last line on space. 同样,要获取最后一个单词,请在空间上拆分最后一行。
Here a code of method that returns the last word of a text 这里的方法代码返回文本的最后一个单词
public static String getLastWord(String s) {
int endOfLine = s.length() - 1;
boolean start = false;
while (!start && endOfLine >= 0) {
if (!Character.isLetter(s.charAt(endOfLine))) {
endOfLine--;
} else {
start = true;
}
}
final StringBuilder lastWord = new StringBuilder("");
while (start && endOfLine >= 0) {
if (!Character.isLetter(s.charAt(endOfLine))) {
start = false;
} else {
lastWord.insert(0, s.charAt(endOfLine));
endOfLine--;
}
}
return lastWord.toString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.