Multiline text for swing components - tutorial

Many times its not sufficient to have only single line as text in swing components such as in JButton, JRadioButton, JCheckBox etc. Now lets see how we can add multiline text for these components. When we use the String to pass as a text to these components we can have this string in html format such as String text should be in html tag. So now we have html string so now we can have br tag to make String as multiline.

Lets see how we can achieve this
String text = " This is how we can achieve 
multiline text ";


Now lets see the following code snippet which is using above technique to achieve the multiline text.

        JFrame multiLineFrame = new JFrame("JRadioButton text example ");
        multiLineFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JRadioButton radioEx = new JRadioButton("Radio button
text example");
        multiLineFrame.getContentPane().setLayout(new GridBagLayout());
        int y = 0;

        JLabel radioLabel = new JLabel("Radio Button with multiline text");

        multiLineFrame.getContentPane().add(radioLabel, new GridBagConstraints(0,y,1,1,0,0,
                        GridBagConstraints.WEST, GridBagConstraints.WEST,new Insets(25,25,5,5),0,0));
        y++;
        multiLineFrame.getContentPane().add(radioEx, new GridBagConstraints(0,y,1,1,0,0,
                        GridBagConstraints.WEST, GridBagConstraints.WEST,new Insets(5,25,5,5),0,0));


        JLabel buttonLabel = new JLabel("JButton with multiline text");
        y++;
        multiLineFrame.getContentPane().add(buttonLabel, new GridBagConstraints(0,y,1,1,0,0,
                        GridBagConstraints.WEST, GridBagConstraints.WEST,new Insets(25,25,5,5),0,0));

        JButton multiLineButton = new JButton("JButton
multiline
text");
        y++;
        multiLineFrame.getContentPane().add(multiLineButton, new GridBagConstraints(0,y,1,1,0,0,
                        GridBagConstraints.WEST, GridBagConstraints.WEST,new Insets(5,25,5,5),0,0));
        multiLineFrame.pack();
        multiLineFrame.setVisible(true);


        JLabel checkBoxLabel = new JLabel("JButton with multiline text");
        y++;
        multiLineFrame.getContentPane().add(checkBoxLabel, new GridBagConstraints(0,y,1,1,0,0,
                        GridBagConstraints.WEST, GridBagConstraints.WEST,new Insets(25,25,5,5),0,0));

        JCheckBox multiLineJCheckBox = new JCheckBox("JCheckBox with 
 multiline text");
        y++;
        multiLineFrame.getContentPane().add(multiLineJCheckBox, new GridBagConstraints(0,y,1,1,0,0,
                        GridBagConstraints.WEST, GridBagConstraints.WEST,new Insets(5,25,5,5),0,0));
        multiLineFrame.pack();
        multiLineFrame.setVisible(true);


Now lets how the output looks like :



Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

1 comments: