Search code examples
javaswinglayout-managergrid-layout

GridLayout ButtonPanel not Displaying Rows/Columns specified


So this code specifically says that there should be three rows and one column for JPanel selectPanel, however, when I run it I get this result and just can't figure out what I'm doing wrong. Most of this code is not relevant to the problem I think. The beginning is where I make the panel that is giving me issues (selectPanel) and the end is where that panel is added to the JFrame (roiguide).

Result of Code:

    JPanel selectPanel = new JPanel();
    selectPanel.setLayout(new GridLayout(3,1,0,0));
    selectPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

    imp.getWindow().toFront();
    IJ.setTool(Toolbar.RECTANGLE);

    referenceButton = new JButton("Add reference region");
    referenceButton.setEnabled(true);
    referenceButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            refDefine();
        }
    });
    refLabel = new JLabel(" ("+printformat.format(rno)+"/1)");
    selectPanel.add(referenceButton);
    selectPanel.add(refLabel);

    backgroundButton = new JButton("Add background region");
    backgroundButton.setEnabled(true);
    backgroundButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            brDefine();
        }
    });
    backgroundLabel = new JLabel(" ("+printformat.format(bno)+"/"+printformat.format(spotno)+")");
    selectPanel.add(backgroundButton);
    selectPanel.add(backgroundLabel);

    spotButton = new JButton("Add spot");
    spotButton.setEnabled(true);
    spotButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            spotDefine();
        }
    });
    spotLabel = new JLabel(" ("+printformat.format(sno)+"/"+printformat.format(spotno)+")");
    selectPanel.add(spotButton);
    selectPanel.add(spotLabel);

    JPanel editPanel = new JPanel();
    selectPanel.setLayout(new GridLayout(2,1,20,20));
    selectPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

    jComboBox1 = new JComboBox();
    editPanel.add(jComboBox1);

    JButton editButton = new JButton("Edit ROIs");
    editButton.setEnabled(true);
    editButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            String item = (String) jComboBox1.getSelectedItem();
            Roi roi = overlay.get(overlay.getIndex(item));
        }
        });
    editPanel.add(editButton);

    // Create the buttonPanel, which has the "Cancel" and "OK" buttons
    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(1,2,20,20));
    buttonPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
    JButton cancelButton = new JButton("Cancel");
    cancelButton.setEnabled(true);
    cancelButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            didCancel = true;
            roiguide.dispose();
            overlay.clear();
        }
    });
    buttonPanel.add(cancelButton);

    JButton okButton = new JButton("OK");
    okButton.setEnabled(true);
    okButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            if(current!=roino){
                IJ.error("ROI Manager","Please select " + roino + " regions of interest.");
            }else{
                canContinue=true;
                roiguide.dispose(); 
            }
        }
    });
    buttonPanel.add(okButton);

    // Create and populate the JFrame
    roiguide = new JFrame("Add regions of interest:");
    roiguide.getContentPane().add(selectPanel, BorderLayout.NORTH);
    roiguide.getContentPane().add(editPanel, BorderLayout.WEST);
    roiguide.getContentPane().add(buttonPanel, BorderLayout.EAST);
    roiguide.pack();
    roiguide.setLocation(400,400);
    roiguide.setVisible(true);
    roiguide.setResizable(false);

Solution

  • the addition of a new feature broke the display

    Then you know what you changed so you should know where to start looking:

    This looks suspicious:

    JPanel editPanel = new JPanel();
    selectPanel.setLayout(new GridLayout(2,1,20,20));