如何在此表上执行加法操作?[英] How addition operation can be performed on this table?

问题描述

在此处输入图片描述

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import net.java.dev.designgridlayout.DesignGridLayout;
import java.io.*;
import net.java.dev.designgridlayout.Tag;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.*;
import java.sql.*;
import javax.swing.table.TableColumn;

class table1 {
    JFrame JF;
    Container C;
    JPanel JP;
    JLabel creditLabel;
    JComboBox credit;
    String[] Credit = { "Vasan Phalke", "Pansare", "Anil Kg", "Suresh",
            "Total Credit", "" };
    String[] Names = { "Name", "Qty", "Rate/ Kg", "Total Amt." };
    JTable table;
    DefaultTableModel model;
    JScrollPane scrollPane;

    public table1() {
        JF = new JFrame();
        JP = new JPanel();
        C = JF.getContentPane();
        C.setLayout(new BorderLayout());
        JF.pack();
        JF.setLocationRelativeTo(null);
        JF.setVisible(true);

        DesignGridLayout layout = new DesignGridLayout(JP);

        creditLabel = new JLabel("Credit");
        credit = new JComboBox<String>(Credit);
        model = new DefaultTableModel(Names, 6) {
            @Override
            public Class<?> getColumnClass(int columnIndex) {
                switch (columnIndex) {
                case 0:
                    return String.class;
                case 1:
                    return Integer.class;
                case 2:
                case 3:
                    return Double.class;
                }
                return super.getColumnClass(columnIndex);
            }

            public Object getValueAt(int row, int column) {

                if (column == 3) {
                    Integer i = (Integer) getValueAt(row, 1);
                    Double d = (Double) getValueAt(row, 2);
                    if (i != null && d != null) {
                        return i * d;
                    } else {
                        return 0 * 0;
                    }
                }
                return super.getValueAt(row, column);
            }

            public boolean isCellEditable(int arg0, int arg1) {
                return true;
            }

            public void setValueAt(Object aValue, int row, int column) {
                super.setValueAt(aValue, row, column);
                fireTableCellUpdated(row, 3);
            }
        };
        table = new JTable(model);
        {
            TableColumn nameColumn = table.getColumnModel().getColumn(0);
            nameColumn.setCellEditor(new DefaultCellEditor(credit));
        }

        scrollPane = new JScrollPane(table);

        layout.row().grid(creditLabel);
        layout.emptyRow();
        layout.row().grid().add(scrollPane);

        C.add(JP);

    }

    public static void main(String args[]) {
        new table1();
    }
}

我想要总金额的总数.列出现在单元格 (5,4) 上.我尝试了一些代码,但失败了,请在下面添加显示附件图像的代码.从过去的 5 小时开始,我被困在这张桌子上.提前致谢.

推荐答案

包含此方法 如下图所示.注意几个要点:

经过测试的代码:

import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class Table1 {

    JFrame frame;
    JComboBox credit;
    String[] rowNames = {
        "Vasan Phalke", "Pansare", "Anil Kg", "Suresh", "Total Credit", ""};
    String[] colNames = {"Name", "Qty", "Rate/ Kg", "Total Amt."};
    JTable table;
    DefaultTableModel model;
    JScrollPane scrollPane;

    public Table1() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        credit = new JComboBox<>(rowNames);
        model = new DefaultTableModel(colNames, 6) {
            @Override
            public Class<?> getColumnClass(int columnIndex) {
                switch (columnIndex) {
                    case 0:
                        return String.class;
                    case 1:
                        return Integer.class;
                    case 2:
                        return Double.class;
                    case 3:
                        return Double.class;
                }
                return super.getColumnClass(columnIndex);
            }

            @Override
            public Object getValueAt(int row, int col) {
                if (col == 3 & row == 5) {
                    double sum = 0;
                    for (int i = 0; i < 5; i++) {
                        sum += ((Double) getValueAt(i, 3)).doubleValue();
                    }
                    return sum;
                }
                if (col == 3 & row != 5) {
                    Integer i = (Integer) getValueAt(row, 1);
                    Double d = (Double) getValueAt(row, 2);
                    if (i != null && d != null) {
                        return i * d;
                    } else {
                        return 0d;
                    }
                }
                return super.getValueAt(row, col);
            }

            @Override
            public void setValueAt(Object aValue, int row, int col) {
                super.setValueAt(aValue, row, col);
                fireTableDataChanged();
            }

            @Override
            public boolean isCellEditable(int row, int col) {
                return col != 3;
            }
        };
        table = new JTable(model);
        TableColumn nameColumn = table.getColumnModel().getColumn(0);
        nameColumn.setCellEditor(new DefaultCellEditor(credit));
        scrollPane = new JScrollPane(table);
        frame.add(new JScrollPane(table));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Table1();
            }
        });
    }
}

本文地址:https://www.itbaoku.cn/post/978522.html