Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

add dynamic row to JTable AbstractTableModel with data columns

843807Jan 18 2010 — edited Jan 21 2010
Hi, what I am trying to accomplish here is the following:

1) Have an applet that receives data through javascript and builds the table based on the input data
2) Have all the data sorting according to the column "data type"
3) The data has to be dynamic. Since the table will receives hundreds of thousand rows, I need the table to have an "addRow" method that will add a new row to the bottom, with it's columns matching the data type

Now, what I've accomplished so far:

1) On my first try I used the DefaultTableModel and it worked just great for everything, but then all the data is considered as strings, therefore the sorting with numbers, dates, booleans... wont work. After reading more documentation I found out that the only way to organize the sorting is to implement my own table model (abstract)
2) I gave up on the DefaultTableModel and moved on to the AbstractTableModel, downloaded an example about it and now I don't know how to use an expandable data model; Maybe using vectors or arraylists, but I don't know how to interface them with the table model
3) For static purposes it's a great example, but how do I make it dynamic? How to have an "addRow" that expects an array of objects, each with it's own data type? I've read a great deal of documentation about it, but I couldn't find a single example of this kind
4) I figured out how to call a method through javascript and how to continue to call it at runtime, so as long as the method works (addRow), it should be fine


Here is the static code I have:
import java.awt.*;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Component;
import java.awt.event.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Vector;
import java.util.*;
import javax.swing.*;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.DefaultTableCellRenderer;
import java.lang.String;
    
public class java_ProtoGrid2 extends JApplet 
{
	public MyTableModel model = new MyTableModel();
    private JTextArea txt = new JTextArea(1,20);
    
    public java_ProtoGrid2() {
        JTable table = new JTable(model);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);
        table.setAutoCreateRowSorter(true);
		
		Container cp = getContentPane();
        cp.add(new JScrollPane(table));
        cp.add(BorderLayout.SOUTH, txt);
    }
    
	class MyClass
	{
		public String MyName="";   
	}

    class MyTableModel extends AbstractTableModel {
        private String[] columnNames = {"First Name",
                                        "Last Name",
                                        "Sport",
                                        "# of Years",
                                        "Vegetarian"};
        Object[][] data = {
            {"Mary", "Campione",
             "Snowboarding", new Integer(5), new Boolean(false)},
            {"Alison", "Huml",
             "Rowing", new Integer(3), new Boolean(true)},
            {"Kathy", "Walrath",
             "Knitting", new Integer(2), new Boolean(false)},
            {"Sharon", "Zakhour",
             "Speed reading", new Integer(20), new Boolean(true)},
            {"Philip", "Milne",
             "Pool", new Integer(10), new Boolean(false)},
        };

        public int getColumnCount() {
            return columnNames.length;
        }
        
        public void addRow(Object[] row) 
        { 
			//DONT KNOW WHAT TO DO HERE
			this.fireTableDataChanged();   
        }  

        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        public boolean isCellEditable(int row, int col) {
            if (col < 2) {
                return false;
            } else {
                return true;
            }
        }
        
        public void setValueAt(Object value, int row, int col) {
            
            data[row][col] = value;
        }
    }
	
	public void init() 
    {
        
    }

    public static void main(String[] args) 
    {
        run(new java_ProtoGrid2(), 650, 600);
    }

    public static void run(JApplet applet, int width, int height) 
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(applet);
        frame.setSize(width, height);
        applet.init();
        applet.start();
        frame.setVisible(true);
    }
}
Thanks in advance for all your help.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 18 2010
Added on Jan 18 2010
14 comments
5,405 views