2017-12-20 22 views
-1

私はそのようなAPIを持っています。 3つの列を持つJTableを示します。 jtableの結果に価格と数量を挿入すると、jframeの一番下に表示されます。例えば、画像のようにデータを挿入し、結果(2 * 5)+(2 * 5)= 20を得る。私の結果は20になります。そして、この結果はGUIウィンドウの一番下に表示されます。jtableのデータをカウントして私のguiの下に表示する方法

enter image description here

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.table.DefaultTableModel; 

public class Demo extends JFrame implements ActionListener{ 

    private static void createAndShowUI() { 

     JFrame frame = new JFrame("Customer Main"); 
     frame.getContentPane().add(new FuGui(), BorderLayout.CENTER); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowUI(); 
      } 
     }); 
    } 

    @Override 
    public void actionPerformed(ActionEvent arg0) { 
     // TODO Auto-generated method stub 

    } 
} 

class FuGui extends JPanel { 
    FuDisplayPanel displayPanel = new FuDisplayPanel(); 
    FuButtonPanel buttonPanel = new FuButtonPanel(); 
    FuInformationPanel informationPanel = new FuInformationPanel(); 

    public FuGui() { 
     //JTextField textField; 
     //textField = new JTextField(20); 
     //textField.addActionListener(this); 
     JPanel bottomPanel = new JPanel(); 
     bottomPanel.add(buttonPanel); 
     bottomPanel.add(Box.createHorizontalStrut(10)); 
     bottomPanel.add(informationPanel); 

     setLayout(new BorderLayout()); 
     add(displayPanel, BorderLayout.CENTER); 
     add(bottomPanel, BorderLayout.SOUTH); 

     buttonPanel.addInfoBtnAddActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       String name = informationPanel.getName(); 
       String price = informationPanel.getPrice(); 
       String quantity = informationPanel.getQuantity(); 

       displayPanel.addRow(name, price, quantity); 
      } 
     }); 
    } 
} 

class FuDisplayPanel extends JPanel { 
    private String[] COLUMNS = {"Name", "Price", "Quantity"}; 
    private DefaultTableModel model = new DefaultTableModel(COLUMNS, 1); 
    private JTable table = new JTable(model); 

    public FuDisplayPanel() { 
     setLayout(new BorderLayout()); 
     add(new JScrollPane(table)); 
    } 

    public void addRow(String name, String price, String quantity) { 
     Object[] row = new Object[3]; 
     row[0] = name; 
     row[1] = price; 
     row[2] = quantity; 
     model.addRow(row); 
    } 
} 

class FuButtonPanel extends JPanel { 
    private JButton addInfoButton = new JButton("Add Information"); 

    public FuButtonPanel() { 
     add(addInfoButton); 
    } 

    public void addInfoBtnAddActionListener(ActionListener listener) { 
     addInfoButton.addActionListener(listener); 
    } 
} 

class FuInformationPanel extends JPanel { 
    private JTextField nameField = new JTextField(10); 
    private JTextField priceField = new JTextField(10); 
    private JTextField quantityField = new JTextField(10); 

    public FuInformationPanel() { 
     add(new JLabel("Kwota:")); 
     add(nameField); 
     add(Box.createHorizontalStrut(10)); 
     // add(new JLabel("Price:")); 
     // add(priceField); 
     //add(new JLabel("Quantity:")); 
     // add(quantityField); 
    } 

    public String getName() { 
     return nameField.getText(); 
    } 

    public String getPrice() { 
     return priceField.getText(); 
    } 

    public String getQuantity() { 
     return quantityField.getText(); 
    } 
} 

答えて

0

このように、合計を返すためにFuDisplayPanelクラスでのごのAddRow方法を更新し、

public int addRow(String name, String price, String quantity) { 
    Object[] row = new Object[3]; 
    row[0] = name; 
    row[1] = price; 
    row[2] = quantity; 
    model.addRow(row); 
    int total = 0; 
    for (int count = 0; count < model.getRowCount(); count++){ 
     price = model.getValueAt(count, 1).toString(); 
     quantity = model.getValueAt(count, 2).toString(); 
     if(price != null && !price.trim().equals("") && quantity != null && !quantity.trim().equals("")) { 
      total += Integer.parseInt(price) * Integer.parseInt(quantity); 
     } 
    } 
    return total; 
} 

public class FuButtonPanel extends JPanel { 
    private JButton addInfoButton = new JButton("Add Information"); 
    public JLabel total = new JLabel("Total : "); 

    public FuButtonPanel() { 
     add(addInfoButton); 
     add(total); 
    } 

    public void addInfoBtnAddActionListener(ActionListener listener) { 
     addInfoButton.addActionListener(listener); 
    } 

    public void setTotal(int total) { 
     this.total.setText("Total : " + total); 
    } 
} 

をしてFuGui.class

int total = displayPanel.addRow(name, price, quantity); 
buttonPanel.setTotal(total); 
+0

の下にで書く方法jtable? – Viola

+0

私は答えを編集しました – baba

+0

ありがとうございました! – Viola

0
でこれを行います

(基本的なスイング)モデルを経由して、テーブル内の基になるデータにアクセスします。

TableModel model = table.getModel(); 

自動更新を行うには、テーブルモデルにリスナーを追加します。

TableModelListener listener = new TableModelListener(tableChanged(TableModelEvent e) { 
// update code here 
}}; 

table.getModel().addTableModelListener(listener); 
関連する問題