It runs fine but when choosing the idCombo
with which I'm searching, the nameFIeld
which is also a JComboBox
does not change but remains the first in the list, while all other fields change accord.
package GUI;
import Logic.*; //Inherits from vector class found in the package Logic
import javax.swing.*;
import java.awt.*;
import java.awt.event.*; //Works with the ActionListener
public class Search extends JFrame implements ActionListener {
//Panels
private JPanel north = new JPanel();
private JPanel south = new JPanel();
private JPanel west = new JPanel();
private JPanel east = new JPanel();
private JPanel center = new JPanel();
//Labels
private JLabel mainTitle = new JLabel ("Search Records"); //Main title
private JLabel westLabel = new JLabel (" "); //To create margins
private JLabel eastLabel = new JLabel (" ");
//Labels and Text Fields
private JLabel idLabel = new JLabel("CD ID");
private JComboBox idCombo = new JComboBox();
//To create a drop down list of CD IDs
private JLabel nameLabel = new JLabel("Customer Name");
private JComboBox nameField;
private JLabel dateLabel = new JLabel("Date");
private JTextField dateField = new JTextField();
private JLabel subjectLabel = new JLabel("Subject");
private JTextField subjectField = new JTextField();
private JLabel fileLabel = new JLabel("File");
private JTextField fileField = new JTextField();
private JLabel pageLabel = new JLabel("Page");
private JTextField pageField = new JTextField();
//Buttons
private JButton exitButton = new JButton("Exit");
//Creating an instance of type VectorRecord
private VectorRecord vr;
private VectorCustomer vc;
public Search(VectorRecord vr, VectorCustomer vc){
//NORTH PANEL
super("Search Records");
this.vr = vr;
this.vc = vc;
this.setLayout(new BorderLayout()); //Setting the 'Search Records' window layout to Border Layout
this.add(north,BorderLayout.NORTH); //Adding the north panel to the Border Layout
mainTitle.setFont(new Font ("Arial",Font.BOLD,20)); //Arranging the font of the main title
mainTitle.setForeground(Color.black); //Arranging the color of the main title
north.add(mainTitle); // Adding the maintitle to the north panel
//CENTER PANEL
center.setLayout(new GridLayout(6,2,0,15)); //6 rows, 2 columns and spacing
this.add(center,BorderLayout.CENTER); //Adding the center panel to the Border Layout
//Adding Labels and TextFields to the center panel
center.add(idLabel);
center.add(idCombo);
idCombo.setModel(new DefaultComboBoxModel(vr.fillIdCombo())); //fillIdCombo is a method in VectorRecords class
idCombo.addActionListener(this);
idLabel.setFont(new Font ("Arial", Font.BOLD, 16)); //Arranging the font and size
center.add(nameLabel);
center.add(nameField);
nameField.setModel(new DefaultComboBoxModel(vc.fillCustomerCombo()));
nameField.addActionListener(this);
nameLabel.setFont(new Font ("Arial", Font.BOLD, 16)); //Arranging the font and size
center.add(dateLabel);
center.add(dateField);
dateLabel.setFont(new Font ("Arial", Font.BOLD, 16)); //Arranging the font and size
center.add(subjectLabel);
center.add(subjectField);
subjectLabel.setFont(new Font ("Arial", Font.BOLD, 16)); //Arranging the font and size
center.add(fileLabel);
center.add(fileField);
fileLabel.setFont(new Font ("Arial", Font.BOLD, 16)); //Arranging the font and size
center.add(pageLabel);
center.add(pageField);
pageLabel.setFont(new Font ("Arial", Font.BOLD, 16)); //Arranging the font and size
//WEST & EAST PANEL
//To create margins
west.setLayout(new FlowLayout()); //Setting the west panel in the border layout, to Flow layout
this.add(west,BorderLayout.WEST);
west.add(westLabel);
east.setLayout(new FlowLayout()); //Setting the east panel in the border layout, to Flow layout
this.add(east,BorderLayout.EAST);
east.add(eastLabel);
//SOUTH PANEL
south.setLayout(new FlowLayout()); //Setting the south panel in the border layout, to Flow layout
this.add(south,BorderLayout.SOUTH); //Adding the south panel to the BorderLayout
south.add(exitButton); //Adding the Exit button to the south panel
exitButton.addActionListener(this); //In order for the Exit button to work
this.setSize(450,350); //Giving the window a size and location a
this.setLocation(320,250);
this.setVisible(true);
}
public void actionPerformed (ActionEvent event){
Record tempRecord= new Record();
Record modifiedRecord = new Record();
String recordId = (String)idCombo.getSelectedItem();
tempRecord = vr.getRecordRec(Integer.parseInt(recordId));
if(event.getSource() instanceof JComboBox){
if(event.getSource() == idCombo){
nameField.setSelectedItem((Customer) tempRecord.getCustomer());
dateField.setText(tempRecord.getDate());
subjectField.setText(tempRecord.getSubject());
fileField.setText(tempRecord.getFile());
pageField.setText(tempRecord.getPage() + "");
}
}
if(event.getSource().equals(exitButton)){
vr.saveRecordsToFile();
this.dispose();
}
}
}