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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sreekanth3084
    New Member
    • Dec 2012
    • 2

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

    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
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    what have you done so far? please post that code - so that we have something to work with.

    Comment

    • sreekanth3084
      New Member
      • Dec 2012
      • 2

      #3
      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();
      	}
      }
      Last edited by Meetee; Dec 7 '12, 09:42 AM. Reason: Use code tags <code/> around your code

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        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

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          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?

          Comment

          Working...