RadioButtons right alignment ?

Hi,

Changing or adding to the code below, I would like the

RadioButtons to be in a right alignment position,

can it be done with the GridLayout ?

or else how can it be done ?

Help will be appreciated.

public class RadioPanel extends JPanel

implements ActionListener {

JRadioButton Button_a = new JRadioButton("action a", false);

JRadioButton Button_b = new JRadioButton("action b", false);

JRadioButton Button_c = new JRadioButton("action c", false);

public RadioPanel() {

Button_a.setHorizontalTextPosition(AbstractButton.LEFT);

Button_b.setHorizontalTextPosition(AbstractButton.LEFT);

Button_c.setHorizontalTextPosition(AbstractButton.LEFT);

setLayout(new GridLayout(7, 1));

add(Button_a);

add(Button_b);

add(Button_c);

}

[852 byte] By [mehappya] at [2007-10-2 10:48:24]
# 1

1) The code you posted isn't executable so I can't see exactly what your problem is.

2) Use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] when posting code.

3) Some LayoutManagers will use the setAlignmentX(...) value to position the component, but since you didn't post an executable program I can't test it out.

camickra at 2007-7-13 3:04:47 > top of Java-index,Desktop,Core GUI APIs...
# 2

First, thank you camickr !

Here is an executable code to illustrate what I mean:

import java.awt.*;

import javax.swing.*;

class Radiotest extends JFrame {

JPanel radiopanel = new JPanel();

JRadioButton Button_a = new JRadioButton("action clean house", true);

JRadioButton Button_b = new JRadioButton("action run", false);

JRadioButton Button_c = new JRadioButton("action do noth", false);

public Radiotest() {

Button_a.setHorizontalTextPosition(AbstractButton.LEFT);

Button_b.setHorizontalTextPosition(AbstractButton.LEFT);

Button_c.setHorizontalTextPosition(AbstractButton.LEFT);

radiopanel.setLayout(new GridLayout(3, 1));

radiopanel.add(Button_a);

radiopanel.add(Button_b);

radiopanel.add(Button_c);

getContentPane().add(radiopanel);

setSize(150, 100);

setVisible(true);

}

public static void main(String args[]) {

Radiotest radio = new Radiotest();

}

}

mehappya at 2007-7-13 3:04:47 > top of Java-index,Desktop,Core GUI APIs...
# 3

Sorry, here is the code with code formatting:

import java.awt.*;

import javax.swing.*;

class Radiotest extends JFrame {

JPanel radiopanel = new JPanel();

JRadioButton Button_a = new JRadioButton("action clean house", true);

JRadioButton Button_b = new JRadioButton("action run", false);

JRadioButton Button_c = new JRadioButton("action do noth", false);

public Radiotest() {

Button_a.setHorizontalTextPosition(AbstractButton.LEFT);

Button_b.setHorizontalTextPosition(AbstractButton.LEFT);

Button_c.setHorizontalTextPosition(AbstractButton.LEFT);

radiopanel.setLayout(new GridLayout(3, 1));

radiopanel.add(Button_a);

radiopanel.add(Button_b);

radiopanel.add(Button_c);

getContentPane().add(radiopanel);

setSize(150, 100);

setVisible(true);

}

public static void main(String args[]) {

Radiotest radio = new Radiotest();

}

}

mehappya at 2007-7-13 3:04:47 > top of Java-index,Desktop,Core GUI APIs...
# 4

1) See point 2 from above

2) See point 3. I gave you a suggestion. There's no reason you can't test it out yourself to see if it works.

If it doesn't work then you could try creating a panel that uses right alignmend, then and you component to the panel and then add your panel to the grid.

If that doesn't work then read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]How to Use Layout Managers[/url]. Maybe a vertical BoxLayout will do what you want.

camickra at 2007-7-13 3:04:47 > top of Java-index,Desktop,Core GUI APIs...
# 5

Hi,

Well, this and that did not work. Though I don't expect someone else to do the work for me, I hope that someone who experienced the same problem, and solved it, will help me, and thus save me work. Sure if I don't find that help, I will have to do it myself, no matter how long it will take. So far, I still hope to receive more specific help, maybe even code.... But thank you just the same, saying the above does not mean what you wrote is not helping !

mehappya at 2007-7-13 3:04:47 > top of Java-index,Desktop,Core GUI APIs...
# 6

> Though I don't expect someone else to do the work for me,

Obviously you do. I gave you a link to the tutorial which has a working example of how right alignment can be done if you choose the correct LayoutManager. The tutorial even has a picture, so its kind of hard to miss, but again this assumes you actually read the tutorial.

So spent more time reading and less time complaining that I didin't give you actual working code.

camickra at 2007-7-13 3:04:47 > top of Java-index,Desktop,Core GUI APIs...
# 7

Hi,

This seem to be the solution:

import java.awt.*;

import javax.swing.*;

class Radiotest extends JFrame {

JPanel radiopanel = new JPanel();

JRadioButton Button_a = new JRadioButton("action clean house", true);

JRadioButton Button_b = new JRadioButton("action run", false);

JRadioButton Button_c = new JRadioButton("action do noth", false);

public Radiotest() {

Button_a.setHorizontalTextPosition(AbstractButton.LEFT);

Button_b.setHorizontalTextPosition(AbstractButton.LEFT);

Button_c.setHorizontalTextPosition(AbstractButton.LEFT);

radiopanel.setLayout(new BoxLayout(radiopanel,BoxLayout.Y_AXIS));

Button_a.setAlignmentX(Component.RIGHT_ALIGNMENT);

Button_b.setAlignmentX(Component.RIGHT_ALIGNMENT);

Button_c.setAlignmentX(Component.RIGHT_ALIGNMENT);

radiopanel.add(Button_a);

radiopanel.add(Button_b);

radiopanel.add(Button_c);

getContentPane().add(radiopanel);

setSize(150, 100);

setVisible(true);

}

public static void main(String args[]) {

Radiotest radio = new Radiotest();

}

}

Thank you camickr, I did say you helped !

mehappya at 2007-7-13 3:04:47 > top of Java-index,Desktop,Core GUI APIs...