0

I'm trying to create a calendar using Java GUI and I want to create a method to create the cells of each date. Is there any way to create a method to create a bunch of JTextAreas without manually creating each individual cell?

Creating a cell one by one I do:

public void createCell() {
    cell1 = new JTextArea(CELL_DIMENSIONS, CELL_DIMENSIONS);
}
1
  • 2
    If you already know how to create a cell manually, your next step is to learn how to use a loop. Commented Oct 13, 2016 at 18:20

1 Answer 1

2

You have many ways of doing that one possibility would be to create a List inside the method with the assistance of a for loop and make the method return it for you to use somewhere else.

public List<JTextArea> createMultipleCells(int numOfCells) {

         List<JTextArea> cells = new LinkedList<JTextArea>();

          for(int i = 0; i < numOfCells; i++){
            cells.add(new JTextArea(CELL_DIMENSIONS, CELL_DIMENSIONS));
          }

         return cells;
    }

Same thing with an array:

public JTextArea[] createMultipleCells(int numOfCells) {

             JTextArea[] cells = new JTextArea[numOfCells];

              for(int i = 0; i < numOfCells; i++){
                cells[i] = new JTextArea(CELL_DIMENSIONS, CELL_DIMENSIONS);
              }

             return cells;
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.