aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTable Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable Problem" Watch "JTable Problem" New topic
Author

JTable Problem

Ranganath Junpal
Ranch Hand

Joined: Aug 31, 2006
Posts: 76
Hi ,

I am developing a simple JTable using the AbstractTableModel. I have coded the AbstractTableModel as follows:-

_++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import javax.swing.table.AbstractTableModel;


public class mytabmod extends AbstractTableModel{

static final long serialVersionUID = 1234567890L;

public final String[] columnNames={"Name","SecondName","Age"};

public Object[][] data= {
{"a", "b","c"},
{"dee", "eee","ffff", },
{"ggg", "Wwww","Kdd", }
};

public mytabmod()
{

}

public mytabmod(String col[])
{
//this.colNames=col;
}

public Object getValueAt(int a,int b)
{
return data[a][b];
}

public void setValueAt(Object o,int row, int col)
{
fireTableCellUpdated(row, col);
}

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

public boolean isCellEditable(int Row,int Col)
{
return false;
}

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

public int getRowCount()
{
return data.length;
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
when i use this model for my JTable as follows:-

public class Table{

public static void main(String args[])
{

//MyTab mt=new MyTab();
JTable mt=new JTable();
mt.setModel(new mytabmod());
JFrame jf=new JFrame("JTable Extended Demo!");
jf.setLayout(new BorderLayout());
jf.add(mt,BorderLayout.CENTER);
jf.setVisible(true);
jf.setSize(500,200);
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
The program executes with out error but the problem is that i am not able to see the columnHeaders ie Name,Sname,Age int he JTable. But i am able to get the values of the data[][] array.

Please let me know where have i gone wrong.
And secondly i want to embed the JprogressBar with one of the columns. Pleas e let know how to go about it.


Thank you
Regards
Kai Witte
Ranch Hand

Joined: Jul 17, 2004
Posts: 356
hello,

use a JScrollPane for the JTable to solve your current problem.

Also use an object oriented approach: Create a Person interface with methods getName, getSecondName, getAge, and use methods like addPerson and getPerson to modify the table data. You could also make your class implement List<Person>.

Kai


Kai Witte's business website Kai Witte's private homepage
Ranganath Junpal
Ranch Hand

Joined: Aug 31, 2006
Posts: 76
Hello I tried addeing the JScrollpane to JTable still the problem exists. I am not able to see the Column Headers...

Regards
Naga Malleshwararao
Greenhorn

Joined: Oct 22, 2006
Posts: 4
Try to subclass DefaultTableModel.Use the following Code

import java.util.Vector;

import javax.swing.table.DefaultTableModel;

// Customizing the DefaultTableModel
class ResourceTableModel extends DefaultTableModel
{
public boolean isCellEditable(int row,int column)
{
return true;
}

public Class getColumnClass(int column)
{
Vector v = (Vector)dataVector.elementAt(0);
return v.elementAt(column).getClass();
}
}

In class where you are using JTable write this code

JTable resourceAllocation;
ResourceTableModel resourceModel = new ResourceTableModel();
resourceAllocation = new JTable(resourceModel);

String[] columns = new String[]{"Resource Name","Start Date","End Date","Allocation"};

for(int i = 0; i < columns.length; i++ )
{
resourceModel.addColumn(columns[i]);
}
Ranganath Junpal
Ranch Hand

Joined: Aug 31, 2006
Posts: 76
hey thanks man! I hope this works! BTW whats wrong if i subclass the AbstractTableModel? Just wanted to know ..


Thanks a lot
Regards
Sam Codean
Ranch Hand

Joined: Feb 26, 2006
Posts: 194
the AbstractTableModel does some hideous work behind the scene. i too have faced some issues when extending it. Dig in more and you will find. How about going through the source code of AbstractTableModel if you are a curious bug.
Else behappy with extending the DefaultTableModel which will anyways sufice for most of your tasks


-Sam Codean<br />SCJP 1.4 (98%)<br />SCJD 5.0 (87.5%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable Problem