• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getString from JTextField in another class file

 
Greenhorn
Posts: 18
Eclipse IDE Opera Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello again,

I'm having a hard time accessing the value of my JTextField from another class file.

Simplified Code:
Class Layout (file.1)

Class GetVar (file.2)

Have tried lots of different codes, but it all seems so wrong to the eye.
If anyone can give me some directions, maybe a link to a tutorials or a place to read about it, it would be great.

Off-topic: am I using the extends function correctly, having a hard time grasping the extends/implements concept.

- Nilsen
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Schoolcost should not be public. If it is needed by a sub class make it protected or default access else make it private. You should also follow Java naming standards and so it should be called schoolCost.

The GetVar class extends Layout so why are you creating a new instance of Layout here.

The Layout class does not have a method declared as getString(JTextField obj). Also you have used a different capitalization of schoolcost here so it is not the same variable as Schoolcost declared in the Layout class.
 
Christoffer Nilsen
Greenhorn
Posts: 18
Eclipse IDE Opera Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:Schoolcost should not be public. If it is needed by a sub class make it protected or default access else make it private. You should also follow Java naming standards and so it should be called schoolCost.

The GetVar class extends Layout so why are you creating a new instance of Layout here.

The Layout class does not have a method declared as getString(JTextField obj). Also you have used a different capitalization of schoolcost here so it is not the same variable as Schoolcost declared in the Layout class.



Thanks for your quick answer.
I'm still inexperienced when it comes to everything, even more when it comes to public/private/default.
I usually go by the Java naming standards, all my code is like you said, dunno why I change it in the "sample code".

However I'm still not sure how to make it work, in my original code the capitalization of schoolcost is the same.
Do I have to getString in the other class since my "schoolcost" is not an method? sorry kinda lost here.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to show us the code you are running or at the very least all the relevant parts, such as the getString(..) method.

BTW if you are getting errors please post the full error message.
 
Christoffer Nilsen
Greenhorn
Posts: 18
Eclipse IDE Opera Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like I have missed my main problem all along.
Seems like my main problem is generally that I don't know how to access variables from other class files.

I will paste most of my code and hope that it helps.

Currently I'm trying to make a program which help students do some quick calculations on their economy.
I have 3 Classes. Main-Class, Layout-Class, GetVarFromField-Class.
MainClass: Have the main function with a few line of code.
LayoutClass: Lots of JLabel & JTextField for my program.
GetVarFromFieldClass: Here I'm trying to make a DocumentListener where someone insert or remove something from my JTextField it will run a calculation method, adding most of the JTextFields and print the sum in the sum JTextField.







So my question follows:
How can I access my JTextFields from other classes?

- Nilsen
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems to me that a class called GetVarFromField should know what field it's getting the value from. The easiest way to do that would be to pass it a reference to the field in its constructor.

However as a general rule, if the name of a class is a verb then chances are, there's something wrong with your design. A class should generally be modelled as a noun, since an object is supposed to represent a thing. If you have a verb in your design, it should generally be implemented as a method, since methods are Java's way of doing actions.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree. The document listener class should be called XYZListener. You would have to add it to something so it can monitor changes in a document. There should be something useful in the Java Tutorials, but I haven’t read it and am not familiar with such listeners.

Please don’t write such long lines, which make the text difficult to read; I have tried to sort it out correctly in the old posts so you can see how to do it in future, but it is awkward since you used tabs. I think the old Sun guide is mistaken in its suggestion that you don’t have to distinguish between tabs and spaces.
 
Christoffer Nilsen
Greenhorn
Posts: 18
Eclipse IDE Opera Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replies, I have been looking into programming/java for less then a week, started out with watching tons of basic tutorials,
however as soon as they started to get a little complicated I had to stop watching and learn another way.
For me, learning without knowing how I can use what I learn in a practical way is hard.

I decided to write my program, knowing it would give me tons of challenges.
But by using docs.oracle.com & youtube I managed till now.

Why does this java style guide do everything so different from docs.oracle.com & most people on youtube, including the books I have looked at?
I have been able to do exactly what I want when working with one class only, getDocument, getText(JTextField XYZ), ect.
However as soon as I went into two classes I lost the ability to access my other class variables.
I think I could have completed the program without very much of a challenge using one class only, but I want to learn, and do it correctly from the start, so I wanted to make multiple classes.

Also lot's of the stuff in the comments I just don't understand because I'm very inexperienced with programming therms.

Would think if it is normal for a program to contain hundreds if not thousands of classes this kind of problem should have an easy solution.

- Nilsen
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why does this java style guide do everything so different from docs.oracle.com & most people on youtube, including the books I have looked at?


Which Java style guide are you referring to?

However as soon as I went into two classes I lost the ability to access my other class variables.
I think I could have completed the program without very much of a challenge using one class only, but I want to learn, and do it correctly from the start, so I wanted to make multiple classes.


You have to remember that everything in a class is only known about by that class or instances of that class so if you want to refer to an object's variable or call an object's method you need to get a reference to object first. Think of it like dealing with a group of people, if you wanted John's passport number you couldn't just say out loud what's your passport number and expect John to answer. You would have to say "John what is your passport number?". In this scenario each person is effectively an instance of the Human class so you need a reference to the John instance to call John's get passport number method ie john.getPassportNumber();.

Also lot's of the stuff in the comments I just don't understand because I'm very inexperienced with programming therms.


Google for them and if you can't find a satisfactory explanation ask here and we will explain.

Would think if it is normal for a program to contain hundreds if not thousands of classes this kind of problem should have an easy solution.


Yes you are correct programs can contain many thousands of classes and there is an easy solution. When you are first starting out it all seems very difficult but it will soon all make sense and become second nature to you.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is some good stuff and some very bad stuff available on YouTube. And it takes some experience to be able to distinguish good from bad.
You are right to use multiple classes, however. In an object language like Java, it is easy to use bad design with too few classes, and difficult to create too many classes.
Which style guide is different from what? It did say on the JavaRanch guide that it was suggestions. There is really only one thing I disagree with the Sun guide about: indenting. But it was last updated in 1999.
The API documentation is not intended to constitute a tutorial.

Each class should represent something, and should be take complete responsibility for itself. Example: a Circle class (in Euclidean geometry) has three attributes (=fields), and they do not include area/circumference. I shall let you work out what they are. Anything you might need to know about a circle, eg calculating area, is done inside the circle class. That includes creating a human‑readable description (toString method). If any other classes need access to any data about the Circle, that can be provided by methods (often getXXX methods). If anything is to change in your classes, you use methods to effect those changes. Remember the less that can change, and the more you can mark final to prevent changes, the less chance there is for things to go wrong. It is usually poor design to allow another class to alter the values in your class (but some methods, eg this one are designed as exceptions to that rule).
As for methods, if you have something like a bank account class, you might use the depositMoney(...) method to change the amount. You would not use a setAmount(...) method. Think hard before giving a class a getXXX method. Think very hard before providing it with a setXXX method.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote: . . . ie john.getPassportNumber();. . . .

You may decide to use access modifiers to permit or restrict access to that method; if you miss out the access modifier then the passport number method is only accessible inside your current package, and you can read about it here, but that may not be the most important thing for you to learn just at the moment.
 
reply
    Bookmark Topic Watch Topic
  • New Topic