0

(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.

tau
  • 1
  • 1
  • 1
    You may need a `JLabel` which implements the `Scrollable` interface – MadProgrammer Mar 02 '23 at 05:45
  • 1
    *with a string that is constantly being appended and trimmed* - sounds like a JTextArea or JTextPane should be used as both allow for easy changing of the text. If you couldn't get it to work I would guess the problem is with your code, not the component, but this is the problem you should be attempting to solve. – camickr Mar 02 '23 at 16:05

1 Answers1

0

You can try following snippet and adapt it accordingly

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container cp = frame.getContentPane();
        JEditorPane editorPane = new JEditorPane();
        editorPane.setText(Constant.text); // Add your long text here
        editorPane.setEditable(false);
        JScrollPane editorScrollPane = new JScrollPane(editorPane);
        editorScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        cp.add(editorScrollPane, BorderLayout.CENTER);
        frame.setSize(new Dimension(500, 500));
        frame.setVisible(true);
    }

A similar issue was discussed here, Multiline text in JLabel

matesio
  • 1,584
  • 17
  • 31
ARNR
  • 43
  • 4
  • apparently changing it to a editor pane and setting its content type to text/html got the job done, i just had to change a few things in the main code but it seemed to work. only issue now is that i cant use setVerticalAlignment(SwingConstants.BOTTOM) to have my text bottom aligned but its a minor detail. regardless thank you! – tau Mar 03 '23 at 09:27