• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTable Problem

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranganath Junpal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello I tried addeing the JScrollpane to JTable still the problem exists. I am not able to see the Column Headers...

Regards
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey thanks man! I hope this works! BTW whats wrong if i subclass the AbstractTableModel? Just wanted to know ..


Thanks a lot
Regards
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic