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.
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 <html> tag at the start of its label string.</html>") );
setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );
setSize( 200,200 );
setVisible( true );
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With