0

This has been confusing and irritating me for a while. I am working on a small program for a friend, and basically, it is a to-do list. Users can create a task with the AddAssignment class. There are a couple of fields the user must complete, but I am working on the first one, the task name. Once the task is saved, it is added to the list, which is in the ViewAssignments class. The JTextField for the task's name is known as textField.

I want the text of lblAssignmentNa, the JLabel in the ViewAssignments to be set to the text of textField on the press of btnCreateAssignment, a button in the AddAssignment class.

I'd appreciate only the code to change the JLabel's text to the same as textField's text as an answer. Thanks. And I'm sorry if you don't understand any of this. Just let me know if you need more information.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • read http://stackoverflow.com/help this to know how to write questions, and please be shorter and show some code.. – nachokk Jun 16 '13 at 05:34

3 Answers3

1

Before answering you I'd like you to check this list :

Writing perfect questions

You could just say : How to set a lable's text to textField's text when button pressed instead of this paragrap. However this will help you :

jLabel.setText(textField.getText());
Azad
  • 5,047
  • 20
  • 38
  • I realize my question was rather long. Secondly, I the JLabel I want the text to be changed of is in another class... – FreshCoffee Jun 16 '13 at 05:52
1

Create some kind of setter method that can pass the request to the ViewAssignments, like setAssignmentName and when the user clicks the btnCreateAssignment, simply call the method, passing the String value from the textField.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

First, try to not call Class names as its behaviour AddAssigment is a bad name for a class. Should be AddAssignmentPanel for example.

Second you should read this tutorial and then use it Property Change Listeners, (basically an implementation of Observer Pattern).

  • Observer in your case is the class having lblAssignmentNa property (use PropertyChangeListener)
  • Observable in your case is the class having textfield property (Use PropertyChangeSupport)

So you have to do something like this.

  1. Register the observer class into the observable class
  2. When event happens, in your case, btnCreateAssignment pressed you should notify your observers.

So in ViewAssigments should be defined like this.

public class ViewAssigments extends Something implements PropertyChangeListener

In AddAssigment register ViewAssigment as an observer
In btnCreateAssigment event you have to call propertyChangeSupport.firePropertyChange(..) and it's send to the observers to notify that state changes.

Additionally if your classes extends JComponent you have a PropertyChangeSupport instance ready for register Listeners and to firePropertyChange(..)

nachokk
  • 14,363
  • 4
  • 24
  • 53
  • +1 for loose coupling; an example is seen [here](http://stackoverflow.com/a/11832979/230513). – trashgod Jun 16 '13 at 13:50
  • Forgive, I've been rereading this answer for a while and it reads backward to me. I don't disagree with the basic intent though. AddAssignment has a button, btnCreateAssignment, which when triggered, wants to set the value of a label on ViewAssignment...this means that AddAssignment doesn't care about any property changes of ViewAssignment. The use of a PropertyChangeListener in this instance seems wrong to me, as the button isn't triggering change in any of the properties in the AddAssignment class. – MadProgrammer Jun 16 '13 at 21:14
  • It would have being simpler to just say "add a action listener from the ViewAssigment to the AddAssignment classes", but why should the ViewAssigment class even care about the AddAssignment class at all? Wouldn't it be better for the ViewAssignment to expose management functionality, perhaps via an interface, thus removing any ambiguity over how the values are changed – MadProgrammer Jun 16 '13 at 21:18
  • i think that ViewAssignment(observer) wants to observes a property of AddAssigment(observable) when an event happen. If you want to use it combine with a Mediator is a good solution :). @MadProgrammer – nachokk Jun 16 '13 at 21:26
  • It is question of design and personal choice I guess. Personally I don't see the button click changing a property of the AddAssignment class – MadProgrammer Jun 16 '13 at 21:46