Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Word-wrap radio button text in Java?

I have some radio buttons whose text may be very long. Is there an easy way to word-wrap them?

Yes, wrapping them in <html> tags and inserting <br> tags would work, but is there a more automatic way to accomplish this? I don't really want to roll my own typesetter.

like image 616
Amanda S Avatar asked Jan 23 '23 13:01

Amanda S


1 Answers

The quickest and dirtiest way is simply to prepend <html> at the start of the radio button's label text. This will make line wrapping start to occur but you'll now need to be careful about that text if it has < characters in it. This also maintains the functionality of click on the label text being a click on the radio button.

Here's a cheap and cheerful example:

public class Test extends JFrame {
    public static void main(String[] args) {
        new Test();
    }

    private Test() {
        Container  c = getContentPane();
        c.setLayout( new BorderLayout() );
        c.add( new JRadioButton( "<html>I've got a very long text description that's going to wrap over very long lines because I stuck an &lt;html&gt; tag at the start of its label string.</html>") );
        setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );
        setSize( 200,200 );
        setVisible( true );
    }
}
like image 92
banjollity Avatar answered Jan 26 '23 04:01

banjollity