Greg Blickley
Greg Blickley

Reputation: 43

Issue with nesting JPanels

I am trying to create nest JPanels here is a verbal diagram from outer to inner containers.

inputScreen which has a borderlayout -> inside the CENTER of inputScreen there is another borderlayout called centerBorderLayout ---> inside the centerBorderLayout there are 2 vertical BoxLayouts named accordingly one on the WEST and one on the EAST we can call those BoxVL and BoxVR

for some reason only all the containers seem to work except for BoxVR. I mimiced the exact same process I did for BoxVL so I am unsure why Java is throwing errors at me when I try to display it.

CODE:

public void initInputScreen(){      // create text feilds for desired number of ingredients
    JPanel inputScreen = new JPanel();      //main JPanel
    JPanel centerBorderLayout = new JPanel();   // center border layout panel
    JPanel leftVertBoxLayout = new JPanel();   // center left colulmn panel
    JPanel rightVertBoxLayout = new JPanel();  // center right column panel
    
    BorderLayout border = new BorderLayout();       // creates border layout 
    BoxLayout BoxVL = new BoxLayout(leftVertBoxLayout,BoxLayout.Y_AXIS);    //creates left vertical box layout
    BoxLayout BoxVR = new BoxLayout(rightVertBoxLayout,BoxLayout.Y_AXIS);   // creates right vertical box layout
    
    inputScreen.setLayout(border);                  //set inputSceen layout
    centerBorderLayout.setLayout(border);           //set center panel layout to border
    leftVertBoxLayout.setLayout(BoxVL);             // set center left panel layout to BoxVL
    rightVertBoxLayout.setLayout(BoxVR);            // set center right panel layout to BoxVR
    
    
    
    
    JLabel ingredientLabel = new JLabel();
    ingredientLabel.setText("ingredients");
    leftVertBoxLayout.add(ingredientLabel);
    
    JLabel quantLabel = new JLabel();
    quantLabel.setText("quantity");
    rightVertBoxLayout.add(quantLabel);
    
    JLabel header = new JLabel();
    header.setText("HEADER");
    header.setFont(new Font("Serif", Font.BOLD, 20));
    inputScreen.add(header, border.NORTH);
    
    for (int x=0; x<ingredNum; x++){
        JTextField ingredTemp = new JTextField();
        JTextField quantTemp = new JTextField();
        leftVertBoxLayout.add(ingredTemp);
        rightVertBoxLayout.add(quantTemp);
    }
    inputScreen.add(centerBorderLayout, border.CENTER);             //add center panel to inputScreen
    centerBorderLayout.add(leftVertBoxLayout, border.WEST);         // add box layout panels to centerPanel
    centerBorderLayout.add(rightVertBoxLayout, border.EAST);
    this.add(inputScreen);
    inputScreen.setVisible(true);
    pack();
    revalidate();
}

NOTE: there should be another column of JTextFields to the right with a label displaying "Quantity" (all the contents of BoxVR)

enter image description here

Upvotes: 0

Views: 28

Answers (1)

Greg Blickley
Greg Blickley

Reputation: 43

I found the issue. I needed to create a separate BorderLayout for centerBorderLayout. I was referencing a layout being used in inputScreen.

Solution: create another BorderLayout and dedicate it to centerBorderLayout.

Upvotes: 1

Related Questions