1

I'm not to sure how to work with ArrayLists. I want to declare variables, add them to a panel, change their font and color etc. Here are the variables I have

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Calculator extends JFrame implements ActionListener {

// declare number buttons
private JButton zero = new JButton("0");
private JButton one = new JButton("1");
private JButton two = new JButton("2");
private JButton three = new JButton("3");
private JButton four = new JButton("4");
private JButton five = new JButton("5");
private JButton six = new JButton("6");
private JButton seven = new JButton("7");
private JButton eight = new JButton("8");
private JButton nine = new JButton("9");

public Calculator(String name) {

    // set overall layout
    setLayout(new BorderLayout());

    // Create panel
    JPanel grid = new JPanel();
    grid.setLayout(new GridLayout(4, 4, 4, 4));

    // Create font
    Font font = new Font("SERIF", Font.BOLD, 32);

    // Set Button Fonts and color
    zero.setFont(font);
    zero.setBackground(Color.cyan);
    one.setFont(font);
    one.setBackground(Color.cyan);
    two.setFont(font);
    two.setBackground(Color.cyan);
    three.setFont(font);
    three.setBackground(Color.cyan);
    four.setFont(font);
    four.setBackground(Color.cyan);
    five.setFont(font);
    five.setBackground(Color.cyan);
    six.setFont(font);
    six.setBackground(Color.cyan);
    seven.setFont(font);
    seven.setBackground(Color.cyan);
    eight.setFont(font);
    eight.setBackground(Color.cyan);
    nine.setFont(font);
    nine.setBackground(Color.cyan);

// add Buttons to grid
    grid.add(zero);
    grid.add(one);
    grid.add(two);
    grid.add(three);
    grid.add(four);
    grid.add(five);
    grid.add(six);
    grid.add(seven);
    grid.add(eight);
    grid.add(nine);

boolean edit = false;
    JTextField numberBar = new JTextField();
    numberBar.setSize(grid.WIDTH, 50);
    numberBar.setEditable(edit);

    // Add Number bar to panel
    add(numberBar, BorderLayout.NORTH);

    // Add grid to main panel
    add(grid);

    setTitle(name);
    setSize(500, 500);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

}

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Calculator calculator = new Calculator("Calculator");

}

As you can see this is a mess without ArrayLists.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user3352215
  • 11
  • 1
  • 1
  • 3
  • 2
    `ArrayList buttons` would be a good start .. – user2864740 Mar 12 '14 at 23:37
  • 2
    You don't even need an `ArrayList` - `new Button[10]` would do the trick. – Boris the Spider Mar 12 '14 at 23:38
  • or `List buttonList = new ArrayList<>();` – donfuxx Mar 12 '14 at 23:38
  • Just give it a try - and feel free to ask, if something is not working as expected. Also google will provide many examples and tutorials about working with `ArrayList`s. – slartidan Mar 12 '14 at 23:38
  • 2
    I just googled "java using an arraylist", and [this](http://stackoverflow.com/questions/2697182/how-to-use-an-array-list) was the first result.... – MirroredFate Mar 12 '14 at 23:39
  • Start with the [Collections](http://docs.oracle.com/javase/tutorial/collections/) trail. An `ArrayList` is simply (at a very basic level) a dynamic array. If you understand how arrays work, you will be able to work with `ArrayList` with out issue – MadProgrammer Mar 12 '14 at 23:41

1 Answers1

5

Use an ArrayList<JButton>. Add them using myList.add(myJButton) and manipulate them all using something like

for (JButton button : myList)
    button.setBackground(Color.cyan);
GreySwordz
  • 368
  • 1
  • 9