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?
Asked
Active
Viewed 3,033 times
3
-
possible duplicate of [How to vertically align text and image above a JRadioButton?](http://stackoverflow.com/questions/8669735/how-to-vertically-align-text-and-image-above-a-jradiobutton) – trashgod Apr 11 '13 at 17:08
-
1@ChrisCooney, actually it is a coding issue, but thanks for your insight. – gtludwig Apr 11 '13 at 17:19
-
1@MrD, Yes. Got it done already! Thanks! – gtludwig Apr 11 '13 at 17:19
-
1@trashgod, It is indeed a duplicate. Thanks for the heads up! – gtludwig Apr 11 '13 at 17:20
2 Answers
5
Use JRadioButton#setText()
with setVerticalTextPosition(SwingConstants.BOTTOM)
.
JRadioButton jrb = new JRadioButton();
jrb.setText("Label");
jrb.setVerticalTextPosition(JRadioButton.BOTTOM);
jrb.setHorizontalTextPosition(JRadioButton.CENTER);

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