Does anyone know how to iterate through an ArrayList and retrieve an item by using the actionPerformed method?
Basically I'm trying to let the user click a button. After that button is clicked a showInputDialog appears, then the user for instance enters the id number of a student. All that student's details will appear in a jtextArea or showMessageDialog box.
Can anyone show me? here is the actual code I was just using the student as example.
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class DepotAdmin
{
private ArrayList<Vehicle> DepoVehicle = new ArrayList<Vehicle>();
public DepotAdmin(String hd,String c, String m, String d,Fuel p,int EngSize)
{
DepoVehicle = new ArrayList<Vehicle>();
}
private void setupDepot(){
//DepotArraylist vehicles = new DepotArraylist("1111","1111","1111","1111",new Fuel(""),0);
DepoVehicle.add(new Car("BobMart","567890","Volvo","04/06/2014", new Fuel("Diesel"),6000));
DepoVehicle.add(new Car("BobMart","465783","BMW","12/052014", new Fuel ("Petrol"), 8000));
DepoVehicle.add(new Car("BobMart","348709","Ford","13/08/2014",new Fuel ("Petrol"), 3000));
DepoVehicle.add(new Car("BobMart","145367","Lexus","18/08/2015",new Fuel ("Diesel"), 2000));
DepoVehicle.add(new RentableCar("BobMart","847469","Citreon","08/07/2015",new Fuel("Diesel"),1000));
Car car1 = new Car("BobMart","567890","Volvo","04/06/2014", new Fuel("Diesel"),6000);
public Vehicle byMake(String make)
{
String st[]=DepoVehicle.toArray(new String[DepoVehicle.size()]);
for ( Vehicle v : DepoVehicle)
if (v.getMake().equals(make)){
return v;
}
return null;
}
private JFrame Depot ;
private JMenu Vehicles;
private JMenu searchVehicle;
private JMenu rentVehicle;
private JMenu returnVehicle;
private JMenuItem vehicles1;
private JMenuItem searchVehicle1;
private JMenuItem rentVehicle1;
private JMenuItem returnVehicle1;
private JTextArea textArea;
private JMenuBar menubar;
private JList vList;
private ResDepotAdmin listener;
private ResDepotAdmin listener1;
private ResDepotAdmin listener2;
private ResDepotAdmin listener3;
private JLabel repeatLabel;
private JTextField inputText;
private JPanel textPanel;
public DepotAdmin(){
//creating the frame to place the menu bar
Depot = new JFrame ("Depot");
//creating the menubar to place the menu items
menubar = new JMenuBar();
//set the menu bar in the frame
Depot.setJMenuBar(menubar);
//set the menus on the menu bar
Vehicles = new JMenu ("vehicles");
menubar.add(Vehicles);
searchVehicle = new JMenu ("search");
menubar.add(searchVehicle);
rentVehicle = new JMenu ("rent");
menubar.add(rentVehicle);
returnVehicle = new JMenu ("return");
menubar.add(returnVehicle);
//set the menu items on the menues
vehicles1 = new JMenuItem ("Vehicle List");
Vehicles.add(vehicles1);
searchVehicle1 = new JMenuItem ("Search Vehicle");
searchVehicle.add(searchVehicle1);
rentVehicle1 = new JMenuItem ("Rent Vehicle");
rentVehicle.add(rentVehicle1);
returnVehicle1 = new JMenuItem ("Return Vehicle");
returnVehicle.add(returnVehicle1);
//data display section
textArea = new JTextArea(70,70);
textPanel = new JPanel();
//add textarea to panel
textPanel.add(textArea);
//add panel to frame
Depot.add((textPanel),BorderLayout.CENTER);
listeners to respond to click by uaer
listener = new ResDepotAdmin();
listener1 = new ResDepotAdmin();
listener2 = new ResDepotAdmin();
listener3 = new ResDepotAdmin();
vehicles1.addActionListener(listener);
searchVehicle1.addActionListener(listener1);
rentVehicle1.addActionListener(listener2);
returnVehicle.addActionListener(listener3);
String vehicleMake = "make";
//JOptionPane.showInputDialog(searchVehicle1,"Enter vehicle make");
//set the size of the frame
Depot.setSize(600,600);
//aet the visibility of the frame
Depot.setVisible(true);
//prevent the running the the program on exit
Depot.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class ResDepotAdmin implements ActionListener
{
private String make;
private void setupDepot(){
DepotArraylist vehicles = new DepotArraylist("1111","1111","1111","1111",new Fuel(""),0);
vehicles.addVehicle(new Car("BobMart","567890","Volvo","04/06/2014", new Fuel("Diesel"),6000));
vehicles.addVehicle(new Car("BobMart","465783","BMW","12/052014", new Fuel ("Petrol"), 8000));
vehicles.addVehicle(new Car("BobMart","348709","Ford","13/08/2014",new Fuel ("Petrol"), 3000));
vehicles.addVehicle(new Car("BobMart","145367","Lexus","18/08/2015",new Fuel ("Diesel"), 2000));
vehicles.addVehicle(new RentableCar("BobMart","847469","Citreon","08/07/2015",new Fuel("Diesel"),1000));
Car car1 = new Car("BobMart","567890","Volvo","04/06/2014", new Fuel("Diesel"),6000);
}
public Vehicle findVehicle(String make)
{
for (Vehicle dv : DepoVehicle)
if (dv.getMake().equals(make))
return dv;
return null;
}
public void actionPerformed(ActionEvent e)
{
//DepotAdmin DepoVehicle = new DepotAdmin("null","null","null","null",new Fuel(""),0);
ArrayList<Vehicle> DepoVehicle = new ArrayList<Vehicle>();
DepoVehicle.add(new Car("BobMart","567890","Volvo","04/06/2014", new Fuel("Diesel"),6000));
DepoVehicle.add(new Car("BobMart","348709","Ford","13/08/2014",new Fuel ("Petrol"), 3000));
if ( e.getSource() == vehicles1){
textArea.append(DepoVehicle.toString()+"\n"+"\n");
}
String make = JOptionPane.showInputDialog(null,"Enter vehicle make");
if (e.getSource()== searchVehicle1){
for ( Vehicle v : DepoVehicle){ if (v.getMake().equals(make)){
textArea.append(make);
}
}
}
}
}
}
I wasn't really sure how to interpret the fact that you have a message about a car, but you want the student as an input. So I changed it around a little. Make your changes accordingly.
I just looped, and if the student Id matches the input, I appended the student to the text area
public void actionPerformed{
String stuId = JOptionPane.showInputDialog(null,"Enter Student ID");
for (Student student : pupils){
if (student.getId().equals(stuId)){
textArea.append(student);
break;
}
}
}
Also, I noticed you have an actionPerformed inside the main
method. Are you trying to create a GUI program? Becuase at this point, your code is far from accomplishing it.
You also seem to have a method inside of a main
which is a no no. You're code in no way compiles. Please post a compileable program so we can see exactly what you trying to accomplish. Because the code in your post is rubbish.
Another HUGE fault I noticed is here
String name;
String id;
public Student (String n, String i) {
n = name;
i = id;
}
Your constructor is wrong. n = name
does nothing. Instead you want
name = n;
id = i;
Also Student
take a String input, but you're trying to add an int
. Maybe that was just for an SSCCE attempt, but it were getting those input from a text field, it would work, but if you're hard coding it in, they need to be String.
pupils.add(new Student("james", "2345"));
pupils.add(new Student ("taylor", "5768"));
pupils.add(new Student("andy", "9874"));
Also for your Student
class if you want the String representation of the Student object, you should @Override the toString()
method, something like this
public String toString() {
//am i wrong here?
return "Student Name: " + name + ", ID: " + id;
}
Here's a simple complete runnable program so you can see how everything fits together. Examine it to see where you may be going wrong. Feel free to ask questions.
import java.awt.BorderLayout;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
public class StudentGUI extends JFrame {
private JTextArea jtaStudent = new JTextArea(10, 30);
private JButton showStudents = new JButton("Show Students");
ArrayList<Student> pupils = new ArrayList<>();
public StudentGUI() {
pupils.add(new Student("james", "2345"));
pupils.add(new Student("taylor", "5768"));
pupils.add(new Student("andy", "9874"));
add(showStudents, BorderLayout.SOUTH);
add(jtaStudent, BorderLayout.CENTER);
showStudents.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (Student student : pupils) {
jtaStudent.append(student + "\n");
}
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new StudentGUI();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
class Student {
String name;
String id;
public Student(String n, String i) {
name = n;
id = i;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public String toString() {
// am i wrong here?
return "Student Name: " + name + ", ID: " + id;
}
}
You created a constructor for:
public Students(parameter, parameter){}
, but you did not create a constructor for:
public Student(){}
Normally, if you do not create any constructors yourself, the compiler will create a no-arg cosntructor for you, but when you create any constructors yourself, you will need to create a no-arg constructor yourself. Hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.