• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 455
  • Last Modified:

Java: how to add values to my TableModel?

Hello Experts,

How can I add string values to my TableModel?

public class Test extends JPanel {

	private static final long serialVersionUID = 1L;

	public Test() {
		super(new GridLayout(1, 0));
		MyTableModel model = new MyTableModel();
		model.addValue("name1");
		JTable table = new JTable(model);
		table.setPreferredScrollableViewportSize(new Dimension(500, 70));
		table.setFillsViewportHeight(true);

		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane);
	}

	private static void createAndShowGUI() {
		// Create and set up the window.
		JFrame frame = new JFrame("TableDemo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Create and set up the content pane.
		Test newContentPane = new Test();
		newContentPane.setOpaque(true); // content panes must be opaque
		frame.setContentPane(newContentPane);

		// Display the window.
		frame.pack();
		frame.setVisible(true);
	}

	class MyTableModel extends AbstractTableModel {

		private static final long serialVersionUID = 1L;
		String newValue;
		private String[] columnNames = { "First Name" };//only one col
		private Object[][] data;//many rows to be set
		
		public MyTableModel() {

 		}
		
		public void addValue(String string){
			this.newValue = string;
			Class obj = null;
			try {
				obj = Class.forName(string);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			
			setValueAt(obj, getRowCount()+1, getColumnCount());
		}

		public int getColumnCount() {
			return columnNames.length;
		}

		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;
			fireTableCellUpdated(row, col);
		}

	}

	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		});

	}// END OF MAIN

}// END OF CLASS

Open in new window


In advance, thank you for your help.
0
epifanio67
Asked:
epifanio67
  • 3
  • 2
1 Solution
 
CEHJCommented:
You need to add values to your array. It'd be a lot more convenient to use List<List<Object>> actually
0
 
epifanio67Author Commented:
Thanks for your help CEHJ,

Still get the nullPointException though...

public class Test extends JPanel {

	private static final long serialVersionUID = 1L;

	public Test() {
		super(new GridLayout(1, 0));
		MyTableModel model = new MyTableModel();
		model.addValue("name1");
		JTable table = new JTable(model);
		table.setPreferredScrollableViewportSize(new Dimension(500, 70));
		table.setFillsViewportHeight(true);

		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane);
	}

	private static void createAndShowGUI() {
		// Create and set up the window.
		JFrame frame = new JFrame("TableDemo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Create and set up the content pane.
		Test newContentPane = new Test();
		newContentPane.setOpaque(true); // content panes must be opaque
		frame.setContentPane(newContentPane);

		// Display the window.
		frame.pack();
		frame.setVisible(true);
	}

	class MyTableModel extends AbstractTableModel {

		private static final long serialVersionUID = 1L;
		ArrayList<Object> list = new ArrayList<Object>();
		String newValue;
		private String[] columnNames = { "First Name" };//only one col
		private Object[][] data;//many rows to be set
		
		public MyTableModel() {

 		}
		
		public void addValue(String string){
			list.add(string);
			for (int x = 0; x < list.size(); x++) {
				setValueAt(list.get(x), getRowCount()+1, getColumnCount());
			}			
		}

		public int getColumnCount() {
			return columnNames.length;
		}

		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;
			fireTableCellUpdated(row, col);
		}

	}

	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		});

	}// END OF MAIN

}// END OF CLASS

Open in new window


thanks again...
0
 
CEHJCommented:
import java.util.*;
import javax.swing.table.*;
import java.awt.GridLayout;
import java.awt.Dimension;
import javax.swing.*;

public class Test extends JPanel {
    private static final long serialVersionUID = 1L;

    public Test() {
        super(new GridLayout(1, 0));

        MyTableModel model = new MyTableModel();
        for(String name : new String[] { "Alan", "Beth", "Charles" }) {
	    model.addValue(name);
	}

        JTable table = new JTable(model);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);

        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane);
    }

    private static void createAndShowGUI() {
        // Create and set up the window.
        JFrame frame = new JFrame("TableDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create and set up the content pane.
        Test newContentPane = new Test();
        newContentPane.setOpaque(true); // content panes must be opaque
        frame.setContentPane(newContentPane);

        // Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
    } // END OF MAIN

    class MyTableModel extends AbstractTableModel {
        private static final long serialVersionUID = 1L;
        List<List<Object>> data = new ArrayList<List<Object>>();
        String newValue;
        private String[] columnNames = { "First Name" }; //only one col

        public MyTableModel() {
	    data = new ArrayList<List<Object>>();
	}

	public void addValue(String string) {
	    List<Object> newRow = new ArrayList<Object>();
	    newRow.add(string);
	    data.add(newRow);
	}

	public int getColumnCount() {
	    return columnNames.length;
	}

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

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

	public Object getValueAt(int row, int col) {
	    return data.get(row).get(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.get(row).set(col, value);
	    fireTableCellUpdated(row, col);
	}
    }
} // END OF CLASS

Open in new window

is the sort of thing i meant
0
 
epifanio67Author Commented:
Thank you CEHJ....

thank you so much for your help...
0
 
CEHJCommented:
:) Of course, if you have only one column in reality, you don't need to nest List
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now