Results 1 to 3 of 3
- 02-01-2010, 06:15 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 68
- Rep Power
- 0
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
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)Java Code:package xxTestxx; import java.awt.*; import javax.swing.*; @SuppressWarnings("serial") public class InventoryTEST2 extends JFrame { private JTable table; private JPanel lowerPanel; private JScrollPane scroll; public InventoryTEST2() { initializeInventory(); } 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); lowerPanel.add(scroll); getContentPane().add(lowerPanel); setDefaultCloseOperation(EXIT_ON_CLOSE); setTitle("Inventory Window"); setSize(1400, 400); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new InventoryTEST2(); } }); } }
Example:
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.. :oLast edited by bigj; 02-01-2010 at 06:17 AM.
- 02-01-2010, 03:49 PM #2
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)
This may need some tweaking to ensure that the aValue parameter can be cast to Number.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);
dbLast edited by DarrylBurke; 02-01-2010 at 03:54 PM. Reason: Missed (at least) a couple of lines of code
- 02-01-2010, 03:58 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 68
- Rep Power
- 0
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,Setting values is a job for the model
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
-
How to create excel sheet?
By kishan in forum Advanced JavaReplies: 3Last Post: 07-13-2010, 01:15 PM -
Help for creating excel book sheet
By anilkumar_vist in forum New To JavaReplies: 3Last Post: 01-24-2010, 10:22 PM -
How To Add More Rows To Excel Sheet In Java
By JMartins in forum New To JavaReplies: 0Last Post: 12-30-2009, 11:30 PM -
JExcel: Copy a cell content from one sheet to another sheet in the same workbook.
By ukbasak in forum Advanced JavaReplies: 0Last Post: 08-02-2007, 12:31 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks