Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with arraylist Java ... I have this code and I don\'t know how to fini

ID: 652753 • Letter: N

Question

Need help with arraylist Java ...

I have this code and I don't know how to finish for

Data Arrays

You will need to use ArrayList (add, set, remove, indexof) in order to dynamically grow or shrink your data arrays as the user add, updates, and deletes records. You will need to use an ArrayList for the following data: id, name, temp min, temp max, moist min, moist max, nutrient min and nutrient max.

you don't need to do all, if you are able to complete 1 arraylist add when you use id to display in the panel will be a great help!..I can see how you do this and finish the rest set, remove, indextof....so on..

code:

package myFirstApp;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

public class MyFirstApp extends JFrame{

PlantsPanel plantsPanel;

public MyFirstApp() {
  setDefaultLookAndFeelDecorated(true);

  plantsPanel = new PlantsPanel();
  
  JMenuBar menuBar = new JMenuBar();
  JMenu menu = new JMenu("Menu");
  menuBar.add(menu);
  JMenuItem exitMenu = new JMenuItem("Exit");
  JMenuItem plantsMenu = new JMenuItem("Plants");
  
  exitMenu.addActionListener(new ActionListener(){
   @Override
   public void actionPerformed(ActionEvent e){
    System.exit(0);
   }
  });
  
  plantsMenu.addActionListener(new ActionListener(){
   @Override
   public void actionPerformed(ActionEvent e){
    getContentPane().removeAll();
    add(plantsPanel);
    revalidate();
    repaint();
   }
  });

  menu.add(plantsMenu);
  menu.add(exitMenu);
  
  setJMenuBar(menuBar);
        
     setTitle("My First App");
  setSize(700,600);
  setLocationRelativeTo(null);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setVisible(true);
  
}

public static void main(String[] args) {
  try {
      for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
      //Metal or Nimbus
          if ("Nimbus".equals(info.getName())) {
              UIManager.setLookAndFeel(info.getClassName());
              break;
          }
      }
  } catch (Exception e) {
      // If Nimbus is not available, you can set the GUI to another look and feel.
   System.out.println("Unable to set Nimbus look and feel.");
  }
  
  SwingUtilities.invokeLater(new Runnable() {
   @Override
   public void run() {
    new MyFirstApp();
   }
  });
}

}

//============================================

package myFirstApp;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Font;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.GridLayout;

import java.util.ArrayList;

import javax.swing.JButton;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextArea;

import javax.swing.JTextField;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class PlantsPanel extends JPanel{

private JTextArea plantTextArea = new JTextArea("Text Area");

private ArrayList<Integer> id = new ArrayList<Integer>();

private ArrayList <String> names = new ArrayList<String>();

private ArrayList<Integer> tempMin = new ArrayList<Integer>();

private ArrayList<Integer> tempMax = new ArrayList<Integer>();

private ArrayList<Integer> moistMin = new ArrayList<Integer>();

private ArrayList<Integer> moistMax = new ArrayList<Integer>();

private ArrayList<Integer> nutrientMin = new ArrayList<Integer>();

private ArrayList<Integer> nutrientMax = new ArrayList<Integer>();

//method will be used textField in constructor

private JTextField textField;

private JTextField textField1;

private JTextField textField33;

private JTextField textField3;

PlantsPanel(){

setLayout(new GridLayout(2,1));

JPanel inputPanel = new JPanel(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

inputPanel.setBackground(Color.getHSBColor(0.1f, 14.0f, 0.1f));

JLabel title = new JLabel("Plant Input Form");

title.setFont(new Font("Aharoni",Font.BOLD, 20));

title.setForeground(Color.BLACK);

gbc.anchor = GridBagConstraints.CENTER;

gbc.gridx = 1;

gbc.gridy = 0;

inputPanel.add(title,gbc);

JLabel label = new JLabel ("ID [1-50]:");

label.setFont(new Font("Estrangelo Edessa",Font.PLAIN,17));

label.setForeground(Color.BLACK);

gbc.anchor = GridBagConstraints.WEST;

gbc.gridx =0;

gbc.gridy =1;

inputPanel.add(label, gbc);

JTextField textField = new JTextField (10);

//align the field w/ y axes

gbc.gridx =1;

gbc.gridy =1;

//add textField and label to the panel

inputPanel.add(textField, gbc);

//search button created to align with ID #

JButton search = new JButton ("Search");

search.setFont(new Font("Estrangelo Edessa",Font.PLAIN,16));

gbc.gridx =2;

gbc.gridy =1;

inputPanel.add(search,gbc);

JLabel label1 = new JLabel ("Plant Name:");

label1.setFont(new Font("Estrangelo Edessa",Font.PLAIN,17));

label1.setForeground(Color.BLACK);

gbc.anchor = GridBagConstraints.WEST;

gbc.gridx =0;

gbc.gridy =10;

inputPanel.add(label1, gbc);

textField1 = new JTextField (10);

//align the field w/ y axes

gbc.gridx =1;

gbc.gridy =10;

inputPanel.add(textField1, gbc);

JLabel label2 = new JLabel ("Temp Min/Max [30-80]:");

label2.setFont(new Font("Estrangelo Edessa",Font.PLAIN,17));

label2.setForeground(Color.BLACK);

gbc.anchor = GridBagConstraints.WEST;

gbc.gridx =0;

gbc.gridy =20;

inputPanel.add(label2, gbc);

JTextField textField2 = new JTextField (10);

//align the field w/ y axes

gbc.gridx =1;

gbc.gridy =20;

//add textField and label to the panel

inputPanel.add(textField2, gbc);

JTextField textField22= new JTextField (10);

//align the field w/ y axes

gbc.gridx =2;

gbc.gridy =20;

gbc.gridwidth =1;

//add textField and label to the panel

inputPanel.add(textField22, gbc);

JLabel label3 = new JLabel ("Moist Min/Max [20-60]:");

label3.setFont(new Font("Estrangelo Edessa",Font.PLAIN,17));

label3.setForeground(Color.BLACK);

gbc.anchor = GridBagConstraints.WEST;

gbc.gridx =0;

gbc.gridy =30;

inputPanel.add(label3, gbc);

textField3 = new JTextField (10);

//align the field w/ y axes

gbc.gridx =1;

gbc.gridy =30;

//add textField and label to the panel

inputPanel.add(textField3, gbc);

textField33 = new JTextField (10);

//align the field w/ y axes

gbc.gridx =2;

gbc.gridy =30;

//add textField and label to the panel

inputPanel.add(textField33, gbc);

JLabel label4 = new JLabel ("Nutrient Min/Max [1-10]:");

label4.setFont(new Font("Estrangelo Edessai",Font.PLAIN,17));

label4.setForeground(Color.BLACK);

gbc.anchor = GridBagConstraints.WEST;

gbc.gridx =0;

gbc.gridy =40;

inputPanel.add(label4, gbc);

JTextField textField4 = new JTextField (10);

//align the field w/ y axes

gbc.gridx =1;

gbc.gridy =40;

//add textField and label to the panel

inputPanel.add(textField4, gbc);

JTextField textField44 = new JTextField (10);

//align the field w/ y axes

gbc.gridx =2;

gbc.gridy =40;

//add textField and label to the panel

inputPanel.add(textField44, gbc);

//create buttons and set location grid x and y

JButton add = new JButton ("Add");

gbc.gridx =1;

gbc.gridy =42;

inputPanel.add(add,gbc);

add.setFont(new Font("Estrangelo Edessa",Font.PLAIN,15));

//+++++++++++++++++++++++++++++++++++++++++++++++

//This IS WHERE I am lost!!!!!

//add actionlistener

add.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

if(e.getSource() == add);

{

// label1= label1.getText();

}

}

});

//method that allow to getText

//+++++++++++++++++++++++++++++++++++++++++

JButton update = new JButton ("Update");

gbc.gridx =2;

gbc.gridy =42;

inputPanel.add(update,gbc);

update.setFont(new Font("Estrangelo Edessa",Font.PLAIN,15));

JButton delete = new JButton ("Delete");

gbc.gridx =3;

gbc.gridy =42;

inputPanel.add(delete,gbc);

delete.setFont(new Font("Estrangelo Edessai",Font.PLAIN,14));

add(inputPanel);

add(plantTextArea);

}

}

Explanation / Answer

/*
Remove an element from specified index of Java ArrayList Example
This Java Example shows how to remove an element at specified index of java
ArrayList object using remove method.
*/

import java.util.ArrayList;

public class RemoveElementFromArrayListExample {

public static void main(String[] args) {
//create an ArrayList object
ArrayList arrayList = new ArrayList();

//Add elements to Arraylist
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");

/*
To remove an element from the specified index of ArrayList use
Object remove(int index) method.
It returns the element that was removed from the ArrayList.
*/
Object obj = arrayList.remove(1);
System.out.println(obj + " is removed from ArrayList");

System.out.println("ArrayList contains...");
//display elements of ArrayList
for(int index=0; index < arrayList.size(); index++)
System.out.println(arrayList.get(index));

}
}

/*
Output would be
2 is removed from ArrayList
ArrayList contains...
1
3
*/