0

I am having a problem with trying to display each separate object's properties (e.g. ID, Title, etc) in a jTextField.

I am able to get the output for the last object in the list, but I am unable to access the ones before it when I click on them in the GUI.

What do I need to add to the loop to be able to access all of the elements within the list/model, rather than just the last element? Below is my code, thankyou

for (int i = 0; i < list.size(); i++) {
    model.addElement(list.get(i));
        jTextFieldID.setText(model.get(i).getId());
        jTextFieldTitle.setText(model.get(i).getTitle());
        jTextFieldSubreddit.setText(model.get(i).getSubreddit());
        jTextAreaSelfText.setText(model.get(i).getSelftext());
        jTextFieldAuthor.setText(model.get(i).getAuthor());
        jTextFieldCreated.setText(model.get(i).getCreated());
        jTextFieldScore.setText(Integer.toString((model.get(i).getScore())));
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Julia Dune
  • 19
  • 3
  • 2
    `JTextField.setText` will not append the value but set the parameter. So you will only see the last item from your list with this. You can use something like `txtField.setSet(txtField.getText() + " " + newValue);` But I suggest you use a bunch of `StringBuilder` (one per text field) instead and then set the values in the text fields after the interation (if this is what you want, I am not sure I get your requirements). PS: You should get the instance in `list.get(i)` once in a variable instead of getting it again and again to call each getters. – AxelH May 30 '18 at 07:27
  • Maybe use a `JTable` instead – MadProgrammer May 30 '18 at 07:34
  • I'm trying to get my head around it. Essentially I have a number of custom objects and I am printing them out onto a GUI in which when I click on the 1st object I want all of that 1st objects to be displayed (e.g. ID, title) and then when I click the 2nd object I want the 2nd objects to be displayed. Thanks for your time, I appreciate it – Julia Dune May 30 '18 at 07:35
  • Then you don't need to loop on your item list. Just need to recover the correct one (based on an ID or index link to the click) and update all you textfield like you do. But with only one item, the one at `list.get(indexOfButtonClick)` – AxelH May 30 '18 at 07:38

1 Answers1

1

What you currently have is an iteration of your items.

For each items, you will set the value in each of your textfield. Of course, JTextField.setText(String)

Sets the text of this TextComponent to the specified text. If the text is null or empty, has the effect of simply deleting the old text.

So each call on the same textfield will replace the previous value.

Now what you want is to set the values of a specific item, all you have do to is to get the instance and update the textfield.

public void setTextField(RedditPost post)
    jTextFieldID.setText(post.getId());
    jTextFieldTitle.setText(post.getTitle());
    jTextFieldSubreddit.setText(post.getSubreddit());
    jTextAreaSelfText.setText(post.getSelftext());
    jTextFieldAuthor.setText(post.getAuthor());
    jTextFieldCreated.setText(post.getCreated());
    jTextFieldScore.setText(Integer.toString((post.getScore())));
}

And you will need to recover the instance based on the event you triggered to call this method.

RedditPost post = list.get(/* basd on the event*/);
setTextField(post);
AxelH
  • 14,325
  • 2
  • 25
  • 55
  • I tried doing a for loop (RedditPost post : list) and then inside that loop I wrote: jtextField.setText(thing.getId()) for example and I am still having the same problem. thankyou – Julia Dune May 30 '18 at 08:06
  • Because you didn't understood my point @JuliaDune. If you loop on every iitems and set the value each time, you erase the previous values of the textfields. Since you only want to show a specific post, you don't want to iterate every posts. So the last to line don't need to be in a loop. – AxelH May 30 '18 at 08:09
  • Can you show exactly what you are expecting with an example list with 3posts @JuliaDune. What you explain and what you are trying to do doesn't match. Edit your question with it. – AxelH May 30 '18 at 08:24
  • I essentially have a DefaultListModel of RedditPost objects which were stored in an ArrayList of type RedditPost called model. Then I have assigned a jList of RedditPosts to the model (jListRedditPost.setModel(model)) and now for the RedditPost objects which contain ID, Title etc.. I am trying to have those display in the jTextField's for each object. Sorry for the confusion, I am not sure if that helps – Julia Dune May 30 '18 at 09:48