вот моя проблема: у меня есть JTextPane, который я добавил в JScrollpane. Теперь, когда я пишу в StyledDocument этой JTextPane, а строка длиннее ширины JTextPane, она отображает остальную часть строки в новой строке вместо активации полосы прокрутки, чтобы я мог просто прокрутить до конца линия.
Вот изображение того, что я имею в виду:
На этом рисунке весь текст должен быть в одну строку, поэтому горизонтальная полоса прокрутки внизу должна быть активной.
Кто-нибудь знает, как этого добиться?
Заранее спасибо!
Редактировать1:
Вот код, который у меня есть на данный момент:
import java.awt.BorderLayout;
public class MainFrame extends JFrame
{
private ArrayList<RssFeed> rssFeeds = new ArrayList<RssFeed>();
private JPanel contentPane;
private JTextPane newsPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainFrame() {
setMinimumSize(new Dimension(450, 300));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
newsPane = new JTextPane();
newsPane.setEditable(false);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.add(newsPane);
scrollPane.setViewportView(newsPane);
contentPane.add(scrollPane, BorderLayout.CENTER);
final StyledDocument doc = newsPane.getStyledDocument();
JPanel updateButton = new JPanel();
FlowLayout fl_updateButton = (FlowLayout) updateButton.getLayout();
fl_updateButton.setAlignment(FlowLayout.RIGHT);
contentPane.add(updateButton, BorderLayout.SOUTH);
JButton addUrlButton = new JButton("URL hinzuf\u00FCgen...");
addUrlButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str = JOptionPane.showInputDialog(MainFrame.this, "Bitte eine neue URL eingeben:", "Adresse der URL");
if (str == null || str.length() == 0) {
JOptionPane.showMessageDialog(MainFrame.this, "Keine URL eingegeben.", "Fehler", JOptionPane.ERROR_MESSAGE);
} else if (!str.endsWith(".xml") && !str.endsWith(".rss")) {
System.out.println(str);
JOptionPane.showMessageDialog(MainFrame.this, "Die URL muss mit .xml oder .rss enden.", "Fehler", JOptionPane.ERROR_MESSAGE);
} else {
try {
RssFeed feed = new RssFeed(str);
rssFeeds.add(feed);
insertStringToDoc(feed.toString() + "\n", doc, doc.getLength());
} catch (XMLStreamException e1) {
JOptionPane.showMessageDialog(MainFrame.this, "Fehler beim Parsen des Rss-Feeds.", "Fehler", JOptionPane.ERROR_MESSAGE);
} catch (IOException e1) {
JOptionPane.showMessageDialog(MainFrame.this, "Fehler beim Lesen der Daten aus der URL.", "Fehler", JOptionPane.ERROR_MESSAGE);
} catch (RssException e1) {
JOptionPane.showMessageDialog(MainFrame.this, e1.getMessage(), "Fehler", JOptionPane.ERROR_MESSAGE);
}
}
}
});
updateButton.add(addUrlButton);
JButton deactivateSchedulerButton = new JButton("Scheduler deaktivieren");
updateButton.add(deactivateSchedulerButton);
JButton btnNewButton_2 = new JButton("Update");
updateButton.add(btnNewButton_2);
}
private void insertStringToDoc(String str, StyledDocument doc, int offset) {
//puts text into the given document...
}
}