Results 1 to 18 of 18
- 01-31-2011, 07:22 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
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:
I annotated it so you can see where the class is that takes the capture and where the GridLayout is created ETC.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(); } } } }
All help is appreciated!
- 01-31-2011, 09:46 PM #2
Do you have a specific question/problem? You have a good start so far.
- 01-31-2011, 09:48 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
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...
- 01-31-2011, 10:04 PM #4
Its ok. You can read the value of a textfield, which returns a String. From here, try
You can repeat this for each textfield. Does that help?Java Code:int n = Integer.parseInt(someString);
- 01-31-2011, 10:06 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
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.
- 01-31-2011, 10:12 PM #6
Take a look at the API page for JTextField. You'll note it has methods for returning the contents of the field :D
- 01-31-2011, 10:51 PM #7
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
Ok I think I found it, thanks a bunch!
- 01-31-2011, 11:07 PM #8
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
Well maybe not.
this is my new code:
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.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(); } } } }
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?
- 02-01-2011, 12:01 AM #9
Member
- Join Date
- Jan 2011
- Posts
- 6
- Rep Power
- 0
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 ;)
- 02-01-2011, 12:20 AM #10
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
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.
- 02-01-2011, 01:00 AM #11
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
What is your textfields' value?
- 02-01-2011, 01:39 AM #12
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
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.
-
- 02-01-2011, 01:48 AM #14
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
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.
-
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");
-
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.
- 02-01-2011, 02:06 AM #17
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
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!
- 02-01-2011, 03:18 AM #18
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
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
-
Using JTextArea
By tnixon22 in forum New To JavaReplies: 10Last Post: 01-23-2011, 07:01 PM -
JTextArea on PopUp -JTextArea isn't editable
By Richy76 in forum AWT / SwingReplies: 3Last Post: 02-01-2010, 07:51 PM -
Tab key in JTextArea
By KristoZ in forum New To JavaReplies: 1Last Post: 09-25-2009, 07:27 PM -
What are Instance variables and static variables?
By sandeshforu in forum New To JavaReplies: 3Last Post: 09-09-2009, 05:48 PM -
JTextArea that I would like to have updated as strings are appended
By paul in forum AWT / SwingReplies: 1Last Post: 08-07-2007, 05:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks