-1

I want to layout 6 components, vertically aligned in one column. In addition, I want a blank space of 200 pixels before the first component in the column. I have the following code:

   public class MongoMusicApplet extends JApplet{

  //*****main menu objects********
  private JPanel mainMenuPanel;
  private JButton buildingButton;
  private JTextField text;

  public void init(){

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

     mainMenuPanel=new JPanel();

     buildingButton=new JButton("Graph-Building Mode");
     text=new JTextField(20);
     JLabel label1=new JLabel("Help us improve by entering");
     JLabel label2=new JLabel("OR");
     JLabel label3=new JLabel("Enter the name of an artist");
     JLabel label4=new JLabel("to enter Discovery Mode");

     mainMenuPanel.add(Box.createRigidArea(new Dimension(0,200)));

     label1.setAlignmentX(Component.CENTER_ALIGNMENT);
     mainMenuPanel.add(label1);

     buildingButton.setAlignmentX(Component.CENTER_ALIGNMENT);
     mainMenuPanel.add(buildingButton);

     ...add all other components in this order: label 2, label3, text, label4...

     add(mainMenuPanel);
  } 
}

And I get the following layout: enter image description here

So first off, it seems that the createRigidArea is creating space before the column starts, and also in between some of the components in the column. Is there a way to make it only create space before the first component in the column?

And second, since I chose the alignment to be Y_AXIS, why are the components not arranged vertically, with one component per row? I also tried to use GridLayout(0,1), but that gave me the exact same layout. How can I force these components into one vertical column?

user50210
  • 93
  • 2
  • 10
  • 1
    1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 3) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Mar 31 '15 at 04:59
  • Use an `EmptyBorder` for the 200px gap before the first component. And see also [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Mar 31 '15 at 05:00
  • @AndrewThompson, even with any setSize methods removed, I still am not getting the components vertically stacked – user50210 Mar 31 '15 at 16:45
  • *"I still am not getting.."* I am still not seeing your MCVE.. And I asked you a question that you have ignored. – Andrew Thompson Mar 31 '15 at 17:42
  • Figured it out on my own. Thanks for the help though – user50210 Mar 31 '15 at 17:50

1 Answers1

0

After some tinkering I found out I needed to change this line:

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

to this:

mainMenuPanel.setLayout(new BoxLayout(mainMenuPanel,BoxLayout.Y_AXIS))

since I am adding the components to the mainMenuPanel, not the applet's content pane directly.

It was a dumb error, but I'm just adding this in case anyone stumbles upon this later

user50210
  • 93
  • 2
  • 10