Search code examples
javaswinglayoutjlabeljradiobutton

Huge gap between JLabel and JRadiobutton?


I have a problem. I have huge gap between the Label and radiobuttons but I dont want to have the gaps. I think it is because of my Layout but I am not sure.

Importent: Below this is a Picture. Picture has more to say then words^^

Huge Gap and the Radiobutton is in the center bottom

"Action choose" is in the correct position. Now I just want to show the Radiobutton underneath of "Action choose". In the position of the 'c' of "Action choose". But this is not important.

This is my Code how I create this type of styling. :)

   JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));

    panel.add(new Label("Action choose:"));
JRadioButton action1 = new JRadioButton("Anime",false);
JRadioButton action2 = new JRadioButton("AMV",false);
JRadioButton action3 = new JRadioButton("OVA",false);
JRadioButton action4 = new JRadioButton("Manga",false);
ButtonGroup group = new ButtonGroup();
    group.add(action1);
    group.add(action2);
    group.add(action3);
    group.add(action4);

    panel.add(action1);
    panel.add(action2);
    panel.add(action3);
    panel.add(action4);

Best Regards

Kuku


Solution

  • panel.add(new Label("Action choose:"));
    

    Don't use a Label. That is an AWT component.

    Use a JLabel for Swing a application.

    A BoxLayout will respect the maximum size of a component. For a JLabel the maximum size is the same as the preferred size. For a Label, there is no maximum size so the label will take as much space as possible.