By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,030 Members | 1,227 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,030 IT Pros & Developers. It's quick & easy.

How to create a textarea and append the list to it?

P: 2
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
Dec 7 '12 #1
Share this Question
Share on Google+
4 Replies


gits
Expert Mod 5K+
P: 5,390
what have you done so far? please post that code - so that we have something to work with.
Dec 7 '12 #2

P: 2
Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2.  
  3. import org.languagetool.JLanguageTool;
  4. import org.languagetool.Language;
  5. import org.languagetool.rules.RuleMatch;
  6.  
  7. import java.awt.*;
  8. import java.awt.event.*;
  9. import java.io.IOException;
  10. import java.util.List;
  11.  
  12. public class Check implements KeyListener {
  13.     private JFrame f; // Main frame
  14.     private JTextArea ta, tSuggest; // Text area
  15.     private JScrollPane sbrText, suggestionsText; // Scroll pane for text area
  16.     private JButton btnQuit; // Quit Program
  17.  
  18.     private Object language;
  19.     private JLanguageTool langTool;
  20.  
  21.     public Check() { // Constructor
  22.         // Create Frame
  23.         try {
  24.             langTool = new JLanguageTool(Language.AMERICAN_ENGLISH);
  25.             langTool.activateDefaultPatternRules();
  26.         } catch (IOException e1) {
  27.             e1.printStackTrace();
  28.         }
  29.         f = new JFrame("Enter the text");
  30.         f.getContentPane().setLayout(new FlowLayout());
  31.  
  32.         // Create Scrolling Text Area in Swing
  33.         ta = new JTextArea("", 10, 50);
  34.         ta.setLineWrap(true);
  35.  
  36.         tSuggest = new JTextArea("", 10, 50);
  37.         tSuggest.setLineWrap(true);
  38.         sbrText = new JScrollPane(ta);
  39.         sbrText
  40.                 .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  41.         ta.addKeyListener(this);
  42.  
  43.         suggestionsText = new JScrollPane(tSuggest);
  44.         suggestionsText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  45.  
  46.         // Create Quit Button
  47.         btnQuit = new JButton("Quit");
  48.         btnQuit.addActionListener(new ActionListener() {
  49.             public void actionPerformed(ActionEvent e) {
  50.  
  51.                 System.exit(0);
  52.             }
  53.         });
  54.  
  55.     }
  56.  
  57.     public void keyTyped(KeyEvent e) {
  58.     }
  59.  
  60.     public void keyReleased(KeyEvent e) {
  61.     }
  62.  
  63.     public void keyPressed(KeyEvent e) {
  64.         if (e.getSource() == ta) {
  65.             if ((e.getKeyCode() == KeyEvent.VK_SPACE)
  66.                     || (e.getKeyCode() == KeyEvent.VK_ENTER)) {
  67.                 String text = ta.getText();
  68.                 int lastWordPos = text.trim().lastIndexOf(" ") + 1;
  69.                 //System.out.println(text.substring(lastWordPos));
  70.                 tSuggest.setText(text.substring(lastWordPos)+"\n");
  71.  
  72.                 List<RuleMatch> word=null;
  73.                 try {
  74.                     word = langTool.check(text);
  75.                 } catch (IOException e1) {
  76.                     // TODO Auto-generated catch block
  77.                     e1.printStackTrace();
  78.                 }
  79.  
  80.                 for (RuleMatch match : word) {
  81. //                    System.out.println("Potential error at line "
  82. //                            + match.getEndLine() + ", column "
  83. //                            + match.getColumn() + ": " + match.getMessage());
  84. //                    System.out.println("Suggested correction: "
  85. //                            + match.getSuggestedReplacements());
  86.  
  87.                     tSuggest.setText(tSuggest.getText()+"\n"+"Potential error at line "
  88.                             + match.getEndLine() + ", column "
  89.                             + match.getColumn() + ": " + match.getMessage());
  90.                     tSuggest.setText(tSuggest.getText()+"\n"+"Suggested correction: "
  91.                             + match.getSuggestedReplacements());
  92.                 }
  93.             }
  94.         }
  95.     }
  96.  
  97.     public void launchFrame() { // Create Layout
  98.         // Add text area and button to frame
  99.         f.getContentPane().add(sbrText);
  100.         f.getContentPane().add(suggestionsText);
  101.         f.getContentPane().add(btnQuit);
  102.  
  103.         // Close when the close button is clicked
  104.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  105.  
  106.         // Display Frame
  107.         f.pack(); // Adjusts frame to size of components
  108.         f.setVisible(true);
  109.     }
  110.  
  111.     public static void main(String args[]) {
  112.         Check gui = new Check();
  113.         gui.launchFrame();
  114.     }
  115. }
Dec 7 '12 #3

gits
Expert Mod 5K+
P: 5,390
you want to create a webpage or a java-desktop app? in the 2nd case i will move the thread over to the java-forum for you.

regards
Dec 7 '12 #4

Rabbit
Expert Mod 10K+
P: 12,430
Also, you haven't said what the problem is. Are you getting errors? What are they? Is it not doing what you expect? What is it doing instead?
Dec 7 '12 #5

Post your reply

Sign in to post your reply or Sign up for a free account.