2011-10-19 17 views
1

我需要一個簡單的方法來實現一個JScrollPane,我可以添加JTextAreas。 這應該像一個評論系統一樣工作,就像你在YouTube上和Stackoverflow上看到的那樣。JScrollPane與多個JTextAreas

它應該是在java代碼中,如果theres和其他簡單的方法我想知道它。

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

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

     } 

我的評論的對象包括:

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; 
} 

我保存的評論在數據庫中,我可以舉起手來容易。問題是顯示部分。

感謝您的幫助

+0

爲了更快提供更好的幫助,請發佈[SSCCE](http://pscode.org/sscce.html)。顯然,要成爲SC,有必要將數據庫分解出來。作爲一個觀點,在某個時候提出明確的問題是值得的。你的問題是什麼? –

+1

獲取'JPanel'並將所有'JTextArea'添加到該面板並將面板放入一個'JScrollPane' –

+0

感謝您對Adil Soomro的幫助,它像一個魅力一樣工作。 –

回答

6

你必須接受這是可能只有一個JComponent投入到JScrollPane,在你的情況下,只有一個JTextArea

+1

'JComponent'可以是文本區域的網格,如[此處]所示(http://stackoverflow.com/questions/7818387/jscrollpane-with-multiple-jtextareas/7820703#7820703)。 – trashgod

+0

儘管沒有嵌套的滾動條! – trashgod

4

這裏有一個簡單的例子,增加了新的文本區域滾動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(); 
      } 
     }); 
    } 
}