2013-02-12 141 views
0

它運行良好,但是當我選擇idCombo正在搜索中時,nameFIeld也是JComboBox不會更改,但仍然是列表中的第一個,而其他所有領域都是一致的。帶2個JComboBox的搜索功能 - 不顯示搜索第1個和第2個搜索

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();               
         } 


        } 


       } 
+2

爲了更快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2013-02-12 11:42:39

回答

0

當你調用setSelectedItem在名稱字段與tempRecord.getCustomer()返回客戶對象將嘗試找到對象是否已經存在於名稱字段組合框。

雖然檢查它將調用等於對象相等方法,請確保覆蓋客戶類的equals方法。