JRadioButton.setHorizontalAlignment() not working?

Hi,

I'm trying to get some JRadioButtons to line up down the left side of a JFrame. The problem is they seem to always end up placed with the buttons in the middle, and the text to the right. Any ideas?

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import javax.swing.event.*;

class Exampleextends JFrame

{

JRadioButton allRadioButton;

JRadioButton uncollectionedRadioButton;

JRadioButton subsetRadioButton;

JButton okButton;

JButton cancelButton;

public Example()

{

this.addWindowListener(new WindowAdapter()

{

publicvoid windowClosing(WindowEvent e)

{

dispose();

}

});

setTitle("Collection Chooser");

Container c = this.getContentPane();

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

allRadioButton =new JRadioButton("all images");

uncollectionedRadioButton =new JRadioButton("uncollectioned images");

subsetRadioButton =new JRadioButton("subset of collections...");

ButtonGroup collectionButtonGroup =new ButtonGroup();

collectionButtonGroup.add(allRadioButton);

collectionButtonGroup.add(uncollectionedRadioButton);

collectionButtonGroup.add(subsetRadioButton);

allRadioButton.setHorizontalAlignment(JRadioButton.LEFT);

uncollectionedRadioButton.setHorizontalAlignment(JRadioButton.LEFT);

subsetRadioButton.setHorizontalAlignment(JRadioButton.LEFT);

c.add(allRadioButton);

c.add(uncollectionedRadioButton);

c.add(subsetRadioButton);

setSize(150, 120);

okButton =new JButton("ok");

cancelButton =new JButton("cancel");

JPanel buttonPanel =new JPanel();

buttonPanel.setLayout(new GridLayout(1,2));

buttonPanel.add(okButton);

buttonPanel.add(cancelButton);

c.add(buttonPanel);

JFrameUtilities.center(this);

setAlwaysOnTop(true);

setResizable(false);

setVisible(true);

}

}

[3161 byte] By [Robert_Spriggsa] at [2007-11-26 16:06:03]
# 1
Sorry, you need to comment out the line JFrameUtilities.center(), other than that, the problem persists. I can't find any help from searching this forum either. Any suggestions?
RobertSpriggsa at 2007-7-8 22:28:10 > top of Java-index,Desktop,Core GUI APIs...
# 2

Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/layout/box.html#features]How to Use Box Layout[/url] which will explain how components with different alignments are postition in the layout.

Simple answer to your question is to add:

buttonPanel.setAlignmentX(0.0f);

> Sorry, you need to comment out the line JFrameUtilities.center(),

Use the following

setLocationRelativeTo( null );

camickra at 2007-7-8 22:28:10 > top of Java-index,Desktop,Core GUI APIs...
# 3
thanks,I actually solved the problem by creating a JPanel with a null LayoutManager for my radio buttons, but I'll keep your soultion in mind next time I'm in a fix. Also thanks for the centering trick.
RobertSpriggsa at 2007-7-8 22:28:10 > top of Java-index,Desktop,Core GUI APIs...