2012-05-05 12 views
5

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: Screenshot

risposta

6

ho appena usato il codice e non causa alcun problema:

import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTextPane; 
import javax.swing.SwingUtilities 

public class TestScrolling { 

    public static void main(String[] args) { 
      SwingUtilities.invokeLater(new Runnable() { 
       public void run() { 
        initUI(); 
       }); 
    } 

    public static void initUI() { 
     StringBuilder sb = new StringBuilder(); 
     for (int i = 0; i < 100; i++) { 
      sb.append("loads loads loads loads of text here "); 
     } 
     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JTextPane newsTextPane = new JTextPane(); 
     newsTextPane.setContentType("text/html"); 
     newsTextPane.setEditable(false); 
     newsTextPane.setText(sb.toString()); 

     JScrollPane scrollPane = new JScrollPane(newsTextPane); 
     scrollPane.setVerticalScrollBarPolicy(
        javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); 
     frame.add(scrollPane); 
     frame.setSize(300, 200); 
     frame.setVisible(true); 
    } 
} 

EDIT:


Devi forzare in qualche modo la larghezza del scrollPane. Nel mio esempio viene fatto implicitamente aggiungendo lo scrollpane al pannello del contenuto del frame, che per impostazione predefinita usa il BorderLayout. Nel tuo caso, hai usato un FlowLayout che alloca la dimensione preferita dello scrollpane che è circa la dimensione preferita del JTextPane.

+0

+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

+0

@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. –

+0

@trashgod, ho incluso l'intero codice, per favore consiglio! Grazie! –

0

Stai usando un pannello o qualcosa attorno al tuo JScrollPane?

Prendendo lo SSCC di @Guillaume Polet con dimensioni innapropriate l'esempio non funziona:

import java.awt.Dimension; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextPane; 

public class TestScrolling { 

    public static void main(String[] args) { 
     StringBuilder sb = new StringBuilder(); 
     for (int i = 0; i < 100; i++) { 
      sb.append("loads loads loads loads of text here "); 

     } 
     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JTextPane newsTextPane = new JTextPane(); 
     newsTextPane.setContentType("text/html"); 
     newsTextPane.setEditable(false); 
     newsTextPane.setText(sb.toString()); 
     JScrollPane scrollPane = new JScrollPane(newsTextPane); 
     scrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); 
     JPanel pan = new JPanel(); 
     pan.setMinimumSize(new Dimension(500,500)); 
     pan.add(scrollPane); 
     frame.add(pan); 
     frame.setSize(500, 500); 
     frame.setVisible(true); 
    } 
} 

io vedere la tua aggiungendo il tuo JScrollPane a panel. Puoi fornire la creazione/modifica che hai fatto su quel pannello e dove questo pannello viene usato?

+0

Ho incluso il codice completo, per favore consiglio! –