1

I have a long string which doesn't fit to the JPanel i am putting it. text is logger than the JPanel width. I can not put "\n" to the string to break the string, in fact i don't have the control over the length and content of the string. It user inputted string. What i want to do is when i am putting the text on JPanel i want any text that doesn't fit in to the JPanel to flow in to the next Line.

Its kind of hard to explain. Please let me know if you need more details.

Thank you

printfmyname
  • 983
  • 15
  • 30

3 Answers3

2

From a quick google search I've read that, logically, all OS's have varying line break characters and as Java is platform independent you need to first find the relative separator using:

lineSeparator = (String) java.security.AccessController.doPrivileged(new sun.security.action.GetPropertyAction("line.separator"));

and then concatenate your string with lineSeparator

For example:

JLabel label = new JLabel("Hello"+lineSeparator+"world");

This method is untried and untested by me, simply the findings of my research.

As for handling overflowing text, my personal experience is to find the max length of characters before it goes out of the frame and then add in the lineSeparator

PsychoMantis
  • 993
  • 2
  • 13
  • 29
  • Problem is User is imputing the Test. when they input text and press submit. I take that Text and put it on a Panel. Now i don't know how long is the string. for example, when you in css there is comething call overflowX and overflowy (something like that) .when overflowX enable what will happen is if user entered text doesnt fit in to the width of the container it residing then it will continue printing to the next line. – printfmyname Jun 09 '12 at 19:47
  • Ok, I'm not a avid Java user so I cant be of too much more help but another quick spot of research suggests you want to count the line length and after X characters, add the `lineSeparator`. I'd go with a for loop for counting the `string.length()` but as for modifying the string to include the `lineSeparator` Im unable to help. – PsychoMantis Jun 09 '12 at 19:54
  • Example I have a Panel -> [ ] User inputted string "this string is too long for the Pane" definitly above string doesnt fit to the Panel. so I want my Panel to look like this [this string ]
    |is too long |
    [for the Pane]
    – printfmyname Jun 09 '12 at 19:55
2

Putting your text inside <html></html> tags will do the trick. Long lines will be word-wrapped automatically.

JLabel label = new JLabel("<html>"+ reallyLongString + "</html>");  
label.setPreferredSize(new Dimension(1, 1); 
kjp
  • 3,086
  • 22
  • 30
  • That's a bad idea. What if the text includes the inequation "k < 3"? – nes1983 Jun 09 '12 at 20:21
  • Thanks, this works, if you know more methods please let me know. – printfmyname Jun 09 '12 at 20:32
  • @nes1983 what do you mean by inequation "k < 3" and what will happen. – printfmyname Jun 09 '12 at 20:35
  • I think @nes1983 means that there might be '<' '>' etc characters in your text which will break the html. He is right so you might want to escape your special characters as described in http://stackoverflow.com/questions/1265282/recommended-method-for-escaping-html-in-java If the solution works, please also accept the working answer. – kjp Jun 09 '12 at 20:56
1

Try using a JTextPane instead, it will handle wordwrapping for you.

import java.awt.Dimension;

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

public class Wordwrap extends JFrame {

    public Wordwrap() {
        String s = "I have a long string which doesn't fit to the JPanel i am putting it. text is logger than the JPanel width. I can not put \n to the string to break the string, in fact i don't have the control over the length and content of the string. It user inputted string. What i want to do is when i am putting the text on JPanel i want any text that doesn't fit in to the JPanel to flow in to the next Line.";

        JTextPane textPanel = new JTextPane();
        textPanel.setText(s);
        textPanel.setPreferredSize(new Dimension(500, 100));

        JPanel p = new JPanel();
        p.add(textPanel);
        getContentPane().add(p);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setVisible(true);
        pack();
    }

    public static void main(String[] args) throws Exception {
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                new Wordwrap();
            }
        });
    }
}
Kennet
  • 5,736
  • 2
  • 25
  • 24