Search code examples
javaswingloopsjpanelborder-layout

Border Layout looping. JPanel/swing


I'm having trouble with my looping and 'BorderLayout'. When I compile and run this using the driver it seems that the add.west(etc) is being overwritten by the proceeding add.west. I am left only with the 9th component in the 'south' panel, with 'east' and 'west' being completely empty. If I change the "for (int i=0; i<8; i++){" to: "for (int i=0; i<2; i++){" I get ONLY the second element of the required 9 in the 'west' panel. Can anyone please tell me why. Forgive my ignorance. I'm a beginner.

Thankyou. Joe

This is roughly what it should look like:

(WEST)                                           (EAST)

btn0, label0, label0                            btn4, label4, label4
btn1, label1, label1                            btn5, label5, label5
btn2, label2, label2                            btn6, label6, label6
btn3, label3, label3                            btn7, label7, label7

                           (SOUTH)
                    btn8, label8, label8

//CODE STARTS HERE:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.NumberFormat;

public class CoinPanel extends JPanel{
  private JButton buttons[] = new JButton[9];
  private JLabel multiplySign[] = new JLabel[9];
  private JLabel coinCount[] = new JLabel[9];
  String [] names= {"1c", "2c", "5c", "10c", "20c", "50c", "€1", "€2", "Reset"};
  int [] values= {1, 2, 5, 10, 20, 50, 100, 200, 0};

  public CoinPanel(){
    for (int i=0; i<8; i++){
      buttons[i] = new JButton(names[i]);
      buttons[i].addActionListener(new BtnListener());
      coinCount[i] = new JLabel("0", JLabel.CENTER);
      coinCount[i].setBorder(BorderFactory.createLineBorder(Color.black));
      multiplySign[i] = new JLabel ("x", JLabel.CENTER);

//Layout stuff from here:

      setLayout (new BorderLayout());
      JPanel west= new JPanel();
      west.setBackground(Color.BLACK);
      JPanel east= new JPanel();
      east.setBackground(Color.RED);
      JPanel south= new JPanel();
      south.setBackground(Color.BLUE);

      if(i<4){
        west.add (buttons[i]);
        west.add (multiplySign[i]);
        west.add (coinCount[i]);
      }
      else if(i<8){
        east.add (buttons[i]);
        east.add (multiplySign[i]);
        east.add (coinCount[i]);
      } 
      else{
        multiplySign[i].setText("TOTAL");
        south.add (multiplySign[i]);
        south.add (coinCount[i]);
        south.add (buttons[i]);
      }
      add(west, BorderLayout.WEST);     
      add(east, BorderLayout.EAST);
      add(south, BorderLayout.SOUTH);
     }

    setPreferredSize (new Dimension(450,300));
  }

//To here^^^

private class BtnListener implements ActionListener{
    public void actionPerformed (ActionEvent event){
      String [] text = new String[9];
      int [] intArray = new int [9];
      double sum =0;
      for (int i=0; i<(intArray.length-1); i++){
        if(event.getSource() == buttons[i]){
          text[i] = coinCount[i].getText();
          intArray[i]=Integer.parseInt(text[i]);
          intArray[i] = ((intArray[i]) +1);
          coinCount[i].setText(intArray[i] + "");
       }    
       if(event.getSource() == buttons[8]){
         coinCount[i].setText("0");
       }
       sum += (Integer.parseInt(coinCount[i].getText())*values[i]);
       NumberFormat nf = NumberFormat.getCurrencyInstance();
       coinCount[8].setText(nf.format(sum/100)+"");
      }
    }
  }
}

//AND THIS IS THE DRIVER:

import javax.swing.*;

public class CoinSorter{
   public static void main(String[] args){ 
      JFrame frame = new JFrame ("Coin Counter Example");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      CoinPanel panel = new CoinPanel();
      frame.getContentPane().add(panel);
      frame.pack();
      frame.setVisible(true);
   }
}

Solution

  • Am I not only adding a single Panel to the three locations?

    No, you have too much code in your loop.

    1) You are creating 3 new panels every time you execute the loop.

    • the creation of your 3 west, east and south panels should be done before the loop starts.

    2) Then at the end of the loop you are adding each of these panels to your main panel.

    • the three panels should be added to the main panel outside of the loop.