如何在JTextArea中读取最后一个单词或最新单词 [英] How to read last word or latest word in JTextArea
问题描述
我正在使用JtextArea进行源代码编辑.为此,我想执行以下任务.当用户在编辑器"窗口(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." 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));
在上面的编码中,String str = textarea.getText();
读取textarea中的所有文本..但是我只需要获取最后一个单词.我如何从JTextArea获取最新单词.
In the above coding String str = textarea.getText();
reads all the text in the textarea.. but i need to get last word only. How can i get latest word from JTextArea..
推荐答案
如果要获取测试区域的最后一个单词,可以使用以下代码块
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];
但是,如果您想获取最后更新的单词,则必须使用监听器.
检查DocumentListener
和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
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
这篇关于如何在JTextArea中读取最后一个单词或最新单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!