1

I am in need of an easy way to implement a JScrollPane where i can add JTextAreas. This should work like a comment system as you see it on youtube and here on Stackoverflow.

it should be in java code and if theres and other easy way I would like to know about it.

List<Comment> comments = businessLogicRepair.getComments(oid, "Internal");

        for (Comment comment : comments) {
            jInternalCommentScrollPane.add(new JTextArea(comment.getText()));

        }

My comment objects contain:

public Comment(String id, String type, String text, String author, String postDate, String repairId) {
    super(id);
    this.type = type;
    this.text = text;
    this.author = author;
    this.postDate = postDate;
    this.repairId = repairId;
}

I save the comments in a database and i can get em up easily. The problem is the showing part.

Thanks for the help

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Chris Lang
  • 13
  • 1
  • 3
  • For better help sooner, post an [SSCCE](http://pscode.org/sscce.html). Obviously, to be SC it will be necessary to factor out the DB. As a side point, it pays to ask an explicit question at some point. What is your question? – Andrew Thompson Oct 19 '11 at 08:33
  • 1
    Get a `JPanel` and add all `JTextArea` to that panel and put panel into a `JScrollPane` – Adil Soomro Oct 19 '11 at 09:04
  • Thanks for the help with this Adil Soomro it worked like a charm. – Chris Lang Oct 26 '11 at 06:52

3 Answers3

5

you have to accept that is possible to put only one JComponent to the JScrollPane, in your case only one JTextArea

mKorbel
  • 109,525
  • 20
  • 134
  • 319
4

Here's a simple example that adds new text areas to a scrolling GridLayout.

enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/** @see http://stackoverflow.com/questions/7818387 */
public class ScrollGrid extends JPanel {

    private static final int N = 16;
    private JTextArea last;
    private int index;

    public ScrollGrid() {
        this.setLayout(new GridLayout(0, 1, 3, 3));
        for (int i = 0; i < N; i++) {
            this.add(create());
        }
    }

    private JTextArea create() {
        last =  new JTextArea("Stackoverflow…" + ++index);
        return last;
    }

    private void display() {
        JFrame f = new JFrame("ScrollGrid");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new JScrollPane(this));
        f.add(new JButton(new AbstractAction("Add") {

            @Override
            public void actionPerformed(ActionEvent e) {
                add(create());
                revalidate();
                scrollRectToVisible(last.getBounds());
            }
        }), BorderLayout.SOUTH);
        f.pack();
        f.setSize(200, 160);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ScrollGrid().display();
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

Maybe a JTable would be easier to use than a JTextArea.

See: How to Use Tables.

camickr
  • 321,443
  • 19
  • 166
  • 288