gtludwig
gtludwig

Reputation: 5611

how can I define label position of a jRadioButton on Netbeans?

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?

Upvotes: 3

Views: 3080

Answers (2)

PeakGen
PeakGen

Reputation: 23025

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();
            }
        });

    }
}

Upvotes: 2

trashgod
trashgod

Reputation: 205785

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

Upvotes: 5

Related Questions