Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with TableModel and DefaultTableColumnModel

 
Alex Florentino
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 908
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Brian Cole
Author
Ranch Hand
Posts: 908
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic