Forums Register Login

JTable Problem

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Hello I tried addeing the JScrollpane to JTable still the problem exists. I am not able to see the Column Headers...

Regards
+Pie Number of slices to send: Send
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]);
}
+Pie Number of slices to send: Send
hey thanks man! I hope this works! BTW whats wrong if i subclass the AbstractTableModel? Just wanted to know ..


Thanks a lot
Regards
+Pie Number of slices to send: Send
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
Do you pee on your compost? Does this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 782 times.
Similar Threads
Refresh rows of a JTable
JTable
JCheckBox in JTable
Swing--JTable in JInternalFrame
jtable
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:57:59.