0

I'm using a JFrame to present a message box with some text and 2 buttons. How do I get the text to wrap automatically based on the size of the box?

Here's my current code:

        dialogFrame = new JFrame();

        JButton newButton = new JButton("New");
        newButton.addActionListener(new newUploadAction());

        JButton resumeButton = new JButton("Resume");
        resumeButton.addActionListener(new resumeUploadAction());

        //dialogFrame.setUndecorated(true);

        JPanel addPanel = new JPanel();

        JPanel addPanel2 = new JPanel();

        addPanel.add(newButton);
        addPanel.add(resumeButton);

        String text = "<html><p>A previous control file exists for this file. ";
        text += "Would you like to initiate a new transfer or resume the previous one?</p></html>";

        JLabel testLabel = new JLabel(text);

        //testLabel.setPreferredSize(new Dimension(1, 1)); 

        addPanel2.add(testLabel);


        Container content = dialogFrame.getContentPane();


        //content.setBackground(Color.white);
        content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS)); 

        content.add(addPanel2);
        content.add(addPanel);


        dialogFrame.setSize(200,200);
        dialogFrame.setVisible(true);
        dialogFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

I read somewhere that calling

  testLabel.setPreferredSize(new Dimension(1, 1)); 

would cause the wrapping behavior I want, but that just resulted in the text not showing up at all.

opike
  • 7,053
  • 14
  • 68
  • 95
  • possible duplicate - http://stackoverflow.com/questions/2420742/make-a-jlabel-wrap-its-text-by-setting-a-max-width – mre Jun 01 '11 at 03:25

1 Answers1

3

You could place the text in a JTextArea and call setLineWrap(true) and setWrapStyleWord(true) on the JTextArea. If you want it to look more JLabel-ish, then just change the color settings of the JTextArea to your liking.

EDIT 1

Also another thing that could work: consider having the holding JPanel use a BorderLayout:

JPanel addPanel2 = new JPanel(new BorderLayout()); //!! added BorderLayout

So that the JLabel added will fill the addPanel2.

Ryan Castillo
  • 1,135
  • 10
  • 22
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    +1 for mentioning `JTextArea` solution and cool name, but another option would be to embed `HTML`. – mre Jun 01 '11 at 03:26
  • 1
    @mre: The problem with `HTML` is that you need to specify the location of line breaks whereas *I believe* that the OP wants lines that wrap depending on the size of the GUI. – Hovercraft Full Of Eels Jun 01 '11 at 03:48
  • 3
    @HFOE: "The problem with HTML is that you need to specify the location of line breaks.." Not with a smattering of CSS. E.G. ``. The output in [this answer](http://stackoverflow.com/questions/5805328/java-html-in-swing-link-margin-not-working/5806181#5806181) is restricted to 600px width. – Andrew Thompson Jun 01 '11 at 04:54
  • you're completely right, but I suppose you could use the work around @Andrew Thompson mentioned. – mre Jun 01 '11 at 05:17