0

I'm currently working on a Eclipse to build a java swing app. I'm having problems displaying my database data into an observing text field. The observing text field is used with a date picker for the user to select the date they want, which will then be saved into the database as a string, e.g. 2014/07/03. After adding this date record to the database, I want to be able to display it before the user can update changes to the date. I tried using the .setText() method but my data is not displaying.

final ObservingTextField txtDutyDate = new ObservingTextField();
    txtDutyDate.setColumns(10);
    txtDutyDate.setBounds(283, 427, 108, 22);
    add(txtDutyDate);
    txtDutyDate.setText(d.getDutyDate());

P.S ObservingTextField is a self-declared class so I can put the date in the textbox after selecting it from the calendar

public class ObservingTextField extends JTextField implements Observer {
/**
 * 
 */
private static final long serialVersionUID = 1L;

public void update(Observable o, Object arg) {
    Calendar calendar = (Calendar) arg;
    DatePicker dp = (DatePicker) o;
 //   System.out.println("picked=" + dp.formatDate(calendar));
    setText(dp.formatDate(calendar));
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user3763216
  • 489
  • 2
  • 10
  • 29
  • What's an `ObservingTextField`? Can't find any javadoc. For what I can see it's someting like a table or a [JTextArea](http://docs.oracle.com/javase/tutorial/uiswing/components/textarea.html), otherwise what does the `.setColums()` do? In any case a `setText()` on an object with an `setColums()` method sounds strange... – Narmer Jul 25 '14 at 13:05
  • My bad, didn't know that `JTextField` has a [`.setColumns()` method](http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextField.html#setColumns(int)). But in the javadoc says that it invalidates your layout, so you have to [revalidate it](http://stackoverflow.com/a/8885906/3735079). – Narmer Jul 25 '14 at 13:13
  • @Narmer Hi there, I tried to revalidate it but still the textbox turns up empty when I use the .setText() method :( I forgot to include the ObservingTextField codes above, my bad! – user3763216 Jul 25 '14 at 13:23
  • Ok, that was just a rock in the ocean. As @DavidPostill says, update your answer. – Narmer Jul 25 '14 at 13:29
  • A complete examples is seen [here](http://stackoverflow.com/a/3072979/230513). – trashgod Jul 25 '14 at 16:38

1 Answers1

0

According to the Oracle documentation, you need to register the observers with the Observable object and then call the notifyObservers method of the Observable object. Since you did not include the code in your question, I assume you did not do it.

rlinden
  • 2,053
  • 1
  • 12
  • 13