Add new lines to the JTable

2004-02-04 Thread Marcos . Rebelo
What is the better way to add lines to a JTable. I have my TableModal and I
add one line to the Model, know I need the line to apear in the interface.
The only way that I get it to work is with the line
'scrollPane.getViewport().add(table);' (I tryed allot of things before I get
here). 

What is the correct way to do it?
This is a simple code showing my problem.




import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;

class MyTableModel extends AbstractTableModel {
int nRows = 5;
public int getRowCount() {
return nRows;
}

public int getColumnCount() {
return 4;
}

public Object getValueAt(int arg0, int arg1) {
return new Integer(arg0*arg1);
}

void IncrementRow() {
nRows++;
}
}

public class Main {
static JFrame m_frame;
public static void main(String[] ARGS) {
m_frame = new JFrame(Teste);
m_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
m_frame.getContentPane().setLayout(new BorderLayout());

final JTable table = new JTable();

final MyTableModel myTableModel = new MyTableModel(); 
table.setModel(myTableModel);

final JScrollPane scrollPane = new JScrollPane(table);

m_frame.getContentPane().add(scrollPane, BorderLayout.SOUTH);

JButton bt = new JButton(XXX);
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
myTableModel.IncrementRow();
scrollPane.getViewport().add(table);
}
});

m_frame.getContentPane().add(bt, BorderLayout.NORTH);

m_frame.pack();
m_frame.setVisible(true);
}
}
___
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing


RE: Add new lines to the JTable

2004-02-04 Thread Marcos . Rebelo
Thanks for this answer, I look in the table and I never remember to go to
the TableModel.
 
Onether question. Know I have the Table with the cells editable. 
If I chose another column the setValueAt is called.
If I press the button the setValueAt is not called.
If I go to the TextArea the setValueAt is not called. 
if I close the Frame the setValueAt is not called.
This isn't suposed? 
 
Simple code. I'm seeing just the output.
 
import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
 
import javax.swing.*; 
import javax.swing.table.AbstractTableModel;
 
class MyTableModel extends AbstractTableModel { 
int nRows = 5; 
public int getRowCount() {
return nRows; 
} 
 
public int getColumnCount() {
return 4; 
} 

public Object getValueAt(int arg0, int arg1) {
return new Integer(arg0*arg1); 
} 

void IncrementRow() {
System.out.println(IncrementRow());
nRows++; 
} 

public boolean isCellEditable(int nRow, int nCol) {
System.out.println(isCellEditable(int nRow, int nCol));
return true;
}
 
public void setValueAt(Object arg0, int arg1, int arg2) {
System.out.println(setValueAt(Object arg0, int arg1, int arg2));
}
} 
 
public class Main { 
static JFrame m_frame; 
public static void main(String[] ARGS) {
m_frame = new JFrame(Teste); 
m_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
m_frame.getContentPane().setLayout(new BorderLayout()); 

final JTable table = new JTable(); 

final MyTableModel myTableModel = new MyTableModel(); 
table.setModel(myTableModel); 
 
final JScrollPane scrollPane = new JScrollPane(table); 

m_frame.getContentPane().add(scrollPane, BorderLayout.CENTER); 
 
JButton bt = new JButton(XXX); 
bt.addActionListener(new ActionListener(){ 
public void actionPerformed(ActionEvent arg0) { 
myTableModel.IncrementRow(); 
myTableModel.fireTableDataChanged();
} 
}); 
 
m_frame.getContentPane().add(bt, BorderLayout.NORTH);

m_frame.getContentPane().add(new JTextField(), BorderLayout.SOUTH); 

m_frame.pack(); 
m_frame.setVisible(true); 
} 
} 
 
___
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing