2012-03-20 109 views
1

因此,我有一個MainFrame類,其中有一個JTable,列出了存儲在數據庫中的所有產品。 JButton在偵聽器的幫助下將打開AddProduct(另一個類和另一個窗口/框架),我可以在其中添加產品。不幸的是,一旦AddProduct添加新產品和自動關閉,我不確定如何在MainFrame中更新/重新驗證JTable。 有些人可以給我一些想法,我該如何輕鬆解決這個問題?Java,如何從另一個框架刷新一個框架中的JTable

由於程序是相當大的,這裏是它的相關部分: 從MainFrame.java

public JPanel tabProducts() { 
    JPanel panel = new JPanel(new MigLayout("","20 [grow, fill] 10 [grow, fill] 20", "20 [] 10 [] 20")); 

    /** Labels **/ 
    JLabel label = new JLabel("List of all available products"); 

    /** Buttons **/ 
    JButton add = new JButton("Add product"); 
    add.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      new AddProduct(); 
     } 
    }); 
    JButton update = new JButton("Update product"); 
    update.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      new UpdateProduct(ps.getProductByID(15)); 
     } 
    }); 

    /** TABLE: Products **/ 
    String[] tableTitle = new String[] {"ID", "Name", "Type", "Price", "In stock"}; 
    String[][] tableData = null; 
    DefaultTableModel model = new DefaultTableModel(tableData, tableTitle); 
    JTable table = null; 
    /** Disable editing of the cell **/ 
    table = new JTable(model){ 
     public boolean isCellEditable(int r, int c) { 
      return false; 
     } 
    }; 
    /** Load the products from DB **/ 
    List<Product> listInv = ps.getProductsByAtt(new ArrayList<String>()); 
    for (int i = 0; i < listInv.size(); i++) { 
     model.insertRow(i, new Object[] { 
       listInv.get(i).getID(), 
       listInv.get(i).getName(), 
       listInv.get(i).getType(), 
       listInv.get(i).getPrice(), 
       listInv.get(i).getQuantity() 
     }); 
    } 
    /** Add scroll pane **/ 
    JScrollPane scrollpane = new JScrollPane(table); 

    /** Add everything to the panel **/ 
    panel.add(label, "wrap, span"); 
    panel.add(scrollpane, "wrap, span"); 
    panel.add(add); 
    panel.add(update); 

    return panel; 
} 

而且AddProduct.java

public class AddProduct { 

    private JFrame frame; 
    private JButton add, cancel; 
    private JRadioButton food, beverage; 
    private JTextField name, price, quantity; 
    private IProductService ps = new ProductService(); 
    private ButtonGroup group = new ButtonGroup(); 
    private Product p; 
    private String type = ""; 

    public AddProduct() { 
     /** Frame options **/ 
     frame = new JFrame("Add new product"); 
     frame.setSize(400, 280); 
     frame.setMinimumSize(new Dimension(400, 280)); 
     frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); 

     /** Default panel **/ 
     final JPanel panel = new JPanel(new MigLayout("","20 [grow, fill] 10 [grow, fill] 20", "20 [] 10 [] 20")); 

     /** Radio Buttons to choose between the food and the beverages **/ 
     food = new JRadioButton("Food"); 
     beverage = new JRadioButton("Beverage"); 
     group.add(food); 
     group.add(beverage); 
     food.addActionListener(new ActionListener(){ 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       type = "Food"; 
       frame.validate(); 
      } 
     }); 
     beverage.addActionListener(new ActionListener(){ 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       type = "Beverage"; 
       frame.validate(); 
      } 
     }); 

     /** Add everything to the panel **/ 
     panel.add(new JLabel("Product ID")); 
     panel.add(new JLabel(Integer.toString(ps.getProductNr()+1)), "wrap, span 2"); 
     panel.add(new JLabel("Name")); 
     panel.add(name = new JTextField(""), "wrap, span 2"); 
     panel.add(new JLabel("Type")); 
     panel.add(food); 
     panel.add(beverage, "wrap"); 
     panel.add(new JLabel("Price")); 
     panel.add(price = new JTextField(""), "wrap, span 2"); 
     panel.add(new JLabel("Quantity")); 
     panel.add(quantity = new JTextField(""), "wrap, span 2"); 


     /** Button: ADD **/ 
     add = new JButton("Add product"); 
     add.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 

       if (!type.equals("Food") && !type.equals("Beverage")) { 
        JOptionPane.showMessageDialog(panel, "Please choose the type of this product."); 
       } else if (name.getText().equals("")) { 
        JOptionPane.showMessageDialog(panel, "Please type a name for this product."); 
       } else if (price.getText().equals("")) { 
        JOptionPane.showMessageDialog(panel, "Please enter the price for this product."); 
       } else if (quantity.getText().equals("")) { 
        JOptionPane.showMessageDialog(panel, "Please enter the available amount of this product in stock."); 
       } else { 
        try { 
         p = new Product(ps.getProductNr()+1, name.getText(), type, Double.parseDouble(price.getText()), Integer.parseInt(quantity.getText())); 
         if (ps.addProduct(p)) { 
          JOptionPane.showMessageDialog(panel, "Product successfully added!"); 
          frame.validate(); 
          frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING)); 
         } 
        } catch (Exception ex) { 
         addFinalError(); 
        } 
       } 
      } 
     }); 

     /** Button: CANCEL **/ 
     cancel = new JButton("Cancel"); 
     cancel.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING)); 
      } 
     }); 

     /** Add buttons to the panel **/ 
     panel.add(cancel); 
     panel.add(add, "span 2"); 

     /** Add panel to frame and make it visible **/ 
     frame.add(panel); 
     frame.setVisible(true); 

    } 

    /** 
    * In case more then one error is encountered 
    */ 
    private void addFinalError(){ 
     JOptionPane.showMessageDialog(frame, "An error occured while adding the product. Please make sure the following is correct:\n\n" + 
       " Name : Can contain letters and numbers\n" + 
       " Price : Must be a number\n" + 
       " Quantity : Must be a whole number\n"); 
    } 
} 
+0

不確定要嘗試什麼。我的第二天曾經和gui一起工作過,所以我有點害怕,因爲與我還沒有做的其他程序相比,這只是一個小小的挫折。我用Google搜索了它,但大多數答案都沒有幫助,或者我不知道如何實現它們。所以,當我問我應該從哪裏開始。我不想找人爲我編程,只是爲了指點我。 – vedran 2012-03-20 11:18:52

回答

0

也許在AddProduct類返回創建Product將解決你的問題的靜態方法。看看在JOptionPane API例如static String showInputDialog(Object message)

+0

爲什麼在所有事情的名字中,你推薦使用靜態任何東西?對不起,但這是可怕的建議。 -1票。 – 2012-03-21 14:37:32

1

您需要在JTable的模型的一部分工作,然後刷新,重新驗證將工作。 試試看的JTable動態更新的一些例子像create TableModel and populate jTable dynamically

+2

再次:如果模型的行爲您沒有_need_(不重新刷新不刷新不重新驗證)在_JTable_上執行任何操作... – kleopatra 2012-03-20 11:58:27

0

最簡單的方法是在主類中的方法填充數據,並在您的actionPerformed方法,你正在處理添加新記錄調用該方法後的表一個記錄已被添加。這樣,主類正在處理表模型的更新,而JTable的內部將處理表的重繪。如果添加記錄成功,您甚至可以使用UpdateProducts中的方法僅更新表。

public void actionPerformed(ActionEvent e) { 
     UpdateProduct up = new UpdateProduct(); 
     if(up.addRecord(ps.getProductByID(15))){ 
      fillTable(); 
     } 
    } 

希望有助於一些。

+1

你可能意味着正確的事情,只是_main類正在處理... JTable_的重繪是錯誤的:表內部將觸發所有必要的重繪(在Swing中重繪,btw) - 假設模型正在觸發正確的事件DefaultTableModel是否 – kleopatra 2012-03-20 11:57:11

+0

感謝您的清除。我不太經常使用揮杆。 – ChadNC 2012-03-20 12:06:36

相關問題