Results 1 to 9 of 9

Thread: Stuck already!

  1. #1
    zb82 is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Default Stuck already!

    Hi,

    I'm completely new to Java, I did quite some Matlab programming but nothing really advanced. Since I enjoy programming, I wanted to learn Java. As a first project, I wanted to make a small program that does some simple calculationsusing windowbuilder. However, I'm stuck already!! I'm trying to use the input from one text field, then add the input from a second text field and display it in a text pane. But it doesn't work at all!

    I can get the console to print the input in the first text field, however, when I try to use the variable I create in first text field it's not recognized in the second text field. I'm sure my problem is a lack of understanding of the basic concepts of programming in Java, I've read some books and online tutorials but I don't manage to figure out what I'm doing wrong. Can anyone help me?

    Here's part of the code I have now:

    /**
    * Create the frame.
    */
    public example() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    textField = new JTextField();
    textField.addCaretListener(new CaretListener() {
    public void caretUpdate(CaretEvent arg0) {
    String input1 = textField.getText(); // this is working fine
    System.out.println((input1));
    }
    });
    textField.setBounds(79, 53, 134, 28);
    contentPane.add(textField);
    textField.setColumns(10);

    textField_1 = new JTextField();
    textField_1.addCaretListener(new CaretListener() {
    public void caretUpdate(CaretEvent e) {
    String input2 = textField.getText();
    System.out.println((input1 + input2)); // here the variable "input2" is not recognized anymore...
    }
    });
    textField_1.setBounds(79, 106, 134, 28);
    contentPane.add(textField_1);
    textField_1.setColumns(10);
    }
    I'm sorry, I'm not sure how to paste this into the forum with the Eclipse layout, that would have been clearer I guess...

    Thank you very much!!

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Stuck already!

    make sure variables are within the scope you are referring to. the scope is the content surrounded by {}

    Also just so you know:

    System.out.println((input1)); can be written as System.out.println(input1); you dont need to wrap input1 in parenthesis.
    Last edited by monkeyjr97; 05-09-2013 at 01:08 AM.

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,334
    Rep Power
    5

    Default Re: Stuck already!

    Since you're new to Java I would recommend *not* starting out writing any form of GUI or graphics related programming. There are so many elements to Java, even without the OOP stuff likes classes and interfaces. Why not just go thru the tutorials at the link in my signature. The stuff you learn will ultimately make your experience in GUI creation more efficient and less frustrating. And have a link to the JDK API handy. Here is the one I use for Java 1.7. Java Platform SE 7
    And as usual, the folks here will certainly help.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    zb82 is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Default Re: Stuck already!

    Quote Originally Posted by monkeyjr97 View Post
    you have these in your code "});" since the code isnt in code tags its hard to read but to me these "});" are causing a problem, they shorten the variable scope, and to my knowledge you never use ); after a {} block

    Also just so you know:

    System.out.println((input1)); can be written as System.out.println(input1); you dont need to wrap input1 in parenthesis.
    Thank you for your reply! These "});" I did not put in, they were put there by windowbuilder I guess. I see what you mean with the double parentheses, I'll take it out!

    I figured out how to paste with code tags (one break through at a time...). Here's the code again:

    Java Code:
    /**
    	 * Create the frame.
    	 */
    	public example() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
    		
    		textField = new JTextField();
    		textField.addCaretListener(new CaretListener() {
    			public void caretUpdate(CaretEvent arg0) {
    				String input1 = textField.getText(); // this is working fine
    				System.out.println((input1));
    			}
    		});
    		textField.setBounds(79, 53, 134, 28);
    		contentPane.add(textField);
    		textField.setColumns(10);
    		
    		textField_1 = new JTextField();
    		textField_1.addCaretListener(new CaretListener() {
    			public void caretUpdate(CaretEvent e) {
    				String input2 = textField.getText(); 
    				System.out.println((input1 + input2)); // here the variable "input2" is not recognized
    			}
    		});
    		textField_1.setBounds(79, 106, 134, 28);
    		contentPane.add(textField_1);
    		textField_1.setColumns(10);
    	}

  5. #5
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Stuck already!

    if you are new to java dont use a windowbuilder, build the GUI by hand, windowbuilder's add a lot of things you wont really understand. Also it helps you learn a lot, swing is a bit confusing at first but once you get the hang of it, its quite enjoyable to use :D

    String input2 = textField.getText(); should be:

    String input2 = textField_1.getText();

  6. #6
    zb82 is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Default Re: Stuck already!

    Since you're new to Java I would recommend *not* starting out writing any form of GUI or graphics related programming. There are so many elements to Java, even without the OOP stuff likes classes and interfaces. Why not just go thru the tutorials at the link in my signature. The stuff you learn will ultimately make your experience in GUI creation more efficient and less frustrating. And have a link to the JDK API handy. Here is the one I use for Java 1.7. Java Platform SE 7
    And as usual, the folks here will certainly help.
    Thanks for that advice, actually with Matlab I also never really managed to get going with GUIs (although I never tried to hard). I'll go through the tutorial you showed, maybe it was impatient to start with the GUIs immediately.... I would however still like to know what's going wrong!

  7. #7
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Stuck already!

    did you try this?
    change : String input2 = textField.getText();

    to: String input2 = textField_1.getText();

  8. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,334
    Rep Power
    5

    Default Re: Stuck already!

    Line 27, you said that input2 is not recognized. Are you certain you didn't mean input1? Input2 is declared right before so it is within scope. However, input1 is declared in the other caretListener and is only local to that caretUpdate method. You need to make input1 an instance variable to be accessible by both methods. And don't redeclare it within the methods.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    zb82 is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Default Re: Stuck already!

    did you try this?
    change : String input2 = textField.getText();

    to: String input2 = textField_1.getText();
    Sorry for not responding to this, somehow I missed your post... It is a mistake indeed and I corrected it, however I don't think it solved the problem. I'll try building a GUI by hand, that will also make me understand what is what much better.

    Line 27, you said that input2 is not recognized. Are you certain you didn't mean input1? Input2 is declared right before so it is within scope.
    You're right too, if I ever want to be able to write some programs I will have to start being a bit more precise!

    I'm reading the Java Tutorial from the link in your signature and it's very helpful so far. I think it's better if I get a better understanding of Java before I continue. Hopefully soon enough I'll find out what an instance variable is!

Similar Threads

  1. Re: I'm new and stuck, please help :(
    By leader111 in forum New To Java
    Replies: 2
    Last Post: 09-25-2012, 10:38 PM
  2. I'm new and stuck, please help :(
    By XoVoX in forum Advanced Java
    Replies: 6
    Last Post: 07-15-2012, 07:48 PM
  3. Im on my last lab!!!! And im stuck...:(
    By clanboru15 in forum New To Java
    Replies: 5
    Last Post: 03-13-2009, 01:44 AM
  4. really stuck now..
    By shongo in forum Advanced Java
    Replies: 2
    Last Post: 11-09-2008, 02:56 AM
  5. Stuck in need of help!
    By Zombie_Leg! in forum New To Java
    Replies: 1
    Last Post: 09-23-2008, 02:22 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
  •