Jump to content




Recent Status Updates

  • Photo
      30 Sep
    rhossis

    laptop hard disk seated beneath motherboard but with no access panel. 7 hours to replace :(

    Show comments (3)
  • Photo
      19 Sep
    Chall

    I love it when you go to write a help thread, then while writing, you reach an enlightenment, and figure it out yourself.

    Show comments (3)
View All Updates

Developed by TechBiz Xccelerator
Photo
- - - - -

JTable + AbstractTableModel


  • Please log in to reply
2 replies to this topic

#1 hushkin

hushkin

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 24 March 2012 - 06:10 AM

Hi

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;
      }
	@Override
	  public int getColumnCount() {
          return columnNames.length;
      }
	@Override
      public int getRowCount() {
          return data.length;
      }
	@Override
      public String getColumnName(int col) {
          return columnNames[col];
      }
	@Override
      public Object getValueAt(int row, int col) {
          return data[row][col];
      }
	@Override
      public Class getColumnClass(int c) {
          return getValueAt(0, c).getClass();
      }
	@Override
      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();
 this.add(tbl1);
 tbl1.setModel(dc.getDataFromSelectedTable(listTableNames.getSelectedValue().toString()));
 tbl1.setBounds(12, 182, 609, 217);
 tbl1.setEnabled(true);

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

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 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

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




Powered by binpress