0

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);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    `Most of this code is not relevant to the problem I think.` - you area correct. Your question is about layout of the components. So why would you write a complete class without doing basic testing along the way? Create a proper [mcve] to post. Chances are you will find the problem. So all you need is a JFrame with a panel using a GridLayout and 3 components to add to the panel. If that doesn't work then you have a simple/complete class to post. Get that working first, then go to the next step and add another panel. Once the panels display properly, then you start adding ActionListeners. – camickr Oct 19 '17 at 16:02
  • 1
    As a side note, the frame should be made non-resizable BEFORE you do the pack() otherwise the sizes won't be correct since the border size will change. – camickr Oct 19 '17 at 16:05
  • @camickr It was functioning as it was tested throughout and the addition of a new feature broke the display of the original features. I will debug as you say though, thank you. – Michael Wallace Oct 19 '17 at 17:56

1 Answers1

0

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));
camickr
  • 321,443
  • 19
  • 166
  • 288