need help with retrieving data in vector using for-loop
ok for my code, i type in String name, password, phone number and int currentsalary and desiredsalary into the textfields and click Add, and it goes into the vector and name appears on the JList ApplicantList.
what do i add to my listselectionlistener so that by clicking on the name, i candisplay the details of the person whose name is in the list, back on the texfields?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
import java.util.*;
import java.util.Vector;
import javax.swing.JScrollPane.*;
import javax.swing.event.ListSelectionListener;
publicclass Employmentextends JFrame
{
//declare class variables
private JPanel jpApplicant, jpEverything,jpWEST, jpCENTRE, jpEAST, jpAddEditDelete,
jpCentreTOP, jpCentreBOT, jpEastTOP, jpEastCENTRE, jpEastBOT,
jpBlank1, panel1, panel2, panel3, panel4;
private JLabel jlblApplicantForm, jlblAppList, jlblName, jlblPhone,
jlblCurrentSalary, jlblPassword, jlblDesiredSalary,
jlblNotes, jlblApplicantSkills, jlblIndustrySkills,
jlblBlank1, jlblBlank2;
private JButton jbtnAdd1, jbtnEdit, jbtnDelete, jbtnSave, jbtnCancel,
jbtnAdd2, jbtnRemove;
private JTextField jtfName, jtfPhone, jtfCurrentSalary, jtfPassword,
jtfDesiredSalary;
String name,password,phone,currentsalary,desiredsalary,textareastuff;
String selectedname;
private JTabbedPane tabbedPane;
private DefaultListModel listModel,listModel2;
private ListSelectionModel listSelectionModel;
JList ApplicantList, ApplicantSkillsList, IndustrySkillsList;
protected JTextArea NotesList;
Label ApplicantListLabel, NotesListLabel, ApplicantSkillsLabel,
IndustrySkillsLabel;
Vector details =new Vector();
publicstaticvoid main(String []args)
{
Employment f =new Employment();
f.setVisible(true);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
f.setResizable(false);
}//end of main
public Employment()
{
setSize(800,475);
setTitle("E-commerce Placement Agency");
JPanel topPanel =new JPanel();
topPanel.setLayout(new BorderLayout() );
getContentPane().add( topPanel );
createPage1();
createPage2();
createPage3();
createPage4();
tabbedPane =new JTabbedPane();
tabbedPane.addTab("Applicant", panel1 );
tabbedPane.addTab("Job Order", panel2 );
tabbedPane.addTab("Skill", panel3 );
tabbedPane.addTab("Company", panel4 );
topPanel.add( tabbedPane, BorderLayout.CENTER );
}
publicvoid createPage1()//PAGE 1
{
panel1 =new JPanel();
panel1.setLayout(new BorderLayout());
jpApplicant=new JPanel();
jpApplicant.setLayout(new BorderLayout());
Font bigFont =new Font("TimesRoman", Font.BOLD,24);
jlblApplicantForm =new JLabel("\t\t\t\tAPPLICANT FORM ");
jlblApplicantForm.setFont(bigFont);
jpApplicant.add(jlblApplicantForm,BorderLayout.EAST);
panel1.add(jpApplicant,BorderLayout.NORTH);
/*****************************************WEST*********************************/
jpWEST =new JPanel();
jpWEST.setLayout(new BorderLayout());
//Applicant List
listModel2=new DefaultListModel();
ApplicantList =new JList(listModel2);
listSelectionModel = ApplicantList.getSelectionModel();
listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());
JScrollPane scrollPane3 =new JScrollPane(ApplicantList);
ApplicantListLabel =new Label("Applicant List:");
jpWEST.add(ApplicantListLabel,"North");
jpWEST.add(scrollPane3,"Center");
panel1.add(jpWEST,BorderLayout.WEST);
/*****************************************************************/
/*********CENTRE*********/
jpCENTRE =new JPanel();
jpCENTRE.setLayout(new GridLayout(2,1));
jpCentreTOP =new JPanel();
panel1.add(jpCENTRE,BorderLayout.CENTER);
jpCENTRE.add(jpCentreTOP);
jpCentreTOP.setLayout(new GridLayout(6,2));
jlblName =new JLabel("Name:");
jlblBlank1 =new JLabel ("");
jtfName =new JTextField(18);
jlblBlank2 =new JLabel("");
jlblPhone =new JLabel("Phone:");
jlblCurrentSalary =new JLabel("Current Salary:");
jtfPhone =new JTextField(13);
jtfCurrentSalary =new JTextField(7);
jlblPassword =new JLabel("Password:");
jlblDesiredSalary =new JLabel("Desired Salary:");
jtfPassword =new JTextField(13);
jtfDesiredSalary =new JTextField(6);
jpCentreTOP.add(jlblName);
jpCentreTOP.add(jlblBlank1);
jpCentreTOP.add(jtfName);
jpCentreTOP.add(jlblBlank2);
jpCentreTOP.add(jlblPhone);
jpCentreTOP.add(jlblCurrentSalary);
jpCentreTOP.add(jtfPhone);
jpCentreTOP.add(jtfCurrentSalary);
jpCentreTOP.add(jlblPassword);
jpCentreTOP.add(jlblDesiredSalary);
jpCentreTOP.add(jtfPassword);
jpCentreTOP.add(jtfDesiredSalary);
//Noteslist
jpCentreBOT =new JPanel();
jpCentreBOT.setLayout(new BorderLayout());
jpCENTRE.add(jpCentreBOT);
jpBlank1 =new JPanel();
JTextArea Noteslist=new JTextArea();
JScrollPane scroll3=new JScrollPane(Noteslist);
scroll3.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll3.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//scroll3.setSize(new Dimension(1,1));
NotesListLabel =new Label("Notes:");
jpCentreBOT.add(NotesListLabel,"North");
jpCentreBOT.add(scroll3,"Center");
jpCentreBOT.add(jpBlank1,"South");
panel1.add(jpCENTRE,BorderLayout.CENTER);
/******************************************************************/
/**********EAST**********/
//Applicant Skills Panel
//EAST ==> TOP
jpEAST =new JPanel();
jpEAST.setLayout(new BorderLayout());
jpEastTOP =new JPanel();
jpEastTOP.setLayout(new BorderLayout());
listModel =new DefaultListModel();
ApplicantSkillsList =new JList(listModel);
ApplicantSkillsLabel =new Label("Applicant Skills");
// JScrollPane scrollPane1 = new JScrollPane(ApplicantSkillsList,
// JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JScrollPane scrollPane1 =new JScrollPane(ApplicantSkillsList);
scrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
ApplicantSkillsList.setVisibleRowCount(6);
jpEastTOP.add(ApplicantSkillsLabel,"North");
//ApplicantSkillsList.addItemListener(new selectNameListener());
jpEastTOP.add(scrollPane1,"Center");
jpEAST.add(jpEastTOP,BorderLayout.NORTH);
panel1.add(jpEAST,BorderLayout.EAST);
//Add & Remove Buttons
//EAST ==> CENTRE
jpEastCENTRE =new JPanel();
jpEAST.add(jpEastCENTRE,BorderLayout.CENTER);
jbtnAdd2 =new JButton("Add");
jbtnRemove =new JButton("Remove");
//add buttons to panel
jpEastCENTRE.add(jbtnAdd2);
jpEastCENTRE.add(jbtnRemove);
//add listener to button
jbtnAdd2.addActionListener(new Add2Listener());
jbtnRemove.addActionListener(new RemoveListener());
//Industry Skills Panel
//EAST ==> BOTTOM
jpEastBOT =new JPanel();
jpEastBOT.setLayout(new BorderLayout());
String[] data ={"Access97","Basic Programming",
"C++ Programming","COBOL Programming",
"DB Design","Fortran programming"};
IndustrySkillsList =new JList(data);
JScrollPane scrollPane =new JScrollPane(IndustrySkillsList);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
IndustrySkillsLabel =new Label("Industry Skills:");
jpEastBOT.add(IndustrySkillsLabel,"North");
jpEastBOT.add(scrollPane,"Center");
jpEAST.add(jpEastBOT,BorderLayout.SOUTH);
/***********************************************************/
//BOTTOM
jpAddEditDelete=new JPanel();
jbtnAdd1=new JButton("Add");
jbtnEdit=new JButton("Edit");
jbtnDelete=new JButton("Delete");
jbtnSave=new JButton("Save");
jbtnCancel=new JButton("Cancel");
jpAddEditDelete.add(jbtnAdd1);
jpAddEditDelete.add(jbtnEdit);
jpAddEditDelete.add(jbtnDelete);
jpAddEditDelete.add(jbtnSave);
jpAddEditDelete.add(jbtnCancel);
//jbtnEdit.addActionListener(new EditListener());
jbtnAdd1.addActionListener(new Add1Listener());
jbtnCancel.addActionListener(new CancelListener());
panel1.add(jpAddEditDelete,BorderLayout.SOUTH);
}
publicvoid createPage2()//PAGE 2
{
panel2 =new JPanel();
panel2.setLayout(new GridLayout(1,1) );
panel2.add(new JLabel("Sorry,under construction" ) );
}
publicvoid createPage3()//PAGE 3
{
panel3 =new JPanel();
panel3.setLayout(new GridLayout( 1, 1 ) );
panel3.add(new JLabel("Sorry,under construction" ) );
}
publicvoid createPage4()//PAGE 4
{
panel4 =new JPanel();
panel4.setLayout(new GridLayout( 1, 1 ) );
panel4.add(new JLabel("Sorry,under construction" ) );
}
publicclass Add1Listenerimplements ActionListener
{
publicvoid actionPerformed(ActionEvent e)
{
//String name,password,phone,currentsalary,desiredsalary,textareastuff;
name = jtfName.getText();
password = jtfPassword.getText();
phone = jtfPhone.getText();
currentsalary = jtfCurrentSalary.getText();
int i= Integer.parseInt(currentsalary);
desiredsalary = jtfDesiredSalary.getText();
int j= Integer.parseInt(desiredsalary);
StoringData person =new StoringData(name,password,phone,i,j);
//textareastuff = NotesList.getText();
details.add(person);
listModel2.addElement(name);
jtfName.setText("");
jtfPassword.setText("");
jtfPhone.setText("");
jtfCurrentSalary.setText("");
jtfDesiredSalary.setText("");
// NotesList.setText("");
// NotesList.append("");
// details.addElement(name);
}
}
publicclass Add2Listenerimplements ActionListener
{
publicvoid actionPerformed(ActionEvent e)
{
String temp1;
temp1 = (String)IndustrySkillsList.getSelectedValue();
listModel.addElement(temp1);
}
}
publicclass RemoveListenerimplements ActionListener
{
publicvoid actionPerformed(ActionEvent e)
{
int index = ApplicantSkillsList.getSelectedIndex();
listModel.remove(index);
}
}
publicclass EditListenerimplements ActionListener
{
publicvoid actionPerformed(ActionEvent e)
{
}
}
publicclass DeleteListenerimplements ActionListener
{
publicvoid actionPerformed(ActionEvent e)
{
}
}
publicclass SaveListenerimplements ActionListener
{
publicvoid actionPerformed(ActionEvent e)
{
}
}
publicclass CancelListenerimplements ActionListener
{
publicvoid actionPerformed(ActionEvent e)
{
jtfName.setText("");
jtfPassword.setText("");
jtfPhone.setText("");
jtfCurrentSalary.setText("");
jtfDesiredSalary.setText("");
}
}
publicclass SharedListSelectionHandlerimplements ListSelectionListener
{
publicvoid valueChanged(ListSelectionEvent e)
{
selectedname =ApplicantList.getSelectedValue().toString();
// ListSelectionModel lsm = (ListSelectionModel)e.getSource();
System.out.println(selectedname);
// for(selectedname==n)
// {
//System.out.println(ApplicantList.getSelectedValue().toString());
//jtfName.setText(n);
//}
}
}
//int firstIndex = e.getFirstIndex();
//int lastIndex = e.getLastIndex();
//boolean isAdjusting = e.getValueIsAdjusting();
/*if (lsm.isSelectionEmpty())
{
System.out.println("hahahhaa");
} else {
// Find out which indexes are selected.
/*int minIndex = lsm.getMinSelectionIndex();
int maxIndex = lsm.getMaxSelectionIndex();
/*for (int i = minIndex; i <= maxIndex; i++)
{
/* if (lsm.isSelectedIndex(i))
{
System.out.println("hohoho");
}
}
}
System.out.println("haho");*/
}
2nd file:
//StoringData class
class StoringData{
private String name;
private String password;
private String phone;
private int currentsalary;
private int desiredsalary;
public StoringData()
{
name = null;
password = null;
phone = null;
currentsalary = 0;
desiredsalary = 0;
}
public StoringData(String n,String pw, String p, int c, int d)
{
name = n;
password = pw;
phone = p;
currentsalary = c;
desiredsalary = d;
}
public String getName1()
{
return name;
}
public String getPassword1()
{
return password;
}
public String getPhone1()
{
return phone;
}
public int getCurrentSalary1()
{
return currentsalary;
}
public int getdesiredsalary1()
{
return desiredsalary;
}
}
You will have to loop through details to find the matching applicant:
StoringData selectedPerson = null;
for (StoringData person : details) {
if (person.getName().equals(selectedName)) {
selectedPerson = person;
break;
}
}
if (selectedPerson != null) {
// set the relevant fields
}
Alternatively you could store details in a Map<String, StoringData> with the name as the key and data as the value, and instead of looping through each time, call StoringData selectedPerson = detailMap.get(selectedName)
Or even better get rid of details altogether and add the StoringDatas directly to the JList (make a toString for StoringData that just returns name, so they will be displayed correctly). You avoid duplicating your data that way, and you can simply call StoringData selectedPerson = ApplicantList.getSelectedValue()
ok i added that part inside my listselectionlistener
it says incompatible types in line 421.
and where do i insert :
StoringData selectedPerson = detailMap.get(selectedName)
and/or
StoringData selectedPerson = ApplicantList.getSelectedValue()
this is what the code looks like after i added the loop u gave me
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
import java.util.*;
import java.util.Vector;
import javax.swing.JScrollPane.*;
//import javax.swing.event.ListSelectionListener;
public class Employment extends JFrame
{
//declare class variables
private JPanel jpApplicant, jpEverything,jpWEST, jpCENTRE, jpEAST, jpAddEditDelete,
jpCentreTOP, jpCentreBOT, jpEastTOP, jpEastCENTRE, jpEastBOT,
jpBlank1, panel1, panel2, panel3, panel4;
private JLabel jlblApplicantForm, jlblAppList, jlblName, jlblPhone,
jlblCurrentSalary, jlblPassword, jlblDesiredSalary,
jlblNotes, jlblApplicantSkills, jlblIndustrySkills,
jlblBlank1, jlblBlank2;
private JButton jbtnAdd1, jbtnEdit, jbtnDelete, jbtnSave, jbtnCancel,
jbtnAdd2, jbtnRemove;
private JTextField jtfName, jtfPhone, jtfCurrentSalary, jtfPassword,
jtfDesiredSalary;
String name,password,phone,currentsalary,desiredsalary,textareastuff;
String selectedname;
private JTabbedPane tabbedPane;
private DefaultListModel listModel,listModel2;
private ListSelectionModel listSelectionModel;
JList ApplicantList, ApplicantSkillsList, IndustrySkillsList;
protected JTextArea NotesList;
Label ApplicantListLabel, NotesListLabel, ApplicantSkillsLabel,
IndustrySkillsLabel;
Vector details = new Vector();
public static void main(String []args)
{
Employment f = new Employment();
f.setVisible(true);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
f.setResizable(false);
}//end of main
public Employment()
{
setSize(800,475);
setTitle("E-commerce Placement Agency");
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
createPage1();
createPage2();
createPage3();
createPage4();
tabbedPane = new JTabbedPane();
tabbedPane.addTab( "Applicant", panel1 );
tabbedPane.addTab( "Job Order", panel2 );
tabbedPane.addTab( "Skill", panel3 );
tabbedPane.addTab( "Company", panel4 );
topPanel.add( tabbedPane, BorderLayout.CENTER );
}
public void createPage1()//PAGE 1
{
panel1 = new JPanel();
panel1.setLayout( new BorderLayout());
jpApplicant= new JPanel();
jpApplicant.setLayout(new BorderLayout());
Font bigFont = new Font("TimesRoman", Font.BOLD,24);
jlblApplicantForm = new JLabel("\t\t\t\tAPPLICANT FORM ");
jlblApplicantForm.setFont(bigFont);
jpApplicant.add(jlblApplicantForm,BorderLayout.EAST);
panel1.add(jpApplicant,BorderLayout.NORTH);
/*****************************************WEST*********************************/
jpWEST = new JPanel();
jpWEST.setLayout( new BorderLayout());
//Applicant List
listModel2=new DefaultListModel();
ApplicantList = new JList(listModel2);
listSelectionModel = ApplicantList.getSelectionModel();
listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());
JScrollPane scrollPane3 = new JScrollPane(ApplicantList);
ApplicantListLabel = new Label( "Applicant List:");
jpWEST.add(ApplicantListLabel,"North");
jpWEST.add(scrollPane3,"Center");
panel1.add(jpWEST,BorderLayout.WEST);
/*****************************************************************/
/*********CENTRE*********/
jpCENTRE = new JPanel();
jpCENTRE.setLayout(new GridLayout(2,1));
jpCentreTOP = new JPanel();
panel1.add(jpCENTRE,BorderLayout.CENTER);
jpCENTRE.add(jpCentreTOP);
jpCentreTOP.setLayout(new GridLayout(6,2));
jlblName = new JLabel( "Name:");
jlblBlank1 = new JLabel ("");
jtfName = new JTextField(18);
jlblBlank2 = new JLabel("");
jlblPhone = new JLabel("Phone:");
jlblCurrentSalary = new JLabel("Current Salary:");
jtfPhone = new JTextField(13);
jtfCurrentSalary = new JTextField(7);
jlblPassword = new JLabel("Password:");
jlblDesiredSalary = new JLabel("Desired Salary:");
jtfPassword = new JTextField(13);
jtfDesiredSalary = new JTextField(6);
jpCentreTOP.add(jlblName);
jpCentreTOP.add(jlblBlank1);
jpCentreTOP.add(jtfName);
jpCentreTOP.add(jlblBlank2);
jpCentreTOP.add(jlblPhone);
jpCentreTOP.add(jlblCurrentSalary);
jpCentreTOP.add(jtfPhone);
jpCentreTOP.add(jtfCurrentSalary);
jpCentreTOP.add(jlblPassword);
jpCentreTOP.add(jlblDesiredSalary);
jpCentreTOP.add(jtfPassword);
jpCentreTOP.add(jtfDesiredSalary);
//Noteslist
jpCentreBOT = new JPanel();
jpCentreBOT.setLayout( new BorderLayout());
jpCENTRE.add(jpCentreBOT);
jpBlank1 = new JPanel();
JTextArea Noteslist=new JTextArea();
JScrollPane scroll3=new JScrollPane(Noteslist);
scroll3.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll3.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//scroll3.setSize(new Dimension(1,1));
NotesListLabel = new Label( "Notes:");
jpCentreBOT.add(NotesListLabel,"North");
jpCentreBOT.add(scroll3,"Center");
jpCentreBOT.add(jpBlank1,"South");
panel1.add(jpCENTRE,BorderLayout.CENTER);
/******************************************************************/
/**********EAST**********/
//Applicant Skills Panel
//EAST ==> TOP
jpEAST = new JPanel();
jpEAST.setLayout( new BorderLayout());
jpEastTOP = new JPanel();
jpEastTOP.setLayout( new BorderLayout());
listModel = new DefaultListModel();
ApplicantSkillsList = new JList(listModel);
ApplicantSkillsLabel = new Label( "Applicant Skills");
// JScrollPane scrollPane1 = new JScrollPane(ApplicantSkillsList,
// JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JScrollPane scrollPane1 = new JScrollPane(ApplicantSkillsList);
scrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
ApplicantSkillsList.setVisibleRowCount(6);
jpEastTOP.add(ApplicantSkillsLabel,"North");
//ApplicantSkillsList.addItemListener(new selectNameListener());
jpEastTOP.add(scrollPane1,"Center");
jpEAST.add(jpEastTOP,BorderLayout.NORTH);
panel1.add(jpEAST,BorderLayout.EAST);
//Add & Remove Buttons
//EAST ==> CENTRE
jpEastCENTRE = new JPanel();
jpEAST.add(jpEastCENTRE,BorderLayout.CENTER);
jbtnAdd2 = new JButton("Add");
jbtnRemove = new JButton("Remove");
//add buttons to panel
jpEastCENTRE.add(jbtnAdd2);
jpEastCENTRE.add(jbtnRemove);
//add listener to button
jbtnAdd2.addActionListener(new Add2Listener());
jbtnRemove.addActionListener(new RemoveListener());
//Industry Skills Panel
//EAST ==> BOTTOM
jpEastBOT = new JPanel();
jpEastBOT.setLayout( new BorderLayout());
String[] data = {"Access97", "Basic Programming",
"C++ Programming", "COBOL Programming",
"DB Design", "Fortran programming"};
IndustrySkillsList = new JList(data);
JScrollPane scrollPane = new JScrollPane(IndustrySkillsList);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
IndustrySkillsLabel = new Label( "Industry Skills:");
jpEastBOT.add(IndustrySkillsLabel,"North");
jpEastBOT.add(scrollPane,"Center");
jpEAST.add(jpEastBOT,BorderLayout.SOUTH);
/***********************************************************/
//BOTTOM
jpAddEditDelete= new JPanel();
jbtnAdd1=new JButton("Add");
jbtnEdit=new JButton("Edit");
jbtnDelete=new JButton("Delete");
jbtnSave=new JButton("Save");
jbtnCancel=new JButton("Cancel");
jpAddEditDelete.add(jbtnAdd1);
jpAddEditDelete.add(jbtnEdit);
jpAddEditDelete.add(jbtnDelete);
jpAddEditDelete.add(jbtnSave);
jpAddEditDelete.add(jbtnCancel);
//jbtnEdit.addActionListener(new EditListener());
jbtnAdd1.addActionListener(new Add1Listener());
jbtnCancel.addActionListener(new CancelListener());
panel1.add(jpAddEditDelete,BorderLayout.SOUTH);
}
public void createPage2()//PAGE 2
{
panel2 = new JPanel();
panel2.setLayout( new GridLayout(1,1) );
panel2.add( new JLabel( "Sorry,under construction" ) );
}
public void createPage3()//PAGE 3
{
panel3 = new JPanel();
panel3.setLayout( new GridLayout( 1, 1 ) );
panel3.add( new JLabel( "Sorry,under construction" ) );
}
public void createPage4()//PAGE 4
{
panel4 = new JPanel();
panel4.setLayout( new GridLayout( 1, 1 ) );
panel4.add( new JLabel( "Sorry,under construction" ) );
}
public class Add1Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//String name,password,phone,currentsalary,desiredsalary,textareastuff;
name = jtfName.getText();
password = jtfPassword.getText();
phone = jtfPhone.getText();
currentsalary = jtfCurrentSalary.getText();
int i= Integer.parseInt(currentsalary);
desiredsalary = jtfDesiredSalary.getText();
int j= Integer.parseInt(desiredsalary);
StoringData person = new StoringData(name,password,phone,i,j);
//textareastuff = NotesList.getText();
details.add(person);
listModel2.addElement(name);
jtfName.setText("");
jtfPassword.setText("");
jtfPhone.setText("");
jtfCurrentSalary.setText("");
jtfDesiredSalary.setText("");
// NotesList.setText("");
// NotesList.append("");
// details.addElement(name);
}
}
public class Add2Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String temp1;
temp1 = (String)IndustrySkillsList.getSelectedValue();
listModel.addElement(temp1);
}
}
public class RemoveListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int index = ApplicantSkillsList.getSelectedIndex();
listModel.remove(index);
}
}
public class EditListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
public class DeleteListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
public class SaveListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
public class CancelListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jtfName.setText("");
jtfPassword.setText("");
jtfPhone.setText("");
jtfCurrentSalary.setText("");
jtfDesiredSalary.setText("");
}
}
public class SharedListSelectionHandler implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent e)
{
selectedname =ApplicantList.getSelectedValue().toString();
StoringData selectedPerson = null;
for (StoringData person : details)
{
if (person.getName1().equals(selectedname))
{
selectedPerson = person;
break;
}
}
if (selectedPerson != null)
{
// set the relevant fields
}
// ListSelectionModel lsm = (ListSelectionModel)e.getSource();
// System.out.println(selectedname);
// for(selectedname==n)
// {
//System.out.println(ApplicantList.getSelectedValue().toString());
//jtfName.setText(n);
//}
}
}
//int firstIndex = e.getFirstIndex();
//int lastIndex = e.getLastIndex();
//boolean isAdjusting = e.getValueIsAdjusting();
/*if (lsm.isSelectionEmpty())
{
System.out.println("hahahhaa");
} else {
// Find out which indexes are selected.
/*int minIndex = lsm.getMinSelectionIndex();
int maxIndex = lsm.getMaxSelectionIndex();
/*for (int i = minIndex; i <= maxIndex; i++)
{
/* if (lsm.isSelectedIndex(i))
{
System.out.println("hohoho");
}
}
}
System.out.println("haho");*/
}
Yeah my fault, I'm so used to parameterized types...
> for (StoringData person : details)
is the line in question? If you're using java 1.5+ (and I think if that line didn't give you a syntax error, you are), you can change
Vector details = new Vector();
to
Vector<StoringData> details = new Vector<StoringData>()
which just tells the compiler that that is the type of thing you will be putting in the vector so that it knows that's the type of thing it's getting out in the for loop.
Also unless the vector is being modified by multiple threads simultaneously, it's generally preferred to use an ArrayList to a Vector (it has the same API and I think is a little faster because it's not synchronized). So that line would become
List<StoringData> details = ArrayList<StoringData>();
The other suggestions I made were alternatives to this method of storing the StoringDatas that are faster/easier, but if you are going with the List<StoringData> route, you don't need to add them anywhere
thanks alot for your help. i really appreciate it. (:
so if i like wanna add the phone number of the person clicked, i just type:
jtfPhone.setText(person.getPhone1());
into the part where it says :
// set the relevant fields
?
current code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
import java.util.*;
import java.util.Vector;
import javax.swing.JScrollPane.*;
//import javax.swing.event.ListSelectionListener;
public class Employment extends JFrame
{
//declare class variables
private JPanel jpApplicant, jpEverything,jpWEST, jpCENTRE, jpEAST, jpAddEditDelete,
jpCentreTOP, jpCentreBOT, jpEastTOP, jpEastCENTRE, jpEastBOT,
jpBlank1, panel1, panel2, panel3, panel4;
private JLabel jlblApplicantForm, jlblAppList, jlblName, jlblPhone,
jlblCurrentSalary, jlblPassword, jlblDesiredSalary,
jlblNotes, jlblApplicantSkills, jlblIndustrySkills,
jlblBlank1, jlblBlank2;
private JButton jbtnAdd1, jbtnEdit, jbtnDelete, jbtnSave, jbtnCancel,
jbtnAdd2, jbtnRemove;
private JTextField jtfName, jtfPhone, jtfCurrentSalary, jtfPassword,
jtfDesiredSalary;
String name,password,phone,currentsalary,desiredsalary,textareastuff;
String selectedname;
private JTabbedPane tabbedPane;
private DefaultListModel listModel,listModel2;
private ListSelectionModel listSelectionModel;
JList ApplicantList, ApplicantSkillsList, IndustrySkillsList;
protected JTextArea NotesList;
Label ApplicantListLabel, NotesListLabel, ApplicantSkillsLabel,
IndustrySkillsLabel;
//Vector details = new Vector();
Vector<StoringData> details = new Vector<StoringData>();
//selectedname =ApplicantList.getSelectedValue().toString();
public static void main(String []args)
{
Employment f = new Employment();
f.setVisible(true);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
f.setResizable(false);
}//end of main
public Employment()
{
setSize(800,475);
setTitle("E-commerce Placement Agency");
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
createPage1();
createPage2();
createPage3();
createPage4();
tabbedPane = new JTabbedPane();
tabbedPane.addTab( "Applicant", panel1 );
tabbedPane.addTab( "Job Order", panel2 );
tabbedPane.addTab( "Skill", panel3 );
tabbedPane.addTab( "Company", panel4 );
topPanel.add( tabbedPane, BorderLayout.CENTER );
}
public void createPage1()//PAGE 1
{
panel1 = new JPanel();
panel1.setLayout( new BorderLayout());
jpApplicant= new JPanel();
jpApplicant.setLayout(new BorderLayout());
Font bigFont = new Font("TimesRoman", Font.BOLD,24);
jlblApplicantForm = new JLabel("\t\t\t\tAPPLICANT FORM ");
jlblApplicantForm.setFont(bigFont);
jpApplicant.add(jlblApplicantForm,BorderLayout.EAST);
panel1.add(jpApplicant,BorderLayout.NORTH);
/*****************************************WEST*********************************/
jpWEST = new JPanel();
jpWEST.setLayout( new BorderLayout());
//Applicant List
listModel2=new DefaultListModel();
ApplicantList = new JList(listModel2);
listSelectionModel = ApplicantList.getSelectionModel();
listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());
JScrollPane scrollPane3 = new JScrollPane(ApplicantList);
ApplicantListLabel = new Label( "Applicant List:");
jpWEST.add(ApplicantListLabel,"North");
jpWEST.add(scrollPane3,"Center");
panel1.add(jpWEST,BorderLayout.WEST);
/*****************************************************************/
/*********CENTRE*********/
jpCENTRE = new JPanel();
jpCENTRE.setLayout(new GridLayout(2,1));
jpCentreTOP = new JPanel();
panel1.add(jpCENTRE,BorderLayout.CENTER);
jpCENTRE.add(jpCentreTOP);
jpCentreTOP.setLayout(new GridLayout(6,2));
jlblName = new JLabel( "Name:");
jlblBlank1 = new JLabel ("");
jtfName = new JTextField(18);
jlblBlank2 = new JLabel("");
jlblPhone = new JLabel("Phone:");
jlblCurrentSalary = new JLabel("Current Salary:");
jtfPhone = new JTextField(13);
jtfCurrentSalary = new JTextField(7);
jlblPassword = new JLabel("Password:");
jlblDesiredSalary = new JLabel("Desired Salary:");
jtfPassword = new JTextField(13);
jtfDesiredSalary = new JTextField(6);
jpCentreTOP.add(jlblName);
jpCentreTOP.add(jlblBlank1);
jpCentreTOP.add(jtfName);
jpCentreTOP.add(jlblBlank2);
jpCentreTOP.add(jlblPhone);
jpCentreTOP.add(jlblCurrentSalary);
jpCentreTOP.add(jtfPhone);
jpCentreTOP.add(jtfCurrentSalary);
jpCentreTOP.add(jlblPassword);
jpCentreTOP.add(jlblDesiredSalary);
jpCentreTOP.add(jtfPassword);
jpCentreTOP.add(jtfDesiredSalary);
//Noteslist
jpCentreBOT = new JPanel();
jpCentreBOT.setLayout( new BorderLayout());
jpCENTRE.add(jpCentreBOT);
jpBlank1 = new JPanel();
JTextArea Noteslist=new JTextArea();
JScrollPane scroll3=new JScrollPane(Noteslist);
scroll3.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll3.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//scroll3.setSize(new Dimension(1,1));
NotesListLabel = new Label( "Notes:");
jpCentreBOT.add(NotesListLabel,"North");
jpCentreBOT.add(scroll3,"Center");
jpCentreBOT.add(jpBlank1,"South");
panel1.add(jpCENTRE,BorderLayout.CENTER);
/******************************************************************/
/**********EAST**********/
//Applicant Skills Panel
//EAST ==> TOP
jpEAST = new JPanel();
jpEAST.setLayout( new BorderLayout());
jpEastTOP = new JPanel();
jpEastTOP.setLayout( new BorderLayout());
listModel = new DefaultListModel();
ApplicantSkillsList = new JList(listModel);
ApplicantSkillsLabel = new Label( "Applicant Skills");
// JScrollPane scrollPane1 = new JScrollPane(ApplicantSkillsList,
// JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JScrollPane scrollPane1 = new JScrollPane(ApplicantSkillsList);
scrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
ApplicantSkillsList.setVisibleRowCount(6);
jpEastTOP.add(ApplicantSkillsLabel,"North");
//ApplicantSkillsList.addItemListener(new selectNameListener());
jpEastTOP.add(scrollPane1,"Center");
jpEAST.add(jpEastTOP,BorderLayout.NORTH);
panel1.add(jpEAST,BorderLayout.EAST);
//Add & Remove Buttons
//EAST ==> CENTRE
jpEastCENTRE = new JPanel();
jpEAST.add(jpEastCENTRE,BorderLayout.CENTER);
jbtnAdd2 = new JButton("Add");
jbtnRemove = new JButton("Remove");
//add buttons to panel
jpEastCENTRE.add(jbtnAdd2);
jpEastCENTRE.add(jbtnRemove);
//add listener to button
jbtnAdd2.addActionListener(new Add2Listener());
jbtnRemove.addActionListener(new RemoveListener());
//Industry Skills Panel
//EAST ==> BOTTOM
jpEastBOT = new JPanel();
jpEastBOT.setLayout( new BorderLayout());
String[] data = {"Access97", "Basic Programming",
"C++ Programming", "COBOL Programming",
"DB Design", "Fortran programming"};
IndustrySkillsList = new JList(data);
JScrollPane scrollPane = new JScrollPane(IndustrySkillsList);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
IndustrySkillsLabel = new Label( "Industry Skills:");
jpEastBOT.add(IndustrySkillsLabel,"North");
jpEastBOT.add(scrollPane,"Center");
jpEAST.add(jpEastBOT,BorderLayout.SOUTH);
/***********************************************************/
//BOTTOM
jpAddEditDelete= new JPanel();
jbtnAdd1=new JButton("Add");
jbtnEdit=new JButton("Edit");
jbtnDelete=new JButton("Delete");
jbtnSave=new JButton("Save");
jbtnCancel=new JButton("Cancel");
jpAddEditDelete.add(jbtnAdd1);
jpAddEditDelete.add(jbtnEdit);
jpAddEditDelete.add(jbtnDelete);
jpAddEditDelete.add(jbtnSave);
jpAddEditDelete.add(jbtnCancel);
//jbtnEdit.addActionListener(new EditListener());
jbtnAdd1.addActionListener(new Add1Listener());
jbtnCancel.addActionListener(new CancelListener());
panel1.add(jpAddEditDelete,BorderLayout.SOUTH);
}
public void createPage2()//PAGE 2
{
panel2 = new JPanel();
panel2.setLayout( new GridLayout(1,1) );
panel2.add( new JLabel( "Sorry,under construction" ) );
}
public void createPage3()//PAGE 3
{
panel3 = new JPanel();
panel3.setLayout( new GridLayout( 1, 1 ) );
panel3.add( new JLabel( "Sorry,under construction" ) );
}
public void createPage4()//PAGE 4
{
panel4 = new JPanel();
panel4.setLayout( new GridLayout( 1, 1 ) );
panel4.add( new JLabel( "Sorry,under construction" ) );
}
public class Add1Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//String name,password,phone,currentsalary,desiredsalary,textareastuff;
name = jtfName.getText();
password = jtfPassword.getText();
phone = jtfPhone.getText();
currentsalary = jtfCurrentSalary.getText();
int i= Integer.parseInt(currentsalary);
desiredsalary = jtfDesiredSalary.getText();
int j= Integer.parseInt(desiredsalary);
StoringData person = new StoringData(name,password,phone,i,j);
//textareastuff = NotesList.getText();
details.add(person);
listModel2.addElement(name);
jtfName.setText("");
jtfPassword.setText("");
jtfPhone.setText("");
jtfCurrentSalary.setText("");
jtfDesiredSalary.setText("");
// NotesList.setText("");
// NotesList.append("");
// details.addElement(name);
}
}
public class Add2Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String temp1;
temp1 = (String)IndustrySkillsList.getSelectedValue();
listModel.addElement(temp1);
}
}
public class RemoveListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int index = ApplicantSkillsList.getSelectedIndex();
listModel.remove(index);
}
}
public class EditListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
public class DeleteListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
public class SaveListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
public class CancelListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jtfName.setText("");
jtfPassword.setText("");
jtfPhone.setText("");
jtfCurrentSalary.setText("");
jtfDesiredSalary.setText("");
}
}
public class SharedListSelectionHandler implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent e)
{
selectedname =ApplicantList.getSelectedValue().toString();
StoringData selectedPerson = null;
for (StoringData person : details)
{
if (person.getName1().equals(selectedname))
{
selectedPerson = person;
break;
}
}
if (selectedPerson != null)
{
// set the relevant fields
}
}
}
ok i figured it out :Dthanks alot for your help