0

hi I have tried to implement group layout fro my assignment requirement below is my code snippet but I am having one problem within this code snippet when i do pack(); it throws me exception also I don't know how to make it visible please guide me where I am wrong code suggestion would be helpful

thanks in advance

public class AMS_GUI extends JFrame
{
   private JFrame frame;



public AMS_GUI()
{
  makeFrame(); 
}

public void makeFrame()
{
   JLabel unitLabel = new JLabel("Units"); // units label
   JComboBox unitCombo = new JComboBox(); // units empty combo box
   JButton addUnit = new JButton("Add"); // add units button for adding units

   JLabel AssessmentLabel = new JLabel("Assessments"); // assessments Label
   JComboBox AssessmentCombo = new JComboBox(); // assessments empty combo box
   JButton addAssessment = new JButton("Add"); // assessments add button

   JLabel TasksLabel = new JLabel("Tasks"); // tasks Label
   JComboBox TasksCombo = new JComboBox(); // tasks empty combo box
   JButton addTasks = new JButton("Add"); // tasks add button
   JButton editTasks = new JButton("Edit");// tasks Edit button

   JLabel planLabel = new JLabel("Plans");
   JButton makePlan = new JButton("MakePlan");
   JButton showPlan = new JButton("ShowPlan");
   JButton savePlan = new JButton("SavePlan");

    //Set up the content pane.
    GroupLayout layout = new GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setAutoCreateGaps(true);
    layout.setAutoCreateContainerGaps(true);

    layout.setHorizontalGroup(layout.createSequentialGroup()
        .addComponent(unitLabel)
        .addComponent(AssessmentLabel)
        .addComponent(TasksLabel)
        .addComponent(planLabel)
        .addGroup(layout.createParallelGroup(LEADING)
        .addComponent(unitCombo)
        .addComponent(AssessmentCombo)
        .addComponent(TasksCombo)
        .addComponent(makePlan)
        .addComponent(showPlan)
        .addComponent(savePlan))
        .addGroup(layout.createParallelGroup(LEADING)
            .addComponent(addUnit)
            .addComponent(addAssessment)
            .addComponent(addTasks)
            .addComponent(editTasks)
            )
            );



        setTitle("AMS_GUI");
        pack();

Exception

Exception in thread "main" java.lang.IllegalStateException: javax.swing.JButton
    [,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.5,
    border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@bb6ab6,flags=296,
    maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,
    margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,
    paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,
    selectedIcon=,text=Edit,defaultCapable=true] 
    is not attached to a vertical group
vyegorov
  • 21,787
  • 7
  • 59
  • 73
Hardy
  • 33
  • 1
  • 1
  • 6

1 Answers1

3
Exception in thread "main" java.lang.IllegalStateException: javax.swing.JButton
    [..] 
    is not attached to a vertical group

Add a vertical group and add the components to it.

From the JavaDocs:

GroupLayout treats each axis independently. That is, there is a group representing the horizontal axis, and a group representing the vertical axis. The horizontal group is responsible for determining the minimum, preferred and maximum size along the horizontal axis as well as setting the x and width of the components contained in it. The vertical group is responsible for determining the minimum, preferred and maximum size along the vertical axis as well as setting the y and height of the components contained in it. Each Component must exist in both a horizontal and vertical group, otherwise an IllegalStateException is thrown during layout, or when the minimum, preferred or maximum size is requested.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 3
    Would it be inappropriate to add an image of the 'O RLY?' owls to this question and answer. I do not think the developers could make the exception message more clear. A +1 that you even bothered to copy-paste the javadoc – Robin May 06 '12 at 16:55
  • @Robin *"Would it be inappropriate to add an image of the 'O RLY?' owls to this question and answer."* Huh? You lost me there. *"I do not think the developers could make the exception message more clear."* It is hard to figure how they could have been more clear, except for being *emphatic* and **bold** (which I chose to add) *"A +1 that you even bothered to copy-paste the javadoc"* (chuckle) I had to turn it into an SSCCE, run it & see the exception before I bothered checking the docs. And that was just morbid curiosity as to whether they would bother mentioning that in the class blurb ;) – Andrew Thompson May 06 '12 at 17:00
  • 1
    Andrew, for your amusement... [O RLY? owl](http://i1.kym-cdn.com/entries/icons/original/000/000/015/orly.jpg) :) @Robin You probably didn't check that it was **Andrew** who edited OP's question to add that exception! Lazy OP, he made Andrew go through all the bother and all he had to do was just copy-paste. – Marko Topolnik May 06 '12 at 20:55
  • @Marko *"Lazy OP"* I'd prefer to think the OP is 'inexperienced', and am hoping their future questions will be better worded after a careful read of the comments on this question. :) – Andrew Thompson May 06 '12 at 21:05