3

I'd like to define label position of jRadioButtons on a buttonGroup on Netbeans so the label would be positioned under its radioButton. Can it be done?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
gtludwig
  • 5,411
  • 10
  • 64
  • 90

2 Answers2

5

Use JRadioButton#setText() with setVerticalTextPosition(SwingConstants.BOTTOM).

JRadioButton jrb = new JRadioButton();
jrb.setText("Label");
jrb.setVerticalTextPosition(JRadioButton.BOTTOM);
jrb.setHorizontalTextPosition(JRadioButton.CENTER);

enter image description here

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2

You have to use both r.setVerticalTextPosition(JRadioButton.BOTTOM); and r.setHorizontalTextPosition(JRadioButton.CENTER); together. Otherwise, it will not work

import javax.swing.*;
import java.awt.*;

public class PersonFrame extends JFrame
{
    public PersonFrame()
    {
        JRadioButton r = new JRadioButton();
r.setText("Text");
r.setVerticalTextPosition(JRadioButton.BOTTOM);
r.setHorizontalTextPosition(JRadioButton.CENTER);

        JPanel testPanel = new JPanel();
        testPanel.setLayout(new FlowLayout());
        testPanel.add(r);

        this.add(testPanel);
        this.setSize(100,100);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public static void main(String[]args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {

            @Override
            public void run() {
                new PersonFrame();
            }
        });

    }
}
PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • +1 for [sscce](http://sscce.org/), but see also [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Apr 11 '13 at 17:21
  • @trashgod: SwingUtilities? Yes, I agree. :) I didn't thought of it, because I answered while doing my uni project. :) Thanks for the +1 :) – PeakGen Apr 11 '13 at 17:23
  • Right, `SwingUtilities.invokeLater()`. It's unlikely to manifest in this example, but it may be worth an edit if you reformat. – trashgod Apr 11 '13 at 17:27
  • 1
    @trashgod: Sure. I will do the edit now. Thanks for pointing out that issue :) – PeakGen Apr 11 '13 at 17:29