0

I should create a list of users as result of a query in twitter like in Google. I'd like to represent the results in the following way:

  • image of the user - user name - user sex - user location
  • a little snippet of the tweets that match the query (if necessary)

Since now, I have been using a JTextArea, to which I have been appending the name, the sex, the location of the user and some tweets that match the query (if necessary), but now I'd like to show also the profile image and I think It would be necessary to put all the information about each user in a JPanel, but I need to attach dynamically new JPanels to the same JScrollPane and I don't know how to do this.

Abdelsalam Shahlol
  • 1,621
  • 1
  • 20
  • 31
StackUser
  • 587
  • 6
  • 26
  • 1
    I don't think that you have to manually add `JPanel` to your guy, probably a better idea would be to work with a `JList` by building a custom `ListCellRenderer` which describes how elements in the list need to be displayed. – StepTNT Feb 07 '16 at 16:17
  • yes, but if a create a list of jpanels, then panels are not showed... – StackUser Feb 07 '16 at 16:25
  • @StepTNT, i have been able to use JList just for String – StackUser Feb 07 '16 at 16:27
  • A `JList`can be used for any type of object. The important part lies in the `ListCellRenderer`. See [How to Use Lists](https://docs.oracle.com/javase/tutorial/uiswing/components/list.html) for details. The rendering is covered in the [Writing a Custom Cell Renderer](https://docs.oracle.com/javase/tutorial/uiswing/components/list.html#renderer) section of the tutorial. – Andrew Thompson Feb 07 '16 at 17:45
  • thank you @AndrewThompson, now I'm trying Squiggli Muffin Man's method, which seems more simple. If I fail, I'll try to implement ListCellRenderer in order to display JPanel instead of its own toString method – StackUser Feb 08 '16 at 11:34
  • Could @AndrewThompson or anyone else reply to this question, please? http://stackoverflow.com/questions/35244234/why-java-util-concurrent-rejectedexecutionexception-using-twitter4j-to-sample-tw – StackUser Feb 08 '16 at 12:59
  • I've never used the Twitter API. – Andrew Thompson Feb 08 '16 at 13:37
  • Could anyone else help me? @StepTNT? – StackUser Feb 08 '16 at 15:25

1 Answers1

1

Use a JEditorPane with HTML content. For example, the code below will add in your image:

JEditorPane pane = new JEditorPane();
pane.setContentType("text/html");
String urlForImage = "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png";
pane.setText("<html><img src=\"" + urlForImage + "\" /></html>");
myJframe.add(pane);
  • Your solution seems to work also with CSS styles! Maybe I'll vote it as best answer soon – StackUser Feb 08 '16 at 11:42
  • *"Does it support styles?"* Some styles, but not a lot. BTW - it will be more tricky to get the editor pane to support an 'in memory' image. ;) – Andrew Thompson Feb 08 '16 at 11:43
  • Thanks! :D If you're having trouble with supporting an in-memory image, try saving your image to a file, using javax.swing.imageio.ImageIO.write(RenderedImage img, String format, File output), and then loading it with –  Feb 08 '16 at 13:27
  • @SquigglyMuffinMan, could you help me also with this question? http://stackoverflow.com/questions/35244234/why-java-util-concurrent-rejectedexecutionexception-using-twitter4j-to-sample-tw – StackUser Feb 08 '16 at 15:25
  • I did my best to try and answer... :D –  Feb 09 '16 at 01:33