0

enter image description here

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

I want the total of Total Amt. column to appear on the cell (5,4). I tried some code, which fails, the code which shows the image attached is added below, please. I am stuck at this table thing from past 5 hrs. Thanks in advance.

Sanjeev
  • 9,876
  • 2
  • 22
  • 33
Toms
  • 239
  • 1
  • 5
  • 15

1 Answers1

4

A simplified variation of your example that incorporates this approach is shown below. Note several important points:

  • While a JTable is not a spreadsheet, it is possible to update cells dynamically as an aid to understanding. Use a TableModelListener to update related view components.

  • The TableModel should fire events for the least number of cells possible; fireTableDataChanged() is used for expedience on this small, fixed-size model.

  • Always pack() the enclosing Window and make setVisible() last.

  • Swing GUI objects should be constructed and manipulated only on the event dispatch thread.

  • Use common coding conventions.

Code, as tested:

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();
            }
        });
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    It works perfect, as i wanted. This is my first java project, ill definitely try to implement your suggestions in future. Thanks for the help and understanding. :) – Toms Mar 03 '14 at 09:39
  • 1
    +1 [but something is virulent there:-) ... fireTableDataChanged();](http://stackoverflow.com/a/22134415/714968) – mKorbel Mar 03 '14 at 11:29
  • @mKorbel is correct; a `TableModel` should remain internally consistent when `setValueAt()` is invoked. – trashgod Mar 03 '14 at 13:15