2012-10-08 145 views
1

我有一個JTable顯示保存在ArrayList集合中的客戶對象的列表。 從此JTable中,我希望能夠突出顯示特定的客戶,然後單擊一個「刪除客戶」按鈕,該按鈕從所選錶行中提取ID列值並使用它通過Iterator搜索ArrayList,找到匹配客戶,並將其從收藏中刪除。 我對揮杆組件和事件聽衆的專業知識有很多不足,我嘗試過5種不同的組合,但是我沒有辦法。我如何完成這項工作?我的代碼的相關部分到目前爲止是:JTable選擇和刪除

class CustomerFrame extends JFrame{ 
    public CustomerFrame(travelagent agent){ 

     CustomerPanel cp = new CustomerPanel(agent, this); 
     setSize(800,500); 
     setLocationRelativeTo(null); 
     setTitle("Actions for customer"); 


     add(cp); 

    } 
} 

class CustomerPanel extends JPanel implements ActionListener, ListSelectionListener{ 
    private travelagent agent; 
    private JButton addCust; 
    private JButton deleteCust; 
    private JButton editCust; 
    private JButton bookFlight; 
    private JButton exit; 
    private CustomerFrame frame; 
    private Object selectedID; 
    private JTable ref; 

    public CustomerPanel(travelagent agent, CustomerFrame frame){ 
     ListCustPanel lp = new ListCustPanel(agent); 
     setLayout(new GridLayout(2,1,5,5)); 

     ref = lp.getTable(); 
     ListSelectionModel sel = ref.getSelectionModel(); 
     sel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
     sel.addListSelectionListener(this); 

     this.agent = agent; 
     this.frame = frame; 
     JLabel options = new JLabel("Options:"); 
     addCust = new JButton("Add customer"); 
     deleteCust = new JButton("Delete customer"); 
     editCust = new JButton("Edit Customer"); 
     bookFlight = new JButton("Book a flight"); 
     exit = new JButton("Back to main menu"); 

     options.setHorizontalAlignment(SwingConstants.CENTER); 
     JPanel b = new JPanel(new GridLayout(3,2,5,5)); 

     b.add(options); 
     b.add(addCust); 
     b.add(deleteCust); 
     b.add(editCust); 
     b.add(bookFlight); 
     b.add(exit); 

     add(lp); 
     add(b); 

     addCust.addActionListener(this); 
     deleteCust.addActionListener(this); 
     editCust.addActionListener(this); 
     bookFlight.addActionListener(this); 
     exit.addActionListener(this); 
    } 


    public void valueChanged(ListSelectionEvent l){ 
     if(!l.getValueIsAdjusting()){ 
      int index = ref.getSelectedRow(); 
      selectedID = ref.getValueAt(index, 0); 
     } 
    } 

    public void actionPerformed(ActionEvent e){ 
     if(e.getSource() == addCust){ 
      NewCustFrame cf = new NewCustFrame(agent, frame); 
      cf.setVisible(true); 
      cf.setDefaultCloseOperation(NewCustFrame.DISPOSE_ON_CLOSE); 

     if(e.getSource() == deleteCust){ 
      DeleteCustFrame df = new DeleteCustFrame(agent, selectedID); 
      df.setVisible(true); 
      df.setDefaultCloseOperation(DeleteCustFrame.DISPOSE_ON_CLOSE); 
     } 
     if(e.getSource() == editCust){ 

     } 
     if(e.getSource() == bookFlight){ 

     } 
     if(e.getSource() == exit){ 
      frame.dispose(); 
     } 

    } 
    } 



} 





/* 
* this displays the list of customers: 
*/ 
class ListCustPanel extends JPanel{ 

    private static final long serialVersionUID = 1L; 
    private JTable table; 
    public ListCustPanel(travelagent agent){ 

     String[] colNames = {"ID", "First Name", "Last Name"}; 

     int size = agent.getCust().size(); //get the size of the arraylist in order to define an object array to contain information in 
     Object[][] data = new Object[size][3]; // create the array 

     int i = 0; 
     Iterator<customer> iter = agent.getCust().iterator(); //create an iterator to progress thought the arraylist 
     while(iter.hasNext()){ 
      customer temp = iter.next(); //assign the current customer to a variable 

      //extract its attributes and feed into object array 
      data[i][0] = temp.getID(); 
      data[i][1] = temp.getFname(); 
      data[i++][2] = temp.getLname(); 

     } 
     setLayout(new GridLayout(1,1,5,5)); 


     // convert the object array into a JTable 
     table = new JTable(data, colNames); 
     table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
     // create a sorter to sort table rows 
     TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel()); 
     table.setRowSorter(sorter); 
     ArrayList<SortKey> sortKeys = new ArrayList<RowSorter.SortKey>(); 
     sortKeys.add(new RowSorter.SortKey(2, SortOrder.ASCENDING)); 
     sorter.setSortKeys(sortKeys); 


     JScrollPane scrollPane = new JScrollPane(table); 
     table.setFillsViewportHeight(true); 
     add(scrollPane); 


    } 

    public JTable getTable(){ 
     return table; 
    } 
} 


/* 
* delete the customer using this 
*/ 
class DeleteCustFrame extends JFrame{ 
    public DeleteCustFrame(travelagent agent, Object ID){ 
     DeleteCustPanel dcp = new DeleteCustPanel(agent, this, ID); 
     add(dcp); 

     setSize(300,200); 
     setTitle("Delete Customer"); 
     setLocationRelativeTo(null); 

    } 

} 

class DeleteCustPanel extends JPanel implements ActionListener{ 
    private travelagent agent; 
    private JButton delete; 
    private JButton cancel; 
    private JTextField id; 
    private DeleteCustFrame frame; 
    public int ID; 

    public DeleteCustPanel(travelagent agent, DeleteCustFrame frame, Object ID){ 
     this.ID = (int)ID; 
     this.agent = agent; 
     setLayout(new GridLayout(3,1,5,5)); 

     String fname = null; 
     String lname = null; 
     boolean hasFlight = false; 
     Iterator<customer> iter = agent.getCust().iterator(); 
     while(iter.hasNext()){ 
      customer temp = iter.next(); 
      if(temp.getID() == this.ID){ 
       fname = temp.getFname(); 
       lname = temp.getLname(); 
      } 
      if(temp.getNumFlight() > 0){ 
       hasFlight = true; 
      } 
     } 

     JLabel desc = new JLabel("Customer: " + ID + " " + fname + " " + lname);; 
     desc.setHorizontalAlignment(SwingConstants.CENTER); 
     JLabel yesFlight; 
     if(hasFlight){ 
      yesFlight = new JLabel("This customer has flights booked!"); 
     }else{ 
      yesFlight = new JLabel("No flights are booked for this customer"); 
     } 

     yesFlight.setHorizontalAlignment(SwingConstants.CENTER); 

     JPanel buttonPanel = new JPanel(); 

     buttonPanel.setLayout(new GridLayout(1,2,5,5)); 
     buttonPanel.add(delete); 
     buttonPanel.add(cancel); 

     add(desc); 
     add(yesFlight); 
     add(buttonPanel); 

     delete.addActionListener(this); 
     cancel.addActionListener(this); 

    } 


    public void actionPerformed(ActionEvent e) { 
     if(e.getSource() == delete){ 
      Iterator<customer> iter = agent.getCust().iterator(); 
      while(iter.hasNext()){ 
       customer temp = iter.next(); 
       if(temp.getID() == ID){ 
        agent.deleteCust(temp); 
       } 
      } 
      JOptionPane.showMessageDialog(null, "Customer deleted!", null, JOptionPane.PLAIN_MESSAGE); 
      frame.dispose(); 
     } 
     if(e.getSource() == cancel){ 
      frame.dispose(); 
     } 

    } 
} 

到目前爲止這個當我點擊與客戶強調,不採取行動,刪除客戶。長屁股問題我知道,但如果它幫助,我懷疑我的問題發生在即時通訊嘗試使用ListSelectionListener。

+0

1)不要延長框架或面板,只使用每個實例。 2)爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –

+0

首先檢查你的代碼是否輸入了actionPerformed函數,如果是的話,這意味着它不能正確地得到源代碼......否則,它看起來沒問題。 –

+1

爲更好地幫助發佈[SSCCE](http://sscce.org/),否則調試需要一定的運氣和勇氣,對不起 – mKorbel

回答

4

JTable提供DefaultTableModel,使這更簡單。用它代替ArrayList

該方法包括removeRow(int)其中int(行)是從JTable.getSelectedRow()

+1

@Bundy並使用CardLayout來創建另一個窗口,使用JPopupMenu代替JButton,convertRowToModel因爲有RowSorter,還有另外3-4個最重要的方法,更多的是你的問題描述在你的問題 – mKorbel

+0

我不知道如何使用這個。這些藏品作爲不屬於這裏的旅行類的一部分來維護。 travellagent就像整個系統的基礎類。此外,我沒有在這裏提到它,但我必須爲航班集合構建類似的功能,然後我必須加載/保存集合到文件。 – Bundy

+0

這會將我們帶回......爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 @mKorbel期待着,但我只是覺得我會'瞎瞎'。 ;) –