Jump to content

Recent Status Updates

  • Photo
      18 Aug

    When faced with a wall of errors and no hope to fix them, remember the following "Programs always do what you tell them to, and seldom what you want them to, but eventually you'll run out of things that can go wrong and it'll just work. and that's the secret to good programming."

    Show comments (2)
View All Updates

Developed by Kemal Taskin
- - - - -

JTable + AbstractTableModel

  • Please log in to reply
2 replies to this topic

#1 hushkin


    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 24 March 2012 - 06:10 AM


I'm trying to create a JTable with an AbstractTableModel.

I created a Class for the model:

import javax.swing.table.AbstractTableModel;

public class JTableModel extends AbstractTableModel{
	private String[] columnNames;
	private String[][] data ;
      public JTableModel(String[][]data,String[] colNames){
		this.columnNames = colNames;
		this.data = data;
	  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) {
      return true;
      public void setValueAt(String value, int row, int col) {
         data[row][col] = value;
         fireTableCellUpdated(row, col);
         System.out.println("New value of data:" +value);

And I add the model to the JTable in a panel:

public class UC6Panel extends javax.swing.JPanel {
 private DomeinController dc;
 tbl1 = new JTable();
 tbl1.setBounds(12, 182, 609, 217);

The objective is that the model changes if another item is selected from a list (the list is filled with tables of a database).

The Object [][] object and tablemodel are created here:

public class DomeinController {
      Manager manager = new Manager();
	public TableModel getDataFromSelectedTable(String selectedValue) {
		String [][] data = manager.getDataFromSelectedTable(selectedValue);
		jTableModel = new JTableModel(data,manager.getColNamesSelectedTable());

		return jTableModel;

This all works fine, when I select a new item, the JTable refreshes with the new model.

Unfortunately I can't edit any cells. Don't get me wrong, they are editable, I can type in new data but when I switch to another cell the cell changes back to the default data, the data from the database.

I have tried adding a TableModelListener but this changed nothing, maybe I didn't use the Listener correctly?

Probably a couple of lines of code can fix this but I can't seem to figure this out.

Any help would be greatly appreciated!
  • 0

#2 Norm


    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 396 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 24 March 2012 - 09:24 AM

Can you make a small, simple(no database) program that compiles, executes and shows the problem?
  • 0

#3 hushkin


    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 24 March 2012 - 09:27 AM

Thx for the respons, but I just figured the problem out

Instead of using String objects you should use normal Objects.

I knew it was something small & stupid.
  • 0