• 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

JTextfield - update from another class

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have 2 classes.

eg.



How do I update the green JTextfield from Class utils ?

regards,
Craig.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why do you want to update the state of that text field? Why do you need to do it from the utils class (which should start U not u)? Why are you returning a panel? Why do you appear to be setting up the GUI is a method not specifically designed for setting up GUIs? I think you need to explain what you want to do, before anybody can work out how to do it.
 
Craig Johnstone
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

Why do you want to update the state of that text field? Why do you need to do it from the utils class (which should start * not *)? Why are you returning a panel? Why do you appear to be setting up the GUI is a method not specifically designed for setting up GUIs? I think you need to explain what you want to do, before anybody can work out how to do it.



Hi, sorry what I should have said that I want to update the TEXT in the JField from another class. The ServiceMonitor class is used to create a panel and fill it will values.. I just need to be able to update certain JFields that are in the panel from another java.class file.

please note the code in this post is extracts from the actual code.. I also typed some of it in so there will be punctuation issues.

Craig.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am afraid that doesn't explain anything new. I already presumed you wanted to update the text.
 
Craig Johnstone
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I am afraid that doesn't explain anything new. I already presumed you wanted to update the text.



I thought what I asked for would be straight forward.

Basically I have a JPanel that has some JTextfields in it.. this JPanel was created in a Class (e.g. Class1)
I need to be able to update the JTextfields in this JPanel from Class1 in Class2 as there is a timer1 event in Class2 that I want one of the functions to update the field in the panel.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, you need to re-design the ServiceMonitor class. The getLegend method should not be static, and it probably should not be public. The GUI items (especially "green" should be private instance variables, and then you call getLegend() from the constructor to create the GUI. You also create a public method setGreenText(). So if your Util class has a reference to a ServiceMonitor instance, it can call
 
Craig Johnstone
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Kleinschmidt wrote:First of all, you need to re-design the ServiceMonitor class. The getLegend method should not be static, and it probably should not be public. The GUI items (especially "green" should be private instance variables, and then you call getLegend() from the constructor to create the GUI. You also create a public method setGreenText(). So if your Util class has a reference to a ServiceMonitor instance, it can call



Sorry, I'm very new to java and OO so dont understand this.
 
Fred Kleinschmidt
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, it is rarely a good idea to extend JFrame; extend JPanel instead. Your driver program creates a JFrame, then add your instance to it. For example,


then your ServiceMonuitor class is something like this:
 
Craig Johnstone
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can I do this without extending JPanel?

I'd like to keep the class setup the same as I'd need to re-engineer the ServiceMonitor and Utils.class files if I change to Extending JPanel instead of JFrame
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless you need specific graphics to appear on the panel, you can probably do it without extending any Component classes at all.
 
Craig Johnstone
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Unless you need specific graphics to appear on the panel, you can probably do it without extending any Component classes at all.



so this brings me back to my issue.. how do I control the text in the Jfield within the JPanel from another class without extending it.

I tried changing the JPanel code from static JPanel getLegend() to public JPanel getLegend() but I still cannot control the JFields.
ie.

ServiceMonitor Class -



Utils Class -

 
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
Fred K gave you a perfectly good ServiceMonitor class. Let me point what it has which your attempt doesn't. First of all it keeps a reference to the JTextField so that methods can be called on it later. And second it provides a method to allow code in other classes to call the setText() method on the JTextField.

You tried to do the second part of that, but since you didn't do the first part properly you can't make it work.

I expect you could do that by extending JFrame too, although it's a bad practice and is universally recommended against (except in innumerable web tutorials who copied their code from 1999).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic