JScrollPane with multiple JTextAreas

I need an easy way to implement JScrollPane where I can add JTextAreas. This should work as a commentary system, as you can see on youtube and here on Stackoverflow.

it should be in java code, and if theres another 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 comments in a database and I can easily get them. The problem is the exponential part.

thanks for the help

+1
source share
3 answers

you must agree that you can only put JComponent in JScrollPane , in your case only one JTextArea

+6
source

Here is a simple example that adds new text areas to the GridLayout scroll.

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(); } }); } } 
+4
source

Maybe JTable will be easier to use than JTextArea.

See: How to use tables .

+1
source

Source: https://habr.com/ru/post/1394050/


All Articles