0

In my application, I need to word wrap Labels, which are contained in a JPanel (with GridBagLayout), which in turn is contained in a JScrollPane, which is contained in a resizable JFrame. The text should always be as wide as the Scrollpane allows(i.e. be completly visible at once). I tried multiple things already:

  • Putting tags around the text.
    Result: Nothing
  • Putting on the Text
    Result: Wraps, but with static width (does not respond to the Frame being resized) (duh)
  • Putting on the Text
    Result: Nothing
  • Using a JTextArea, as in https://stackoverflow.com/a/26426585/4936150
    Result: Appears to work at first, when the width of the Frame is increased, the words get "unwrapped" (so they are all on one line), but when the width is decreased afterwards, it will stay that way and not "wrap back". Also, weirdly doesn't work in the Nimbus L&F
  • Implementing Scrollable on the JPanel and returning true from getScrollableTracksViewportWidth()
    Result: Works, but I DO want it to scroll horizontally if the Frame is too small for other Components

For reference, here is the source for my Panel class, without any wrapping:

public class ProgPanel extends JPanel {

    /**
     * Create the panel.
     */
    public ProgPanel() {
        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWidths = new int[]{0};
        gridBagLayout.rowHeights = new int[]{59, 0, 0};
        gridBagLayout.columnWeights = new double[]{1.0};
        gridBagLayout.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
        setLayout(gridBagLayout);


        JLabel lblName = new JLabel("Long, long text taht needs to be wrapped, because it is displayed in a very big font on a kindof small screen, bla bla bla bhlergh lol rofl 1337");
        GridBagConstraints gbc_lblName = new GridBagConstraints();
        gbc_lblName.gridwidth = 2;
        gbc_lblName.anchor = GridBagConstraints.WEST;
        gbc_lblName.insets = new Insets(0, 0, 5, 0);
        gbc_lblName.gridwidth = 1;
        gbc_lblName.gridx = 0;
        gbc_lblName.gridy = 0;
        add(lblName, gbc_lblName);
        lblName.setBorder(new EmptyBorder(10, 5, 0, 10));
        lblName.setIconTextGap(10);
        lblName.setIcon(new ImageIcon("someicon.png"));
        lblName.setFont(getFont().deriveFont(Font.BOLD+Font.ITALIC, 48));

    }


}

EDIT: Here is the implementation using a JTextArea, which will only grow in width, but not shrink: https://pastebin.com/egMXfwK1 (Pastebin due to long length and tedious SO formatting)

EDIT2: Here are some pictures showing the issue: http://imgur.com/a/mhtV9

Community
  • 1
  • 1
Wuerfel_21
  • 61
  • 1
  • 7
  • 1
    `Using a JTextArea` - a JTextArea will work. The text will wrap and unwrap as the frame size is changed. Post your [mcve] that demonstrates the problem. – camickr Apr 11 '17 at 21:41
  • Added an example of the problem. Note that the ProgPanel should be put into a JScrollPane, but I am pretty sure that isn't where the problem lies. – Wuerfel_21 Apr 11 '17 at 21:56
  • [Seems to work fine for me](http://stackoverflow.com/questions/14737810/jlabel-show-longer-text-as-multiple-lines/14738193#14738193) – MadProgrammer Apr 11 '17 at 21:58
  • That is not an [mcve]. There is no main() method to create the class. There is no frame to add the panel to. So basically we can't test the code. `Note that the ProgPanel should be put into a JScrollPane, but I am pretty sure that isn't where the problem lies` Until the problem is solved you don't know what is or isn't the problem. Post the code that attempts to do what you want it to do. So include the scrollpane. – camickr Apr 11 '17 at 21:59
  • You're `JTextArea` example works fine for me – MadProgrammer Apr 11 '17 at 22:04
  • I have updated the example, it now includes all the boilerplate code required to run it. – Wuerfel_21 Apr 11 '17 at 22:14
  • `Pastebin due to long length` - why is it so long. You were asked for an [mcve]. So all you need to test this is a JFrame, a JTextArea added to the JScrollPane and the scroll pane added to the frame. The total code size should be 20-30 lines of code. So the first step is to prove it works in a simple situation. Then you make the situation a little more complex. If it stops working then you can tell us what you changed to cause the problem. This is how you problem solve. Small steps at a time. – camickr Apr 12 '17 at 00:55
  • The is no need for a custom text area. Again the point of the `MCVE` is to provide simple code. Maybe the custom text area is the problem? Also, did you try this with just using the default BorderLayout? Maybe the problem is the GridBagLayout. Again start simple and get more complex with the layout managers you use. We have no idea why you think you need all those custom constraints. – camickr Apr 12 '17 at 00:58

0 Answers0