Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 939
gtludwig Avatar asked Nov 22 '25 11:11

gtludwig


2 Answers

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

like image 191
trashgod Avatar answered Nov 25 '25 00:11

trashgod


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

    }
}
like image 24
PeakGen Avatar answered Nov 25 '25 00:11

PeakGen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!