1

What im trying to do is have the user input an index of "record" they want to append. The index will be used to look at a specific object in the array list. Everything stored in that object will be written to the input from. The user will then be able to append their input and write it back to the same object. What I'm stuck on is the taking data from the object and adding it to the form controls. I was am using

Movie genreOfMovie = (Movie) movieList.get(Integer.parseInt(index) - 1);

but that gave me object instance which is not a string that I wanted.

I was told to use

 @Override
    public String toString() {

        return genreOfMovie;
    }

It worked but only for that veritable, is there a way to make it work for all the variables in the selected object?

Dzyuv001
  • 318
  • 1
  • 6
  • 18

3 Answers3

0

If you have a text field (for instance for the title of the movie) I suggest you add setTitle(String newTitle) and getTitle() to your Movie class.

With this in place, you can do

textField.setText(selectedMovie.getTitle());

and, when the user clicks save,

selectedMovie.setTitle(textField.getText());

If you have more properties (genre, director, ...) I suggest you add more get-/set-methods.


Unless the class has an very obvious String-representation, the toString should only be used in debugging purposes.

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • the toString method returns a string that "textually represents" an object. To suggest it only be used for debugging purposes is not correct. – Constantin May 06 '15 at 12:47
  • I doubt that a program that deals with movies should display a string to the user that "textually represents" the movie. I.e. `Movie[Title: Star Wars, Director: George Lucas, Internal flag: true]` does not belong in a user interface. With very few exceptions the user interface should only define well defined strings, such as `"Title: " + m.getTitle() + ", director: " + m.getDirector()`. (See the link I provided for further detais.) – aioobe May 06 '15 at 13:02
  • I should have clarified my remarks. A properly designed class should override toString. I will grant you that it should not be used liberally when better methods exist however in cases such as error messages and such, using toString are perfectly valid, even outside of debugging – Constantin May 06 '15 at 13:13
  • I would say this depends on the error message. If there's a user error, then I would still argue against using toString (see my previous comment). If it's a program error, then fine, but in the end this message is still intended aid debugging! – aioobe May 06 '15 at 13:19
  • As I said, not to be used liberally but fine for such things as logging and reporting error messages to the user. My main point is that it is good class design to override toString – Constantin May 06 '15 at 13:28
  • I agree that it's good design to override toString, but not good design to make it visible to the user in the GUI. The use cases you list as acceptable (logging and reporting error messages) do in fact serve as debugging help, so it's unclear if you actually disagree with what I'm saying. – aioobe May 08 '15 at 19:13
0

If you want to get all variables data from Movie object,

either

include a method inside Movie object which appends the variables data in required format and gives as String output.

or

Write your own method which will take Movie object as an input, and calls required fields getter methods like getTitle()+getGenreOfMovie+getLength() like that and return a String object.

And important thing is to check for Null or Empty data inside those variables.

K139
  • 3,654
  • 13
  • 17
0
import java.util.ArrayList;
import java.util.List;

public class MovieTest {

    public static void main(String[] args) {

        List<Movie> movies = new ArrayList<Movie>();

        movies.add(new Movie("A"));
        movies.add(new Movie("B"));
        movies.add(new Movie("C"));


        Movie movie = movies.get(1);
        System.out.println(movie);
        movie.setDirector("Director for B");
        movie = movies.get(1);
        System.out.println(movie);

    }
}

class Movie {   
    String title;
    String director;
    int length;

    Movie(String title) {
        this.title = title;
    }

    public String getTitle() { return title; }
    public String getDirector() { return director; }
    public int getLength() { return length; }

    public void setTitle(String title) { this.title=title; }
    public void setDirector(String director) { this.director = director; }
    public void setLength(int length) { this.length = length; }

    public String toString() {
        return "[" + title + "]["+ director +"][" + length + "]";
    }
}
Constantin
  • 1,506
  • 10
  • 16
  • As suggested by others, use the appropriate setters and getters for specific class members you need to get and set – Constantin May 06 '15 at 13:17