2017-12-20 4 views
-1

Ich habe so eine API. Es zeigt JTable mit 3 Spalten. Ich möchte, dass, wenn ich Preis und Menge in die Jtable-Ergebnis einfügen wird auf der Unterseite meines Jframe gesehen werden. Zum Beispiel füge ich Daten wie auf dem Bild ein und bekomme dann das Ergebnis (2 * 5) + (2 * 5) = 20. Mein Ergebnis wird 20 sein. Und dieses Ergebnis wird am unteren Rand des GUI-Fensters angezeigt.Wie zähle ich Daten in JTable und zeige auf der Unterseite meiner 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(); 
    } 
} 

Antwort

0

Update Ihr addRow Methode in der Klasse FuDisplayPanel die Gesamt wie diese und

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); 
    } 
} 

und tun dies bei FuGui.class

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

, wie auf der Unterseite zu schreiben, in der jtable? – Viola

+0

Ich bearbeitete die Antwort – baba

+0

Vielen Dank! – Viola

0
zurückzukehren

Zugriff auf die zugrunde liegenden Daten in der Tabelle über das Modell (Swing-Grund-):

TableModel model = table.getModel(); 

einen Zuhörer an den Tisch Modell Fügen Sie das automatische Updates zu tun:

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

table.getModel().addTableModelListener(listener); 
Verwandte Themen