2012-10-08 57 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期待着,但我只是觉得我会'瞎瞎'。 ;) –