Results 1 to 3 of 3
  1. #1
    bigj is offline Member
    Join Date
    Dec 2009
    Rep Power

    Default automatic cell computation in a JTable like an excel spread sheet

    I dont know what should be the right title for this topic, but this is the only thing that comes up in my mind, well heres the scenario

    Java Code:
    package xxTestxx;
    import java.awt.*;
    import javax.swing.*;
    public class InventoryTEST2 extends JFrame {
      private JTable table;
      private JPanel lowerPanel;
      private JScrollPane scroll;
      public InventoryTEST2() {
      private void initializeInventory() {
          lowerPanel = new JPanel();
          lowerPanel.setLayout(new BorderLayout(1,2));
          final String[] columnNames = {"Number1", "Number2", "Sum"};
          final Object[][] data = new Object[200][3];
          table = new JTable(data, columnNames);
          scroll = new JScrollPane(table);
          scroll.setBounds(0, 0, 900, 400);
          setTitle("Inventory Window");
          setSize(1400, 400);
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            new InventoryTEST2();
    I want to make my table act like an excel spread sheet, (but not perfectly as it is) , what I want is to make a cell that will generate a value FROM entering a value on the previous cell, (something automatic)

    as you can see in the JTable there are 3 columns , 1 for the first number
    1 for the second number, what I want is, if I enter the first and second number the third column will generate the answer(the value(the sum)),
    the problem is I'm confuse(getting frustrated) on where should I start to deal with this and I don't even know what action(event) should I implement to make this possible.

    I hope you understand this concern, this is very complicated with me but I have to finish this as soon as possible, please guide me and give me some advice I tried to search at google but still i don't see any usefull resources,
    especially this one JTable: auto edit cells and ENTER=next cell : jtable, cell

    T_T, I think this one could give me the best answer for my question, but darn it. I need to pay. I don't have money... So I desperately need your answers and advice here, ANY HELP would be so much appreciated.. Thank you.. :o
    Last edited by bigj; 02-01-2010 at 07:17 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power


    Setting values is a job for the model. I suggest you construct your JTable with a subclassed DefaultTableModel which overrides setValueAt to compute and set the value in the third column whenever the value in either the first or second column changes. And don't forget to make the third column uneditable. Something like (untested, not even compiled, may have typos)
    Java Code:
    TableModel model = new DefaultTableModel(data, columnNames) {
       public void setValueAt(Object aValue, int row, int column) {
          if (column < 2) {
             double value = ((Number) aValue).doubleValue();
             double otherValue = ((Number) getValueAt(row, (column + 1) % 2)).doubleValue();
             super.setValueAt(value + otherValue, row, 2);
          super.setValueAt(aValue, row, column);
       public boolean isCellEditable(int row, int column) {
          return column != 2;
    table = new JTable(model);
    This may need some tweaking to ensure that the aValue parameter can be cast to Number.

    Last edited by DarrylBurke; 02-01-2010 at 04:54 PM. Reason: Missed (at least) a couple of lines of code

  3. #3
    bigj is offline Member
    Join Date
    Dec 2009
    Rep Power


    Setting values is a job for the model
    THANK YOU SO MUCH! SIR! :) actually this is the first time that I've use one of the complicated swings in java(JTable), atleast now I now where to start,

    particularly in manipulating values in a table, thank you so much. I can proceed now in my next step in my program, Ill just post any further questions in this topic just in case, anyway sir, regarding with the JTable , thats only my biggest problem , manipulating values, theres a lot of resources that I got from google, but I dont know which of them should I implement, thanks for giving this VERY Usefull advice of yours. I dont have any problems yet with key/Mouse events/listeners..

    THank you so much!

Similar Threads

  1. How to create excel sheet?
    By kishan in forum Advanced Java
    Replies: 3
    Last Post: 07-13-2010, 02:15 PM
  2. Help for creating excel book sheet
    By anilkumar_vist in forum New To Java
    Replies: 3
    Last Post: 01-24-2010, 11:22 PM
  3. How To Add More Rows To Excel Sheet In Java
    By JMartins in forum New To Java
    Replies: 0
    Last Post: 12-31-2009, 12:30 AM
  4. Replies: 0
    Last Post: 08-02-2007, 01:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts