Word-wrap radiotext text in Java?

I have radio buttons whose text can be very long. Is there an easy way to wrap them in words?

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

+4
source share
3 answers

The fastest and dirtiest way is to simply add <html> at the beginning of the text of the switch label. This will start the line break, but now you need to be careful about this text if it has <characters in it. It also supports the functionality of clicking on the label text, which is a click on the radio button.

Here's a cheap and fun 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 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 ); } } 
+5
source

I do not think there are ideal solutions for this. Besides using <br> tags, you can use JTextArea and make it look like a label. Then set lineWrap and wrapStyleWord to true .

Then you lose the functionality of clicking on the shortcuts to select / deselect your radio button, so you will need to add a mouse listener.

+2
source

Wrap them with <html> tags and make sure they get enough vertical space should work. No need to break the lines yourself.

0
source

Source: https://habr.com/ru/post/1285759/


All Articles