java - 如何在java中减去两列值并显示在另一列中

标签 java swing jtable

在我的代码中,我想减去“金额和成本值”列并将其显示在利润列上。

这是我的代码...

package my.bill;


import java.awt.*;
import java.awt.event.*;
import java.awt.event.*;
import java.io.*;
import java.lang.*;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax .swing.table.DefaultTableModel;


public class bill extends javax.swing.JFrame {

    /**
     * Creates new form bill
     */
    Object[][] data=null;
    String[] columnNames = new String[2];

   /*static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL="jdbc:mysql://localhost:3306/gdb?zeroDateTimeBehavior=convertToNull";
   //static final String DB_URL = "jdbc:mysql://localhost/";

   //  Database credentials
   static final String USER = "root";
   static final String PASS = "root";*/



    public bill() {
        initComponents();
        CurrentDate();

           }

    public void CurrentDate(){
    Calendar cal=new GregorianCalendar();
    int month=cal.get(Calendar.MONTH);
    int year=cal.get(Calendar.YEAR);
    int day=cal.get(Calendar.DAY_OF_MONTH);
    Date.setText(day+"-"+(month+1)+"-"+year);
    }



    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        Date = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        netprofit = new javax.swing.JButton();
        netsum = new javax.swing.JTextField();
        save = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Date");

        Date.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DateActionPerformed(evt);
            }
        });

        jTable1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jTable1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"", null, null, null, null, null},
                {"", null, null, null, "", null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "JNo", "Item", "Amount", "cost", "Profit", "P"
            }
        ));
        jTable1.setColumnSelectionAllowed(true);
        jTable1.setGridColor(new java.awt.Color(0, 0, 0));
        jTable1.setRowHeight(20);
        jTable1.setRowMargin(2);
        jTable1.setSelectionBackground(new java.awt.Color(255, 255, 255));
        jTable1.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                jTable1InputMethodTextChanged(evt);
            }
        });
        jScrollPane2.setViewportView(jTable1);
        jTable1.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        if (jTable1.getColumnModel().getColumnCount() > 0) {
            jTable1.getColumnModel().getColumn(0).setHeaderValue("Job No");
            jTable1.getColumnModel().getColumn(1).setHeaderValue("Item");
            jTable1.getColumnModel().getColumn(2).setHeaderValue("Billed Amount");
            jTable1.getColumnModel().getColumn(3).setHeaderValue("Parts cost");
            jTable1.getColumnModel().getColumn(4).setHeaderValue("Net Profit");
            jTable1.getColumnModel().getColumn(5).setHeaderValue("Percentage");
        }

        netprofit.setText("Total Profit");
        netprofit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                netprofitActionPerformed(evt);
            }
        });

        save.setText("Save");
        save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(114, 114, 114)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Date, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(350, 350, 350)
                        .addComponent(netprofit)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(netsum, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(221, 221, 221)
                        .addComponent(save)))
                .addContainerGap(147, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Date, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(netprofit)
                    .addComponent(netsum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(1, 1, 1)
                .addComponent(save)
                .addContainerGap(32, Short.MAX_VALUE))
        );

        Date.getAccessibleContext().setAccessibleName("date");
        save.getAccessibleContext().setAccessibleName("save");

        pack();
    }// </editor-fold>                        



    private void DateActionPerformed(java.awt.event.ActionEvent evt) {                                     
        // TODO add your handling code here:

    }                                    

    private void netprofitActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:

    }                                         

    private void saveActionPerformed(java.awt.event.ActionEvent evt) {                                     
        // TODO add your handling code here:

        Connection conn = null;

   Statement stmt = null;

        int count=jTable1.getRowCount();
        int col=jTable1.getColumnCount();
       String jobno[] =new String[count]; // name is array and index 4 means no. of row 
        String item[]=new String[count]; 
        String bill[] =new String[count]; // name is array and index 4 means no. of row 
         String part[]=new  String[count]; 
       String profit[] =new  String[count]; // name is array and index 4 means no. of row 
       String percent[]=new  String[count]; 

       for(int i=0;i<=count;i++) 
 {
     for(int j=0;j<=col;j++)
     {
 no[i]=jTable1.getValueAt(i,j).toString(); // it get value from 0 row and 0 column
it[i]=jTable1.getValueAt(i,j).toString();
 amount[i]=jTable1.getValueAt(i,j).toString();
 p[i]=jTable1.getValueAt(i,j).toString();
 cost[i]=jTable1.getValueAt(i,j).toString();
s[i]=jTable1.getValueAt(i,j).toString();


        try{

            String sql="INSERT INTO m (no,it,amount,cost,sell,p,date) VALUES('"+no[i]+"','"+it[i]+"','"+amount[i]+"','"+p[i]+"','"+cost[i]+"','"+s[i]+"','"+Date.getText()+"')";
          //  stmt.execute(sql);
            stmt.executeQuery(sql);
            //stmt.execute(sql);
            /*PreparedStatement ps=conn.prepareStatement(sql);
            ps.setString(1, "");
            ps.setString(2, "");
            ps.setString(3, "");
            ps.setString(4, "");
            ps.setString(5, "");
            ps.setString(6, "");
            ps.setString(7,Date.getText());
            ps.execute();*/

             JOptionPane.showMessageDialog(null,"saved");
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null,e);
        }
 }
    }                                    

    private void jTable1InputMethodTextChanged(java.awt.event.InputMethodEvent evt) {                                               
        // TODO add your handling code here:
    }                                              

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(bill.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(bill.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(bill.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(bill.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

         Connection conn = null;

   Statement stmt = null;
   try{
      //STEP 2: Register JDBC driver
      Class.forName("com.mysql.jdbc.Driver");
       //Class.forName("jdbc:mysql://localhost:3306/ganpatidb?zeroDateTimeBehavior=convertToNull");

      //STEP 3: Open a connection
      System.out.println("Connecting to database...");
      conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ganpatidb", "root", "root");

      //STEP 4: Execute a query
      System.out.println("Creating database...");
      stmt = conn.createStatement();


   }catch(SQLException se){
      //Handle errors for JDBC
     // se.printStackTrace();
   }catch(Exception e){
      //Handle errors for Class.forName
     // e.printStackTrace();
   }finally{
      //finally block used to close resources
      try{
         if(stmt!=null)
            stmt.close();
      }catch(SQLException se2){
      }// nothing we can do
      try{
         if(conn!=null)
            conn.close();
      }catch(SQLException se){
         //se.printStackTrace();
      }//end finally try
   }//end try
   //System.out.println("Goodbye!");

        //int r=jTable1.selectedrow;
        //jTable1.getValue(3,3);
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new bill().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JTextField Date;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton netprofit;
    private javax.swing.JTextField netsum;
    private javax.swing.JButton save;
    // End of variables declaration                   
}

我想知道如何自动减去另一列中显示的两列值?

最佳答案

您将需要使用 AbstractTableModel 或覆盖 DefaultTableModel 的功能

无论哪种情况,(至少)有两种方法可以做到这一点......

你可以...

重写getValueAt方法,当询问指定列的值时,计算返回结果的金额...

public Object getValueAt(int row, int col) {
    Object value = null;
    switch (col) {
        case 4:
            double amount = (Double)getValueAt(row, 2);
            double cost = (Double)getValueAt(row, 3);
            value = cost - amount;
            break;
    }
    return value;
}

我在这里做出假设来证明这一点。如果您使用的是 DefaultTableModel,则只需调用 super.getValueAt(row, col) 即可自动获取其他单元格的值...

public Object getValueAt(int row, int col) {
    Object value = null;
    switch (col) {
        case 4:
            double amount = (Double)getValueAt(row, 2);
            double cost = (Double)getValueAt(row, 3);
            value = cost - amount;
            break;
        default:
            value = super.getValueAt(row, col);
    }
    return value;
}

如果数据不可编辑(大部分是静态的)并且行数据已完成,这真的很好,否则......

你可以...

重写setValueAt方法来相应修改利润值...

public void setValueAt(int row, int col, Object value) {
    if (col != 4) {
        super.setValueAt(row, col, value);
        if (col == 2 || col == 3) {
            double amount = (Double)getValueAt(row, 2);
            double cost = (Double)getValueAt(row, 3);
            setValueAt(row, 4, amount - cost);
        }
    }
}

如果数据不是静态的并且用户将其直接输入到表中,则效果会更好...

免责声明

为了简单起见,我对数据类型进行了假设,您可能需要将数据转换为适当的格式以进行计算

更新了简单的示例

DefaultTableModel model = new DefaultTableModel(
        new String[]{"JNo", "Item", "Amount", "cost", "Profit", "P"}, 0) {
    @Override
    public Object getValueAt(int row, int col) {
        Object value = null;
        switch (col) {
            case 4:
                double amount = (Double) getValueAt(row, 2);
                double cost = (Double) getValueAt(row, 3);
                value = cost - amount;
                break;
            default:
                value = super.getValueAt(row, col);
        }
        return value;
    }
};

概念证明

这只是一个概念证明,请勿将其复制并粘贴到您的代码中并期望它神奇地工作,您将必须修改它以满足您的需求...

AutoTable

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class AutoSumming {

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

    public AutoSumming() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                DefaultTableModel model = new DefaultTableModel(
                                new String[]{"Amount", "Cost", "Profit"},
                                10) {
                                    @Override
                                    public Object getValueAt(int row, int col) {
                                        Object value = null;
                                        switch (col) {
                                            case 2:
                                                double amount = getAmountForRow(row);
                                                double cost = getCostForRow(row);
                                                value = cost - amount;
                                                break;
                                            default:
                                                value = super.getValueAt(row, col);
                                        }
                                        return value;
                                    }

                                    protected double getAmountForRow(int row) {
                                        return getNumberForRow(row, 0);
                                    }

                                    protected double getCostForRow(int row) {
                                        return getNumberForRow(row, 1);
                                    }

                                    protected double getNumberForRow(int row, int col) {
                                        double number = 0;
                                        Object value = getValueAt(row, col);
                                        if (value != null && value instanceof Number) {
                                            number = ((Number) value).doubleValue();
                                        }
                                        return number;
                                    }

                                    @Override
                                    public void setValueAt(Object aValue, int row, int column) {
                                        if (column != 2) {
                                            if (aValue instanceof Number) {
                                                super.setValueAt(aValue, row, column);
                                                if (column == 0 || column == 1) {
                                                    fireTableCellUpdated(row, 2);
                                                }
                                            }
                                        }
                                    }

                                    @Override
                                    public Class<?> getColumnClass(int columnIndex) {
                                        return Double.class;
                                    }

                                    @Override
                                    public boolean isCellEditable(int row, int column) {
                                        return column != 2;
                                    }

                                };

                JTable table = new JTable(model);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

仔细看看How to use tables为了更好地理解这个例子是如何工作的

关于java - 如何在java中减去两列值并显示在另一列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24299984/

相关文章:

java - 解析 "no results found for query (code 101)"。无法获取对象 ID

java - 访问动态创建的控件 Java Swing 的值

java - 线程中的异常 "AWT-EventQueue-0"java.lang.ArrayIndexOutOfBoundsException : -1

java - 在java中旋转图像

java - 如何在方法期间而不是之后进行按钮更改重绘?

java - 如何从jtable中删除一行

java - 如何为 JTable 中的单个列提供多个排序选项?

java - 是否有准备好使用 Java lifo 类(堆栈)并在重新插入时推到前面?

Java AWT 或 Swing 用于 GUI 构建?

java - spring框架内置html/css/javascript输出编码?