Results 1 to 18 of 18
  1. #1
    TheGadgetCat is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default Getting strings from JTextArea into variables

    So, I'm working on a little program for myself that takes a picture of the screen at specific coordinates and then outputs the picture in a .jpg

    I have it working taking a picture of the whole screen, but I wanted it to be able to be more specific by creating JTextAreas in a GridLayout and then having the strings entered in them being converted into integers and expressed as a,b,c,d which would then feed into the capture coordinates.

    Here is what I have so far:
    Java Code:
    import java.awt.AWTException;
    import java.awt.BorderLayout;
    import java.awt.ComponentOrientation;
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.HeadlessException;
    import java.awt.Robot;
    import java.awt.Rectangle;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.io.*;
    
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class ScreenCap implements ActionListener{
    	
    	//Create frame and button and text fields
    	int a,b,c,d;
    	
    	JFrame frame; 
    	JButton button;
    	JTextField t1,t2,t3,t4;
    	
    	//Create the Grid Layout
    	
    	public final static boolean RIGHT_TO_LEFT = false;
    	
    	public static void addComponentsToPane(Container contentPane){
    		
    		if(RIGHT_TO_LEFT){
    			contentPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    		}
    		
    		contentPane.setLayout(new GridLayout(0,2));
    		
    		contentPane.add(new JTextField("W Coordinate"));
    		contentPane.add(new JTextField("X Coordinate"));
    		contentPane.add(new JTextField("Y Coordinate"));
    		contentPane.add(new JTextField("Z Coordinate"));
    	}
    	
    	//Create and define frame and button
    	public void go(){
    		frame = new JFrame();
    		button = new JButton("Capture Screen");
    		t1 = new JTextField("W Coordinate");
    		t2 = new JTextField("X Coordinate");
    		t3 = new JTextField("Y Coordinate");
    		t4 = new JTextField("Z Coordinate");
    		
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(300,200);
    		frame.setVisible(true);
    		frame.add(button);
    		
    		addComponentsToPane(frame.getContentPane());
    		
    		frame.add(BorderLayout.SOUTH,button);
    		
    		button.addActionListener(new buttonListener());
    		
    	}
    	//Run the program
    	public static void main(String[] args){
    		ScreenCap screen = new ScreenCap();
    		screen.go();
    	}
    
    	//Cap the screen when the button is pressed
    	
    	 private class buttonListener implements ActionListener{
    		public void actionPerformed(ActionEvent e){
    			
    			BufferedImage screencapture = null;
    			try {
    				screencapture = new Robot().createScreenCapture(
    						new Rectangle(a,b,c,d));
    			} catch (HeadlessException e2) {
    				// TODO Auto-generated catch block
    				e2.printStackTrace();
    			} catch (AWTException e2) {
    				// TODO Auto-generated catch block
    				e2.printStackTrace();
    			}
    			
    			File file = new File("screencapture1.jpg");
    			try {
    				ImageIO.write(screencapture, "jpg", file);
    			} catch (IOException e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			}
    			}
    	 }
    }
    I annotated it so you can see where the class is that takes the capture and where the GridLayout is created ETC.

    All help is appreciated!

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Do you have a specific question/problem? You have a good start so far.

  3. #3
    TheGadgetCat is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    Yea, the problem is that I don't know how to get the strings that the user would enter into the text boxes to be converted to integers and used in the coordinates for the screen capture.

    I guess I wasn't too clear about that an I apologize...

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Its ok. You can read the value of a textfield, which returns a String. From here, try
    Java Code:
    int n = Integer.parseInt(someString);
    You can repeat this for each textfield. Does that help?

  5. #5
    TheGadgetCat is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    Ok, that's going to be helpful, but how do I read the string, that seems to be the first logical step that I am having trouble with.

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Take a look at the API page for JTextField. You'll note it has methods for returning the contents of the field :D

  7. #7
    TheGadgetCat is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    Ok I think I found it, thanks a bunch!

  8. #8
    TheGadgetCat is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    Well maybe not.

    this is my new code:

    Java Code:
    import java.awt.AWTException;
    import java.awt.BorderLayout;
    import java.awt.ComponentOrientation;
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.HeadlessException;
    import java.awt.Robot;
    import java.awt.Rectangle;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.io.*;
    
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class ScreenCap implements ActionListener{
    	
    	//Create frame and button and text fields
    	
    	JFrame frame; 
    	JButton button;
    	JTextField t1,t2,t3,t4;
    	
    	//Create the Grid Layout
    	
    	public final static boolean RIGHT_TO_LEFT = false;
    	
    	public static void addComponentsToPane(Container contentPane){
    		
    		if(RIGHT_TO_LEFT){
    			contentPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    		}
    		
    		contentPane.setLayout(new GridLayout(0,2));
    		
    		contentPane.add(new JTextField("W Coordinate"));
    		contentPane.add(new JTextField("X Coordinate"));
    		contentPane.add(new JTextField("Y Coordinate"));
    		contentPane.add(new JTextField("Z Coordinate"));
    	}
    	
    	//Create and define frame and button
    	public void go(){
    		frame = new JFrame();
    		button = new JButton("Capture Screen");
    		t1 = new JTextField("W Coordinate");
    		t2 = new JTextField("X Coordinate");
    		t3 = new JTextField("Y Coordinate");
    		t4 = new JTextField("Z Coordinate");
    		
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(300,200);
    		frame.setVisible(true);
    		frame.add(button);
    		
    		addComponentsToPane(frame.getContentPane());
    		
    		frame.add(BorderLayout.SOUTH,button);
    		
    		button.addActionListener(new buttonListener());
    		
    		
    	}
    	//Run the program
    	public static void main(String[] args){
    		ScreenCap screen = new ScreenCap();
    		screen.go();
    	}
    
    	//Cap the screen when the button is pressed
    	
    	 private class buttonListener implements ActionListener{
    		public void actionPerformed(ActionEvent e){
    			
    			String w = t1.getText();
    			String x = t2.getText();
    			String y = t3.getText();
    			String z = t4.getText();
    			
    			int a = Integer.parseInt(w);
    			int b = Integer.parseInt(x);
    			int c = Integer.parseInt(y);
    			int d = Integer.parseInt(z);
    			
    			
    			BufferedImage screencapture = null;
    			try {
    				screencapture = new Robot().createScreenCapture(
    						new Rectangle(a,b,c,d));
    			} catch (HeadlessException e2) {
    				// TODO Auto-generated catch block
    				e2.printStackTrace();
    			} catch (AWTException e2) {
    				// TODO Auto-generated catch block
    				e2.printStackTrace();
    			}
    			
    			File file = new File("screencapture1.jpg");
    			try {
    				ImageIO.write(screencapture, "jpg", file);
    			} catch (IOException e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			}
    			}
    	 }
    }
    What is different is that after the button is pressed, I have Java get the text from all of those text fields, and then convert them into integers.

    At least that is the plan.

    This is what I get from the console on output:

    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "W Coordinate"
    at java.lang.NumberFormatException.forInputString(Unk nown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at ScreenCap$buttonListener.actionPerformed(ScreenCap .java:82)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

    From what I can see, at line 82, which is:

    int a = Integer.parseInt(w);
    int b = Integer.parseInt(x);
    int c = Integer.parseInt(y);
    int d = Integer.parseInt(z);

    It begins to stop working, I don't really know what to do. Looks to me like it isn't refreshing the text fields and it still thinks that the string is "W Coordinate" instead of the value 100 which I put in when I ran the program.

    Do I have to place this somewhere else in the code?

  9. #9
    mastal is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    You have to rewrite your addComponentsToPane method couse you're adding there new JTextField objects, not the ones you created in go() method :)

    Sorry for my english ;)

  10. #10
    TheGadgetCat is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    I don't think that it needs to be written into the addComponentsToPane method because all that does is set up the grid layout. It needs to be written into the go function or maybe the actionEvent because I need the program to get the new strings that the user has entered and then convert them to integers for use in the actionEvent when the button is pressed.

    I need Java to refresh the strings so it recognizes that the user changed the value in the JTextArea to something other than "W Coordinate."

    It is changing it to an integer, but not recognizing the new string I enter.

  11. #11
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    What is your textfields' value?

  12. #12
    TheGadgetCat is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    The text fields each have a parameter of "W Coordinate" - "Z Coordinate" which is the default string that they show when they appear on the frame.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by TheGadgetCat View Post
    The text fields each have a parameter of "W Coordinate" - "Z Coordinate" which is the default string that they show when they appear on the frame.
    Perhaps you want to put this text in a JLabel next to the JTextField and not in the JTextField itself. Otherwise if you put it in the JTextField, you'll have to take care to delete it before trying to parse the text in the field, which should seem kind of obvious.

  14. #14
    TheGadgetCat is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    That would make sense, but the problem still remains that the program isn't sending the updated string to the conversion to an integer, meaning that even if the string was blank, it would just send a blank string to the conversion to integer, meaning that the problem still wouldn't be solved. I could be wrong, and I love to be wrong, so please prove me wrong.

    Thanks for all of your help in troubleshooting this by the way.

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Heck, you're not even adding the JTextFields that you're checking to the GUI. You're adding new JTextFields that have no relationship to the t1, t2,... fields:

    Java Code:
    		//   **** you're creating completely unrelated JTextFields here!
    		contentPane.add(new JTextField("W Coordinate"));
    		contentPane.add(new JTextField("X Coordinate"));
    		contentPane.add(new JTextField("Y Coordinate"));
    		contentPane.add(new JTextField("Z Coordinate"));
    	}
    	
    	//Create and define frame and button
    	public void go(){
    		frame = new JFrame();
    		button = new JButton("Capture Screen");
    		t1 = new JTextField("W Coordinate"); //  ***** these guys are never added to the GUI!!
    		t2 = new JTextField("X Coordinate");
    		t3 = new JTextField("Y Coordinate");
    		t4 = new JTextField("Z Coordinate");

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Also, your variable names should be smarter than the ones you are using and should make so much sense that your code becomes self-documenting.

  17. #17
    TheGadgetCat is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    I realize that as this is a learning experience, I should really learn to make the code a bit more organized, and I think that will be the next step.

    But I got it to work!

    I got the JTextAreas in the right places as you mentioned and then moved the String getText and parseInt to the button's Action Event and now it is working flawlessly!

    I just need to add some labels to make it a little nicer, and get the code cleaned up and annotated.

    I appreciate all of your help, and I really learned a lot from this. Thanks a bunch!

  18. #18
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    298
    Rep Power
    4

    Default

    it is always good to learn something new, take a look at this code for some ideas
    import java.awt.AWTException;
    import java.awt.BorderLayout;
    import java.awt.ComponentOrientation;
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.HeadlessException;
    import java.awt.Robot;
    import java.awt.Rectangle;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.io.*;

    import javax.imageio.ImageIO;
    import javax.swing.*;

    public class ScreenCap { // remark: no need for ActionListener implementation here

    //Create frame and button and text fields

    JFrame frame;
    JButton button;
    JTextField t1,t2,t3,t4;

    //Create the Grid Layout

    // remark: in my case no need for this
    public final static boolean RIGHT_TO_LEFT = false;

    /* remark: i see no need for this method so i didn't use it
    *
    * the fact is that by using this method you always put these:
    * new JTextField("W Coordinate")... newly created components
    * on screen and when user changes its value it doesn't affect
    * value of t1, t2... components so they keep their original values:
    * "W Coordinate", "X Coordinate"...
    *
    * So when you ask for String values of t1, t2... latter in your actionPerrformed() method
    * you will always get "W Coordinate" and so on...
    */

    public static void addComponentsToPane(Container contentPane){

    if(RIGHT_TO_LEFT){
    contentPane.setComponentOrientation(ComponentOrien tation.RIGHT_TO_LEFT);
    }

    contentPane.setLayout(new GridLayout(0,2));

    contentPane.add(new JTextField("W Coordinate"));
    contentPane.add(new JTextField("X Coordinate"));
    contentPane.add(new JTextField("Y Coordinate"));
    contentPane.add(new JTextField("Z Coordinate"));
    }

    //Create and define frame and button
    public void go(){
    frame = new JFrame();
    button = new JButton("Capture Screen");

    // remark: why not to add actionListener here
    button.addActionListener(new buttonListener());
    t1 = new JTextField("W Coordinate");
    t2 = new JTextField("X Coordinate");
    t3 = new JTextField("Y Coordinate");
    t4 = new JTextField("Z Coordinate");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setSize(300,200);

    // remark: sets the layout of frame in one way or another, you choose...
    frame.setLayout(new GridLayout(5,1));
    //frame.setLayout(new GridLayout(1,5));

    // remark: adding components to frame
    frame.add(t1);
    frame.add(t2);
    frame.add(t3);
    frame.add(t4);
    frame.add(button);

    // remark: always set visible at the end when everything is added
    frame.setVisible(true);
    }
    //Run the program
    public static void main(String[] args){
    ScreenCap screen = new ScreenCap();
    screen.go();
    }

    //Cap the screen when the button is pressed

    private class buttonListener implements ActionListener{
    public void actionPerformed(ActionEvent e){

    /* remark: in case user entered a letter instead of number you will get
    * java.lang.NumberFormatException.
    * to avoid your app crash you can use try - catch block
    * in that case you need to declare int variables out of
    * try - catch block so they can be used latter
    */
    int a = 0;
    int b = 0;
    int c = 1;
    int d = 1;

    try{

    String w = t1.getText();
    String x = t2.getText();
    String y = t3.getText();
    String z = t4.getText();

    a = Integer.parseInt(w);
    b = Integer.parseInt(x);
    c = Integer.parseInt(y);
    d = Integer.parseInt(z);
    // and more apropriate way is to put these into one line of code:
    // a = Integer.parsInt(t1.getText());
    // b = ...
    }
    catch(java.lang.NumberFormatException nfe){
    System.out.println("you must enter a number");
    }

    BufferedImage screencapture = null;
    try {
    screencapture = new Robot().createScreenCapture(
    new Rectangle(a,b,c,d));
    } catch (HeadlessException e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
    } catch (AWTException e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
    }

    File file = new File("screencapture1.jpg");
    try {
    ImageIO.write(screencapture, "jpg", file);
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    }
    }
    }

Similar Threads

  1. Using JTextArea
    By tnixon22 in forum New To Java
    Replies: 10
    Last Post: 01-23-2011, 07:01 PM
  2. JTextArea on PopUp -JTextArea isn't editable
    By Richy76 in forum AWT / Swing
    Replies: 3
    Last Post: 02-01-2010, 07:51 PM
  3. Tab key in JTextArea
    By KristoZ in forum New To Java
    Replies: 1
    Last Post: 09-25-2009, 07:27 PM
  4. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM
  5. Replies: 1
    Last Post: 08-07-2007, 05:13 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
  •