Latif
Latif

Reputation: 125

Calculate sum of the values in columns and rows in JTable

enter image description here

I am making a program in Java and need some help from you guys. The questions are:

  1. If I press the button (Tüm 0) I want the program to add up the values in the rist row and display it in the cell which I marked with ?????.

    If I press the button (Tüm 2) I want the program to add up the values in the second column and display it in the cell which I marked with ????.

  2. Does JTable have the function as excel that sum the values and display them if I select curtain cells in that table? Here is the code:

    package mgm;
    public class thirdFrame extends JFrame{
    
    private JPanel contentPane;
    private JFrame thirdFrame;
    private JTextField tF;
    private JTextField tF1;
    private JTextField tF2;
    private JTable table;
    private JTextField tF3;
    private JTextField tF4;
    private JTextField tF5;
    
    public thirdFrame() {
    thirdFrame = new JFrame();
    thirdFrame.setFont(new Font("David",Font.BOLD,17));                     
    thirdFrame.setTitle("TOSCOW");
    thirdFrame.setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\EXP\\Desktop\\mgm\\src\\MGM-LOGO.png"));
    thirdFrame.setBounds(100, 100, 1600, 620);
    thirdFrame.setVisible(true);
    thirdFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    thirdFrame.getContentPane().setLayout(null);
    JDesktopPane desktop = new JDesktopPane();
    desktop.setOpaque(true);
    thirdFrame.getContentPane().add(desktop);
    
    JInternalFrame internalFrame = new JInternalFrame ("MGMSportif ",false,true,false,true);
    internalFrame.setFrameIcon(new ImageIcon("C:\\Users\\EXP\\Desktop\\mgm\\src\\reized logo.png"));
    internalFrame.setBounds(209, 126, 720, 400);
    
    JToolBar toolBar = new JToolBar();
    toolBar.setBounds(0, 0, 1362, 32);
    thirdFrame.getContentPane().add(toolBar);
    JButton bt = new JButton("");
    bt.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    bt.setToolTipText("Dosya a\u00E7mak i\u00E7in t\u0131klay\u0131n");
    bt.setIcon(new ImageIcon("C:\\Users\\EXP\\Desktop\\mgm\\src\\open-file-icon.png"));
    toolBar.add(bt);
    JButton bt1 = new JButton("");
    bt1.addActionListener(new ActionListener() {
         @Override
    public void actionPerformed(ActionEvent e) {
        Save sF =new Save();
        sF.setVisible(false);
        dispose();                  
                }
            });
    bt1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    bt1.setToolTipText("Kay\u0131d etmek i\u00E7in t\u0131klay\u0131n");
    bt1.setIcon(new ImageIcon("C:\\Users\\EXP\\Desktop\\mgm\\src\\save.png"));
    toolBar.add(bt1);
    JButton bt2 = new JButton("");
    bt2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    bt2.setToolTipText("Kesmek i\u00E7in t\u0131klay\u0131n");
    bt2.setIcon(new ImageIcon       
    ("C:\\Users\\EXP\\Desktop\\mgm\\src\\Cut-icon.png"));
    toolBar.add(bt2);
    JButton bt3 = new JButton("");
    bt3.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));           
    bt3.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    internalFrame.setVisible(true);
        }
         });
    
    thirdFrame.getContentPane().add(internalFrame);
    internalFrame.getContentPane().setLayout(null);
    JLabel lb = new JLabel("Tarihe g\u00F6re ara");
    lb.setFont(new Font("Tahoma", Font.PLAIN, 13));
    lb.setBounds(10, 11, 110, 26);
    internalFrame.getContentPane().add(lb);
    tF = new JTextField();
    tF.setToolTipText("Aramak isteyen tarihi giriniz");
    tF.setBounds(144, 16, 445, 20);
    internalFrame.getContentPane().add(tF);
    tF.setColumns(10);
    JProgressBar pBar = new JProgressBar(0);
    pBar.setValue(0);
    pBar.setString("Aranıyor...."); 
    pBar.setStringPainted(true);
    pBar.setBounds(10, 67, 684, 21);
    internalFrame.getContentPane().add( pBar);
    JButton sesarchButton = new JButton("");
     sesarchButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    sesarchButton.addActionListener(new ActionListener() {
         @Override
    public void actionPerformed(ActionEvent e) {
    sesarchButton.setEnabled(false);
    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            }
       });      
    sesarchButton.setIcon(new ImageIcon("C:\\Users\\EXP\\Desktop\\mgm\\src\\search48.png"));
    sesarchButton.setBounds(599, 7, 57, 56);
    internalFrame.getContentPane().add(sesarchButton);
    JLabel lb1 = new JLabel("Makineye  g\u00F6re ara");
    lb1.setFont(new Font("Tahoma", Font.PLAIN, 13));
    lb1.setBounds(10, 43, 124, 20);
    internalFrame.getContentPane().add( lb1);
    tF1 = new JTextField();
    tF1 .setBounds(144, 42, 445, 20);
    internalFrame.getContentPane().add(tF1 );
    tF1 .setColumns(10);
    tF1 .setBounds(144, 42, 445, 20);
    JList list = new JList();
    list.setBounds(10, 99, 684, 360);
    internalFrame.getContentPane().add(list);       bt3.setToolTipText("Dosya aramak i\u00E7in t\u0131klay\u0131n");
    bt3.setIcon(new ImageIcon("C:\\Users\\EXP\\Desktop\\mgm\\src\\Zoom-icon.png"));
     toolBar.add( bt3);
    JButton bt4 = new JButton("");
    bt4.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    bt4.setToolTipText("\u00C7\u0131kt\u0131 almak i\u00E7in         t\u0131klay\u0131n");
    bt4.setIcon(new ImageIcon("C:\\Users\\EXP\\Desktop\\mgm\\src\\print-icon16.png"));
    toolBar.add(bt4);
     JButton bt5 = new JButton("");
    bt5.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    bt5.setToolTipText("Pencereyi kapatmak i\u00E7in t\u0131klay\u0131n");
    bt5.setIcon(new ImageIcon("C:\\Users\\EXP\\Desktop\\mgm\\src\\Close.png"));
    toolBar.add(bt5);
    JCheckBox checkBox = new JCheckBox("Enjeksyon Makine No:");
    checkBox.addItemListener(new ItemListener() {
          @Override
      public void itemStateChanged(ItemEvent e) {
    if(e.getStateChange() == ItemEvent.SELECTED){
    tF2.setEnabled(true);
                   }                     
     else if(e.getStateChange() == ItemEvent.DESELECTED){
     tF2.setEnabled(false);
              }
          }              
             });    
          checkBox.setFont(new Font("Tahoma", Font.PLAIN, 11));
    checkBox.setBounds(25, 39, 131, 23);
    thirdFrame.getContentPane().add(checkBox);
    
     tF2 = new JTextField();
     tF2.setBounds(162, 40, 31, 19);
    thirdFrame.getContentPane().add( tF2);
     tF2.setColumns(10);
    
    JCheckBox checkBox1 = new JCheckBox("Kaynak Makine No:");
    checkBox1.addItemListener(new ItemListener() {
         @Override
        public void itemStateChanged(ItemEvent e) {
             if(e.getStateChange() == ItemEvent.SELECTED){
                 tF3.setEnabled(true);
                 tF3.setText("");
             }
             else if(e.getStateChange() == ItemEvent.DESELECTED){
                 tF3.setEnabled(false);
                 tF3.setText("");
             }
    
    
        }
    });
       checkBox1.setFont(new Font("Tahoma", Font.PLAIN, 11));
    checkBox1.setBounds(337, 39, 117, 23);
    thirdFrame.getContentPane().add(checkBox1);
    
    tF3 = new JTextField();
    
    tF3.setBounds(460, 43, 31, 19);
    thirdFrame.getContentPane().add(tF3);
    tF3.setColumns(10);
    
    JCheckBox checkBox2 = new JCheckBox("Otomatsyon Makine No:");
    checkBox2.addItemListener(new ItemListener() {
         @Override
        public void itemStateChanged(ItemEvent e) {
             if(e.getStateChange() == ItemEvent.SELECTED){
                 tF4.setEnabled(true);
                 tF4.setText("");
             }
             else if(e.getStateChange() == ItemEvent.DESELECTED){
                 tF4.setEnabled(false);
                 tF4.setText("");
             }
    
    
        }
    });
      checkBox2.setFont(new Font("Tahoma", Font.PLAIN, 11));
    checkBox2.setBounds(651, 39, 141, 23);
    thirdFrame.getContentPane().add(checkBox2);
    
    tF4 = new JTextField();
    tF4.setBounds(795, 43, 31, 19);
    thirdFrame.getContentPane().add(tF4);
    tF4.setColumns(10);
    
    JCheckBox checkBox3 = new JCheckBox("Pakitleme:");
    checkBox3.setFont(new Font("Tahoma", Font.PLAIN, 11));
    checkBox3.addItemListener(new ItemListener() {
    
        public void itemStateChanged(ItemEvent e) {
            if(e.getStateChange() == ItemEvent.SELECTED){
                tF5.setEnabled(true);
                tF5.setText("");
            }
            else if(e.getStateChange() == ItemEvent.DESELECTED){
                tF5.setEnabled(false);
                tF5.setText("");
            }
        }
    });
    
    
    
    checkBox3.setBounds(923, 39, 85, 23);
    thirdFrame.getContentPane().add(checkBox3);
    
    tF5 = new JTextField();
    tF5.setBounds(1010, 43, 31, 19);
    thirdFrame.getContentPane().add(tF5);
    tF5.setColumns(10);
    
    JLabel lb2 = new JLabel("                                                 Vardiya 1                                                                                                                                                                                                          Vardiya 2");
    lb2.setForeground(SystemColor.desktop);
    lb2.setFont(new Font("Tahoma", Font.PLAIN, 13));
    lb2.setBounds(0, 73, 1341,21);
    thirdFrame.getContentPane().add(lb2);
    
    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(20, 94, 1324, 455);
    thirdFrame.getContentPane().add(scrollPane);
    
    table = new JTable();
    JTable table = new JTable();
    table.setColumnSelectionAllowed(true);
    table.setAutoCreateRowSorter(true);
    
    
    table.setForeground(Color.BLACK);
    table.setModel(new 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, 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, 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, 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, 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, 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[] {
            "TAR\u0130H", "BEYAZ", "KIRMIZI", "MAV\u0130", "SARI", "S\u0130YAH", "ADET", "PERS.", "\u00DCR.NEDEN\u0130", "Toplam", "TAR\u0130H", "BEYAZ", "KIRMIZI", "MAV\u0130", "SARI", "S\u0130YAH", "ADET", "PERS.", "\u00DCR.NEDEN\u0130", "Toplam", "2VDT"
        }
    ) {
        Class[] columnTypes = new Class[] {
            Integer.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class
        };
        public Class getColumnClass(int columnIndex) {
            return columnTypes[columnIndex];
        }
    });
    scrollPane.setViewportView(table);
    
    
    JSeparator separator = new JSeparator();
    separator.setBounds(0, 69, 1362, 2);
    thirdFrame.getContentPane().add(separator);
    
    JButton bt6 = new JButton("T\u00FCm 0");
    bt6.setFont(new Font("Tahoma", Font.PLAIN, 8));
    bt6.setBounds(579, 69, 61, 23);
    thirdFrame.getContentPane().add(bt6);
    
    JButton bt7 = new JButton("T\u00FCm 1");
    bt7.setFont(new Font("Tahoma", Font.PLAIN, 8));
    bt7.addActionListener(new ActionListener() {
    
    
        public void actionPerformed(ActionEvent e) {
    
    
        }
    });
    
    bt7.setBounds(1201, 69, 61, 23);
    thirdFrame.getContentPane().add(bt7);
    
    JButton bt8 = new JButton("T\u00FCm 2");
    bt8.setFont(new Font("Tahoma", Font.PLAIN, 8));
    bt8.addActionListener(new ActionListener() {
    
       public void actionPerformed(ActionEvent e) {
    
    
        }
    });
    
    bt8.setBounds(82, 551, 61, 23);
    thirdFrame.getContentPane().add(bt8);
    
    JButton bt9 = new JButton("T\u00FCm 3");
    bt9.setFont(new Font("Tahoma", Font.PLAIN, 8));
    bt9.setBounds(145, 551, 61, 23);
    thirdFrame.getContentPane().add(bt9);
    
    JButton bt10 = new JButton("T\u00FCm 4");
    bt10.setFont(new Font("Tahoma", Font.PLAIN, 8));
    bt10.setBounds(207, 551, 61, 23);
    thirdFrame.getContentPane().add(bt10);
    
    JButton bt11 = new JButton("T\u00FCm 5");
    bt11.setFont(new Font("Tahoma", Font.PLAIN, 8));
    bt11.setBounds(270, 551, 61, 23);
    thirdFrame.getContentPane().add(bt11);
    
    JButton bt12 = new JButton("T\u00FCm 6");
     bt12.setFont(new Font("Tahoma", Font.PLAIN, 8));
     bt12.setBounds(332, 551, 61, 23);
    thirdFrame.getContentPane().add( bt12);
    
    JButton bt13 = new JButton("T\u00FCm 7");
    bt13.setFont(new Font("Tahoma", Font.PLAIN, 8));
    bt13.setBounds(395, 551, 61, 23);
    thirdFrame.getContentPane().add(bt13);
    
    JButton b14 = new JButton("T\u00FCm 8");
    b14.setFont(new Font("Tahoma", Font.PLAIN, 8));
    b14.setBounds(703, 551, 61, 23);
    thirdFrame.getContentPane().add(b14);
    
    JButton bt15 = new JButton("T\u00FCm 9");
    bt15.setFont(new Font("Tahoma", Font.PLAIN, 8));
    bt15.setBounds(764, 551, 61, 23);
    thirdFrame.getContentPane().add(bt15);
    
    JButton bt16 = new JButton("T\u00FCm 10");
    bt16.setFont(new Font("Tahoma", Font.PLAIN, 8));
    bt16.setBounds(826, 551, 61, 23);
    thirdFrame.getContentPane().add(bt16);
    
    JButton bt17 = new JButton("T\u00FCm 11");
    bt17.setFont(new Font("Tahoma", Font.PLAIN, 8));
    bt17.setBounds(889, 551, 61, 23);
    thirdFrame.getContentPane().add(bt17);
    
    JButton bt18 = new JButton("T\u00FCm 12");
     bt18.setFont(new Font("Tahoma", Font.PLAIN, 8));
     bt18.setBounds(952, 551, 61, 23);
    thirdFrame.getContentPane().add( bt18);
    
    JButton bt19 = new JButton("T\u00FCm 13");
     bt19.setFont(new Font("Tahoma", Font.PLAIN, 8));
     bt19.setBounds(1015, 551, 61, 23);
    thirdFrame.getContentPane().add( bt19);
    
    
    
    }
     }              
    

Upvotes: 0

Views: 8451

Answers (2)

MatheM
MatheM

Reputation: 811

To answer your second question, no JTable doesn't have sum functions like excel. You will need to implement those yourself. To sum values in a row you can use something like this.

public int sumRow(TableModel mdl, int row) {
    int total = 0;
    // iterate over all columns
    for (int i = 0 ; i < mdl.getColumnCount() ; i++) {
        // null or not Integer will throw exception
        total += (Integer) mdl.getValueAt(row, i);
    }
    return total;
}

for column it is similar you just change row index and keep the column index constant. This function assumes that your table columns are of type Integer and that values are not null. You will have to put some null checking and parsing in there if that is not the case.

To look for cell with specific value you have to iterate over rows and columns. Similar to this

public int[] findValue(TableModel mdl, Object value) {
    int[] returnVal = {-1, -1}; // default returnVal if value is not found
        // iterate over rows
        for(int row = 0 ; row < mdl.getRowCount() ; row++){
            // iterate over columns
            for(int col = 0 ; col < mdl.getColumnCount() ; col++) {
                // if table cell is null it is ignored
                if (mdl.getValueAt(row, col) != null && mdl.getValueAt(row, col).equals(value)) {
                    returnVal[0] = row;
                    returnVal[1] = col;
                    return returnVal;
                }
            }
        }
    return returnVal;
}

To get/set value of cell see javadoc for functions to get and set table model values.

Upvotes: 0

trashgod
trashgod

Reputation: 205775

There is no built-in sum function, but you can write your own. Override the getValueAt() method in your (anonymous) TableModel as shown here. In the fragment below, sum(col) should call super.getValueAt() to add the values found in a particular col. I'm not sure a row of buttons is required.

@Override
public Object getValueAt(int row, int col) {
    if (row == DEPENDENT_ROW) {
        return sum(col);
    } else {
        return super.getValueAt(row, col);
    }
}

Upvotes: 3

Related Questions