-1

Lets say I have a string and JLabel, the JLabel displayed on a JFrame:

public String content="Hello fellow stack-overflowers, I want to format this output";
public JLabel jL = new JLabel();

Once do the following:

jL.setText(content);

I get this output:

Hello fellow stack-overflowers, I want to for..

But what I really want is the text not to keep going right past the length of the label or textfield, but to make a newline every like 4 words or so, something like:

Hello fellow stack-overflowers, I want
to format this output

Ask if more info is needed.

user2855405
  • 495
  • 2
  • 7
  • 20
  • 1
    You can use `` tags in a JLabel. See ['Is there a "word wrap" property for JLabel?'](http://stackoverflow.com/questions/7861724/is-there-a-word-wrap-property-for-jlabel) For a JTextField, I would say use JTextArea instead. – Radiodef Nov 05 '14 at 00:27

1 Answers1

3

A possible solution is to wrap the label text in HTML tags and allow the underlying layout manager the ability to resize it, for example...

Label Test

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test100 {

    public static void main(String[] args) {
        new Test100();
    }

    public Test100() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel label;
        private JTextField textField;

        public TestPane() {
            label = new JLabel("<html>Hello fellow stack-overflowers, I want to format this output</html>");
            textField = new JTextField(10);

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.NORTHWEST;

            add(label);

            gbc.gridx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1;
            add(textField, gbc);
        }

    }

}

This example goes out of it's way to pressure the label to split, you might need to play around with the values a little...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I think you want to set the label's maximum size as well, otherwise, the layout manager will probably give it as much room as it wants. – MarsAtomic Nov 05 '14 at 00:43
  • @MarsAtomic Have a read of [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) for why I didn't ;) – MadProgrammer Nov 05 '14 at 00:45
  • @MarsAtomic You are right, but I would change the `TestPane`'s `preferredSize` method instead ;) – MadProgrammer Nov 05 '14 at 00:46
  • Yeah, I know. But try without setting max size and see what happens. Edit: that might work. I'll try it myself and see how it works. – MarsAtomic Nov 05 '14 at 00:46
  • @MarsAtomic Yeah, I know, it starts out really wide ;) – MadProgrammer Nov 05 '14 at 00:51