Search code examples
javaswingjpanelboxlayout

BoxLayout can't be shared. Trying to add multiple labels from an array into a BoxLayout Panel


I know there are plenty of BoxLayout questions on here, however I can't find one that fixes my problem. I need my scoreDescPanel to show each label directly below each other (like a list) however I'm having problems with BoxLayout. The priblem occurs on the line scoreDescPanel.add(lblScoreDesc[i]); at the bottom.

private JFrame frame;
private JPanel panel;
private JPanel dicePanel;
private JButton btnRoll;
private JButton[] btnDice = new JButton[5];

private JPanel mainPanel;
private JPanel scoreDescPanel;
private JPanel scoreBtnPanel;

private JLabel[] lblScoreDesc = new JLabel[20];
private JButton[] btnScore = new JButton[20];

private Yahtzee y = new Yahtzee();

public YahtzeeGUI(){
    createWindow();
    addButtonRoll();
    addButtonDice();
    addMainPanel();
    addScoreDesc();
    //addScoreCardUpper();
    //addScoreCardLower();


    frame.add(panel);
    frame.setVisible(true);
}

public void createWindow(){
    frame = new JFrame();
    frame.setTitle("Yahtzee");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(1000,700);
    panel = new JPanel(new BorderLayout());
    dicePanel = new JPanel();

    mainPanel = new JPanel();
    scoreDescPanel = new JPanel();

}

public void addButtonRoll(){
    btnRoll = new JButton ("Roll the Dice");
    btnRoll.addActionListener(new RollHandler());

    dicePanel.add (btnRoll);
    panel.add(dicePanel, BorderLayout.SOUTH);
}

public void addButtonDice(){

    for (int i = 0; i < btnDice.length; i++){
        btnDice[i] = new JButton(String.valueOf(y.dice[i].getFaceValue()));
        btnDice[i].addActionListener(new HoldHandler());
        dicePanel.add (btnDice[i]);
    }
    panel.add(dicePanel, BorderLayout.SOUTH);
}

public void addMainPanel(){
    mainPanel.setLayout(new BorderLayout());
    mainPanel.setBackground(Color.red);
    panel.add(mainPanel, BorderLayout.CENTER);
}

public void addScoreDesc(){
    lblScoreDesc[0] = new JLabel("UPPER SECTION");
    lblScoreDesc[1] = new JLabel("Aces");
    lblScoreDesc[2] = new JLabel("Twos");
    lblScoreDesc[3] = new JLabel("Threes");
    lblScoreDesc[4] = new JLabel("Fours");
    lblScoreDesc[5] = new JLabel("Fives");
    lblScoreDesc[6] = new JLabel("Sixes");
    lblScoreDesc[7] = new JLabel("TOTAL SCORE");
    lblScoreDesc[8] = new JLabel("BONUS");
    lblScoreDesc[9] = new JLabel("TOTAL UPPER");
    lblScoreDesc[10] = new JLabel("LOWER SECTION");
    lblScoreDesc[11] = new JLabel("3 of a Kind");
    lblScoreDesc[12] = new JLabel("4 of a Kind");
    lblScoreDesc[13] = new JLabel("Full House");
    lblScoreDesc[14] = new JLabel("Small Straight");
    lblScoreDesc[15] = new JLabel("Large Straight");
    lblScoreDesc[16] = new JLabel("Yahtzee!");
    lblScoreDesc[17] = new JLabel("Chance");
    lblScoreDesc[18] = new JLabel("TOTAL LOWER");
    lblScoreDesc[19] = new JLabel("GRAND TOTAL");

    mainPanel.add(scoreDescPanel, BorderLayout.WEST);
    scoreDescPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));

    for(int i = 0; i < lblScoreDesc.length; i++){
        scoreDescPanel.add(lblScoreDesc[i]);
    }
}

Solution

  • BoxLayout doesn't allow a different target container from that on which the layout is being set, i.e.

    scoreDescPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));
    

    should be

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

    Read: How to Use BoxLayout