Ho bisogno di visualizzare i collegamenti, quindi sto usando JTextPane con setContentType. Tuttavia, il contenuto non si avvolge e non c'è scorrimento. Il contenuto di JTextPane verrà restituito da un feed RSS. Ecco il codice completo:JTextPane non visualizza JScrollPane e non Wrap Text
import java.awt.*;
import javax.swing.*;
class Main extends JFrame
{
JFrame frame;
JTabbedPane tabbedPane;
JPanel home, news;
public Main()
{
setTitle("My Title");
setSize(900, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
home();
news();
tabbedPane = new JTabbedPane();
tabbedPane.addTab(" Home", home);
tabbedPane.addTab("News", news);
JPanel framePanel = new JPanel();
framePanel.setLayout(new BorderLayout());
framePanel.add(tabbedPane, BorderLayout.CENTER);
getContentPane().add(framePanel);
}
public void home()
{
home = new JPanel();
// some stuffs here
}
public void news()
{
news = new JPanel();
JTextPane newsTextPane = new JTextPane();
newsTextPane.setContentType("text/html");
newsTextPane.setEditable(false);
JScrollPane scrollPane = new JScrollPane(newsTextPane);
scrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
news.add(scrollPane);
RSS reader = RSS .getInstance();
reader.writeNews();
String rssNews = reader.writeNews();
newsTextPane.setText(rssNews);
}
public static void main(String args[])
{
RSS reader = RSS.getInstance();
reader.writeNews();
Main mainFrame = new Main();
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Il mio risultato:
+1 per [sscce] (http://sscce.org/); Sto indovinando la domanda (non vista) [markup] (http://stackoverflow.com/q/2645834/230513) e [contenitore] (http://stackoverflow.com/q/4702891/230513) potrebbe essere anche un fattore. – trashgod
@Guillaume Polet +1 per l'esempio, l'ho usato per mostrare che con una dimensione del contenitore errata non comparirà la barra di scorrimento. L'OP sta usando un 'panel', ma non ne sappiamo abbastanza. –
@trashgod, ho incluso l'intero codice, per favore consiglio! Grazie! –