Results 1 to 6 of 6
  1. #1
    mitty is offline Member
    Join Date
    Jan 2010
    Posts
    16
    Rep Power
    0

    Default JOptionPane to JFrame

    Hi guys, I have a program which uses the JOptionPane for user input. I want to put this or something similar to this on a JFrame as I need a GUI for my program. Also, I'm trying to look for a way to display the results obtained once the user enters something and clicks OK. Currently, when the program is run, a small input box appears and when the user has entered their sentence, the information is displayed in the console on eclipse.

    I really don't know how to go about this. The following is the code for my current program.

    Java Code:
    import javax.swing.*;
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.GridBagLayout;
    import java.io.*;
    import java.util.*;
    import edu.stanford.nlp.trees.*;
    import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
    
    class FileInput{
    	public static void main(String[] args)throws IOException{
    				
    	LexicalizedParser lp = new LexicalizedParser("C:/Documents and Settings/Mitesh/Desktop/eclipse/stanford-parser-2010-02-26/bin/englishPCFG.ser.gz");
        Writer output = null; 
        
        String text =  JOptionPane.showInputDialog("Please enter your sentence here");
        String text2= null;
        String Text3 = "";
        
        File file = new File("UserInput.txt");
        output = new BufferedWriter(new FileWriter(file));
        StringTokenizer st;
        
    	st = new StringTokenizer (text, " ");
    		
    		while (st.hasMoreElements()){
    	    	  text2 = ("\"" + st.nextToken() + "\"");
    	    	  System.out.println (text2);
    	    	  output.write (text2);
    	    	  Text3 = Text3 + " " + text2;
    		}
    	String[] words = Text3.split (" ");
    	
    	//for (int i=0; i < words.length; i++){
    	//  System.err.println (words[i]);
    //	}
    	       	
    		  
    		  	Tree parse = (Tree) lp.apply(Arrays.asList(words));
    		    parse.pennPrint();
    		    System.out.println();
    
    		  TreebankLanguagePack tlp = new PennTreebankLanguagePack();
    		  GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
    		  GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
    		  Collection tdl = gs.typedDependenciesCollapsed();
    		  System.out.println(tdl);
    		  System.out.println(text2);
    		  
    		    TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
    		    tp.printTree(parse);
    		    
    		    System.err.println("Penn format:");
    		    TreePrint treePrint = new TreePrint("penn");
    		    treePrint.printTree(parse);
    		    output.close ();
    	 
    		    if(file.exists()) {
    			  String absolutePathOfFirstFile = file.getAbsolutePath();
    			  System.out.println("Saved to" + absolutePathOfFirstFile);
    			} 
    		    
    		    else {
    			  System.out.println("file does not exist");
    		    }
    
    	
    	} 
    	
    }

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by mitty View Post
    Hi guys, I have a program which uses the JOptionPane for user input. I want to put this or something similar to this on a JFrame as I need a GUI for my program. Also, I'm trying to look for a way to display the results obtained once the user enters something and clicks OK. Currently, when the program is run, a small input box appears and when the user has entered their sentence, the information is displayed in the console on eclipse.

    I really don't know how to go about this. The following is the code for my current program.

    what the user enter is stored in the String text, so to display what the user entered is done by System.out.println(text). here is little example i create

    Java Code:
    package myjava.examples.org;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    
    public class TestClass extends JFrame {
    	
    	public static void main(String[] args) {
    
    		TestClass c = new TestClass();
    		
    		String text =  JOptionPane.showInputDialog("Please enter your sentence here");
    		// print in the console
    		System.out.println(text);
    		// print in jframe
    		c.getContentPane().add(new JLabel(text));
    		c.setSize(400, 400);
    		c.setVisible(true);
    		}
    }

  3. #3
    mitty is offline Member
    Join Date
    Jan 2010
    Posts
    16
    Rep Power
    0

    Default

    Thanks j2me that really helped. I've come across one more problem though. The sentence entered is stored in an array and then it is parsed which adds tags to each word and then outputs it. for example,

    "hello world" returns

    "hello",
    "world",
    [nn("world"-3, -1), nn("world"-3, "hello"-2)]

    etc.

    I want these to be displayed in a textarea or something similar for the user to see, also is it possible to have the GUI load up when the program is run and all of the input/ouput is done on there instead of having different frames appear and disappear?

    sorry for the silly questions, still getting my head around Java.

    thanks in advance.

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by mitty View Post
    Thanks j2me that really helped. I've come across one more problem though. The sentence entered is stored in an array and then it is parsed which adds tags to each word and then outputs it. for example,

    "hello world" returns

    "hello",
    "world",
    [nn("world"-3, -1), nn("world"-3, "hello"-2)]

    etc.

    I want these to be displayed in a textarea or something similar for the user to see, also is it possible to have the GUI load up when the program is run and all of the input/ouput is done on there instead of having different frames appear and disappear?

    sorry for the silly questions, still getting my head around Java.

    thanks in advance.

    are you looking for a jframe with a non-editable textarea and a textfield where the user can enter a sentence and when the user pushes send his entering is added to the textarea?

  5. #5
    mitty is offline Member
    Join Date
    Jan 2010
    Posts
    16
    Rep Power
    0

    Default

    Pretty much yes, although the outcome of the input to be in one box (which can be the label as you showed me previously) and then another non-editable textarea to display the output

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by mitty View Post
    Pretty much yes, although the outcome of the input to be in one box (which can be the label as you showed me previously) and then another non-editable textarea to display the output

    here is a piece of code that you can use for the tokenizer with the basic functionality: you can enter a sentence in the textfield and when you press enter key the sentence is added to the textArea 1:1. what is missing is the tokenizer but for that you can add your code after my comment // manipulate your text here before ... if you have question feel free to ask.

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.TextArea;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class TestClass extends JFrame implements KeyListener {
    
    	private static final long serialVersionUID = 1L;
    	private JTextField textField;
    	private TextArea textArea;
    	private JPanel south;
    	private JPanel southEast;
    	private JButton exit;
    
    	public TestClass(String title) {
    		// layout the GUI
    		super(title);
    		textField = new JTextField();
    		textArea = new TextArea("", 0, 0, TextArea.SCROLLBARS_VERTICAL_ONLY);
    		textArea.setEditable(false);
    
    		this.add(textArea, BorderLayout.CENTER);
    		south = new JPanel(new BorderLayout());
    		south.add(textField, BorderLayout.CENTER);
    		this.add(south, BorderLayout.SOUTH);
    
    		southEast = new JPanel(new BorderLayout());
    		exit = new JButton("Exit");
    		southEast.add(exit, BorderLayout.EAST);
    		exit.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent evt) {
    				exitAction();
    			}
    		});
    		south.add(southEast, BorderLayout.EAST);
    
    		textField.addKeyListener((KeyListener) this);
    
    	}
    
    	public JTextField getTextField() {
    		return textField;
    	}
    
    	public void keyTyped(KeyEvent e) {
    	}
    
    	public void keyReleased(KeyEvent e) {
    
    	}
    
    	public void keyPressed(KeyEvent e) {
    		if ((e.getKeyCode() == e.VK_ENTER)) {
    			// manipulate your text here before appending it to the textArea
    			textArea.append(textField.getText() + "\n");
    			textField.setText("");
    		}
    	}
    
    	public void exitAction() {
    		System.exit(0);
    	}
    
    	public static void main(String[] args) {
    
    		TestClass f = new TestClass("Tokenizer");
    		f.setSize(400, 200);
    		f.setVisible(true);
    		f.getTextField().requestFocus();
    	}
    }

    gook luck.

Similar Threads

  1. Replies: 6
    Last Post: 04-12-2010, 12:33 PM
  2. Help with JOptionPane
    By sanctishit in forum New To Java
    Replies: 1
    Last Post: 03-10-2010, 11:20 AM
  3. need help with a JOptionPane
    By dr4g0nk1ng in forum Advanced Java
    Replies: 2
    Last Post: 02-19-2010, 10:40 PM
  4. Passing data from one JFrame to another JFrame
    By tarami in forum New To Java
    Replies: 3
    Last Post: 08-06-2009, 06:44 PM
  5. Replies: 4
    Last Post: 04-23-2009, 12:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •