I have to create a textarea and if i have typed any character in the textarea it has to check in the dictionary.jar file and it has to display the related words in the list attached to that textarea
How to create a textarea and append the list to it?
Collapse
X
-
Tags: None
-
Code:import javax.swing.*; import org.languagetool.JLanguageTool; import org.languagetool.Language; import org.languagetool.rules.RuleMatch; import java.awt.*; import java.awt.event.*; import java.io.IOException; import java.util.List; public class Check implements KeyListener { private JFrame f; // Main frame private JTextArea ta, tSuggest; // Text area private JScrollPane sbrText, suggestionsText; // Scroll pane for text area private JButton btnQuit; // Quit Program private Object language; private JLanguageTool langTool; public Check() { // Constructor // Create Frame try { langTool = new JLanguageTool(Language.AMERICAN_ENGLISH); langTool.activateDefaultPatternRules(); } catch (IOException e1) { e1.printStackTrace(); } f = new JFrame("Enter the text"); f.getContentPane().setLayout(new FlowLayout()); // Create Scrolling Text Area in Swing ta = new JTextArea("", 10, 50); ta.setLineWrap(true); tSuggest = new JTextArea("", 10, 50); tSuggest.setLineWrap(true); sbrText = new JScrollPane(ta); sbrText .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); ta.addKeyListener(this); suggestionsText = new JScrollPane(tSuggest); suggestionsText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); // Create Quit Button btnQuit = new JButton("Quit"); btnQuit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); } public void keyTyped(KeyEvent e) { } public void keyReleased(KeyEvent e) { } public void keyPressed(KeyEvent e) { if (e.getSource() == ta) { if ((e.getKeyCode() == KeyEvent.VK_SPACE) || (e.getKeyCode() == KeyEvent.VK_ENTER)) { String text = ta.getText(); int lastWordPos = text.trim().lastIndexOf(" ") + 1; //System.out.println(text.substring(lastWordPos)); tSuggest.setText(text.substring(lastWordPos)+"\n"); List<RuleMatch> word=null; try { word = langTool.check(text); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } for (RuleMatch match : word) { // System.out.println("Potential error at line " // + match.getEndLine() + ", column " // + match.getColumn() + ": " + match.getMessage()); // System.out.println("Suggested correction: " // + match.getSuggestedReplacements()); tSuggest.setText(tSuggest.getText()+"\n"+"Potential error at line " + match.getEndLine() + ", column " + match.getColumn() + ": " + match.getMessage()); tSuggest.setText(tSuggest.getText()+"\n"+"Suggested correction: " + match.getSuggestedReplacements()); } } } } public void launchFrame() { // Create Layout // Add text area and button to frame f.getContentPane().add(sbrText); f.getContentPane().add(suggestionsText); f.getContentPane().add(btnQuit); // Close when the close button is clicked f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Display Frame f.pack(); // Adjusts frame to size of components f.setVisible(true); } public static void main(String args[]) { Check gui = new Check(); gui.launchFrame(); } }Comment
Comment