2017-07-11 8 views
0

クラスCartを作成しました。内部はJTableと2つのArrayListです。何らかの理由で、私のJTableは表示されません。Javaスウィング - スクロールペインに表示されないJTable

は、ここに私のカートクラスです:

class Cart { 
    ArrayList<Product> products = new ArrayList<>(); // Holds the products themselves 
    ArrayList<Integer> quantities = new ArrayList<>(); // Holds the quantities themselves 
    JTable prdTbl = new JTable(); // The GUI Product Table 
    DefaultTableModel prdTblModel = new DefaultTableModel(); // The Table Model 
    Object[] columns = {"Description","Price","Quantity","Total"}; // Column Identifiers 
    DecimalFormat fmt = new DecimalFormat("$#,##0.00;$-#,##0.00"); // Decimal Format for formatting USD ($#.##) 

    Cart() { 
     setTableStyle(); 
    } 

    void renderTable() {   
     // Re-initialize the Table Model 
     this.prdTblModel = new DefaultTableModel(); 

     // Set the Table Style 
     setTableStyle(); 

     // Create a row from each list entry for product and quantity and add it to the Table Model 
     for(int i = 0; i < products.size(); i++) { 
      Object[] row = new Object[4]; 

      row[0] = products.get(i).getName(); 
      row[1] = products.get(i).getPrice(); 
      row[2] = quantities.get(i); 
      row[3] = fmt.format(products.get(i).getPrice() * quantities.get(i)); 

      this.prdTblModel.addRow(row); 
     } 

     this.prdTbl.setModel(this.prdTblModel); 
    } 

    void setTableStyle() { 
     this.prdTblModel.setColumnIdentifiers(columns); 
     this.prdTbl.setModel(this.prdTblModel); 
     this.prdTbl.setBackground(Color.white); 
     this.prdTbl.setForeground(Color.black); 
     Font font = new Font("Tahoma",1,22); 
     this.prdTbl.setFont(font); 
     this.prdTbl.setRowHeight(30); 
    } 

    JTable getTable() { 
     renderTable(); // Render Table 
     return this.prdTbl; 
    } 
} 

注:私は、彼らが必要ないと感じて、いくつかの方法は、addProduct()とremoveProduct()のような削除されました。あなたがそれらを見る必要がある場合は、お尋ねください。私はここで何かが欠けていた場合、私はわからない

/** 
* Initialize the contents of the frame. 
*/ 
private void initialize() { 
    frame = new JFrame(); 
    frame.setBounds(100, 100, 590, 425); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    Cart cart = new Cart(); 

    JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP); 
    frame.getContentPane().add(tabbedPane, "cell 0 0,grow"); 

    JPanel cartPanel = new JPanel(); 
    tabbedPane.addTab("Cart", null, cartPanel, null); 
    cartPanel.setLayout(new MigLayout("", "[grow]", "[][grow]")); 

    JScrollPane scrollPane = new JScrollPane(); 
    cartPanel.add(scrollPane, "cell 0 1,grow"); 

    table = new JTable(); 
    scrollPane.setViewportView(table); 

    JButton btnAdd = new JButton("Add"); 
    btnAdd.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      String[] item = {"Macadamia", "Hazelnut", "Almond", "Peanut", "Walnut", "Pistachio", "Pecan", "Brazil"}; 
      Double[] price = {2.00, 1.90, 1.31, 0.85, 1.12, 1.53, 1.25, 1.75}; 

      int choice = (int) (Math.random() * item.length); 

      Product p = new Product(item[choice], price[choice]); 

      cart.addProduct(p); 
      table = cart.getTable(); 
     } 
    }); 
    cartPanel.add(btnAdd, "flowx,cell 0 0"); 

    JButton btnRemove = new JButton("Remove"); 
    cartPanel.add(btnRemove, "cell 0 0"); 

    JButton btnClear = new JButton("Clear"); 
    cartPanel.add(btnClear, "cell 0 0"); 

} 

:ここ

は、Swingアプリケーションウィンドウのための私のinitialize()メソッドのですか?過去にこのようにうまくいった?私はtable = cart.getTable();に値を印刷しようとしましたが、値が正常に受信されているようですので、Cartクラスではなく、スウィングinitialize()と何か関係があります。しかし、Cartクラスも同様です。

答えて

1

正しい表を追加してもよろしいですか?あなたのコードを示しています。テーブルが宣言のWHERE

table = new JTable(); 
scrollPane.setViewportView(table); 

は、私が見ることができないのactionListenerの内側にあなたが新しいインスタンスを持つテーブルを初期化しながら、より一層のテーブルには、何も含まれていない、いない行何列:

table = cart.getTable(); 

が、スクロールJTableの別のインスタンスを保持します。

+0

私はあなたがそれを投稿する前にそれを認識しました。ありがとうございました@ugo –

1

cartPanelにカートを関連付けることはないようです。私はあなたの問題はここにあると思う:

JPanel cartPanel = new JPanel(); 

あなたは新しいパネルを作成し、それをあなたのカートをフックすることはありません。そうでなければ良い見えます。

幸運を祈る!

+0

@ugoが示唆しているように、そのようにしたい場合は、initialize()で新しいテーブルをカートテーブルに関連付けることもできます。 – Hans

+0

ありがとう@Hans –

+0

私の喜び@TommyYamakaitis、私は助けることができてうれしい! – Hans

関連する問題