(to note im new to programming so this may be a simple answer or my code is just written wrong) I have a larger program with a string that is constantly being appended and trimmed. Its written with html so most of it has line breaks where its not an issue however sometimes the lines are long enough to go out of the edge of the scroll pane. I don't want it to scroll to the side but rather wrap the text down. Code below to replicate
public class Main {
public static void main(String[] args) {
JLabel label = new JLabel("<html>really long text here.................................................................................................................................................................................................");
JScrollPane scrollPane = new JScrollPane(label, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JPanel panel = new JPanel();
JFrame frame = new JFrame();
scrollPane.setPreferredSize(new Dimension(400,400));
SpringLayout layout = new SpringLayout();
panel.add(scrollPane);
panel.setLayout(layout);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(500,500));
frame.setVisible(true);
}
}
I have tried doing a lot of thins with the maximum, minimum, and preferred size of both the scrollpane and the jlabel as well as different constraints. i also tried looking around for a similar problem but i could only find information on jtextareas in scrollpanes which i couldnt get to work either because of a lot of different factors in the main code. another thing i kept finding was mutliple jlabels in one scrollpane which mine only consists of 1 jlabel, so a lot of the fixes were not applicable or were not the correct issue.