Two Laptop Bag*
The moose likes Swing / AWT / SWT and the fly likes problem with TableModel and DefaultTableColumnModel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problem with TableModel and DefaultTableColumnModel " Watch "problem with TableModel and DefaultTableColumnModel " New topic
Author

problem with TableModel and DefaultTableColumnModel

Alex Florentino
Ranch Hand

Joined: Dec 12, 2006
Posts: 48
why System.err.println("["+rowIndex+"]["+columnIndex+"]"); print columnIndex always 0 ?

thanks a lot. fellow the code:


import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableColumn;

public class TableTest {

/**
* @param args
*/
public static void main(String[] args) {

JFrame frame = new JFrame("Test JTable");
frame.setLayout(new FlowLayout());
frame.setSize(600, 400);
frame.setLocationRelativeTo(null);

MyTableModel tableModel = new MyTableModel();
MyColumnModel columnModel = new MyColumnModel();

final JTable table = new JTable(tableModel);
table.setColumnModel(columnModel);

JScrollPane scroll = new JScrollPane(table);
scroll.setPreferredSize(new Dimension(200,100));

JButton btn = new JButton("renovar");

frame.add(btn);

btn.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {
//I need change model.
table.setModel(new MyTableModel());
table.setColumnModel(new MyColumnModel());

}});

frame.add(scroll);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);





}





}
class MyColumnModel extends DefaultTableColumnModel {

private static final long serialVersionUID = 1L;

public MyColumnModel() {

TableColumn column1 = new TableColumn();
column1.setHeaderValue("Nome");
TableColumn column2 = new TableColumn();
column2.setHeaderValue("Sobre nome");
TableColumn column3 = new TableColumn();
column3.setHeaderValue("Idade");

addColumn(column1);
addColumn(column2);
addColumn(column3);
}

}

class MyTableModel extends AbstractTableModel {

private static final long serialVersionUID = 1L;

public MyTableModel(){}

public int getRowCount() {
return 5;
}


public Object getValueAt(int rowIndex, int columnIndex) {
System.err.println("["+rowIndex+"]["+columnIndex+"]");
return "Alex";
}

public int getColumnCount() {
return 5;
}

public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}

}
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Your MyColumnModel code is not doing what you expect.
Instead of
you could write

But why bother with a ColumnModel at all? Just override
the getColumnName() method in MyTableModel to
return "Nome"/Sobre nome"/"Idade" and be done with it.


bitguru blog
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Brian Cole:
But why bother with a ColumnModel at all? Just override
the getColumnName() method in MyTableModel to
return "Nome"/Sobre nome"/"Idade" and be done with it.


Well I guess you would also need to change
getColumnCount() to return 3 instead of 5.

But then you can remove both setColumnModel() calls.
Alex Florentino
Ranch Hand

Joined: Dec 12, 2006
Posts: 48
Originally posted by Brian Cole:
Your MyColumnModel code is not doing what you expect.
Instead of
you could write

But why bother with a ColumnModel at all? Just override
the getColumnName() method in MyTableModel to
return "Nome"/Sobre nome"/"Idade" and be done with it.


because my real system... load automatically this infos... table column name and data...

sample my application update my database and this app allowed edit data loaded... and I think if I split code of show column of logic show the data is easy of mantain...

thanks for you answers, and my sorry my english !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with TableModel and DefaultTableColumnModel