ето моят проблем: имам 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...
}
}