93MH1351
asked on
JTable
I have a JTable which I initially populate before I show it. However when I add elements to the model's vector and redraw it does not refresh with the
new rows.
Does anyone have sample code for adding extra rows to a JTable and then
re-displaying? Similar code for a JList would also suffice.
Thanks
Denis
new rows.
Does anyone have sample code for adding extra rows to a JTable and then
re-displaying? Similar code for a JList would also suffice.
Thanks
Denis
Try the following code after adding an element:
myTable.invalidate();
myTable.validate();
myTable.repaint();
or try a repaint of the parent container of your JTable for example:
myScrollPane.invalidate();
myScrollPane.validate(),
myScrollPane.repaint();
myTable.invalidate();
myTable.validate();
myTable.repaint();
or try a repaint of the parent container of your JTable for example:
myScrollPane.invalidate();
myScrollPane.validate(),
myScrollPane.repaint();
look:
http://java.sun.com/docs/books/tutorial/uiswing/components/table.htm
http://manning.spindoczine.com/sbe/files/uts2/Chapter18html/Chapter18.htm
http://users.vnet.net/wwake/swing/faq.html#table
http://www.codeguru.com/java/Swing/JTable/index.shtml
http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/
and very good examples
http://www2.gol.com/users/tame/swing/examples/SwingExamples.html
http://java.sun.com/docs/books/tutorial/uiswing/components/table.htm
http://manning.spindoczine.com/sbe/files/uts2/Chapter18html/Chapter18.htm
http://users.vnet.net/wwake/swing/faq.html#table
http://www.codeguru.com/java/Swing/JTable/index.shtml
http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/
and very good examples
http://www2.gol.com/users/tame/swing/examples/SwingExamples.html
>when I add elements to the model's vector
Do you mean: add elements to the model, or add elements to the vector on which the model is based?
You should add elements to the model. If the model is a DefaultTableModel, it will fire TableModelEvents to notify listeners (including the JTable(s) displaying the model) that they should update -> no repaint required. This is more efficient, because only the added row has to be updated instead of the entire view.
If you're using your own TableModel implementation, you should fire TableModelEvents to all listeners of the model.
In general, Swing components rely on update events from their underlying models, calling repaint is never a good solution.
Do you mean: add elements to the model, or add elements to the vector on which the model is based?
You should add elements to the model. If the model is a DefaultTableModel, it will fire TableModelEvents to notify listeners (including the JTable(s) displaying the model) that they should update -> no repaint required. This is more efficient, because only the added row has to be updated instead of the entire view.
If you're using your own TableModel implementation, you should fire TableModelEvents to all listeners of the model.
In general, Swing components rely on update events from their underlying models, calling repaint is never a good solution.
Sorry vladi21, didn't see your comment.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if you still have problems - post ur code
import javax.swing.*;import java.util.*;
public class SortedListModel extends AbstractListModel { // Define a SortedSet
SortedSet model; public SortedListModel() { // Create a TreeSet
// Store it in SortedSet variable model = new TreeSet(); }
// ListModel methods public int getSize() { // Return the model size
return model.size(); } public Object getElementAt(int index) {
// Return the appropriate element return model.toArray()[index]; }
// Other methods public void add(Object element) {
if (model.add(element)) { fireContentsChanged(this, 0, getSize()); }
} public void addAll(Object elements[]) {
Collection c = Arrays.asList(elements); model.addAll(c);
fireContentsChanged(this, 0, getSize()); } public void clear() {
model.clear(); fireContentsChanged(this, 0, getSize()); }
public boolean contains(Object element) { return model.contains(element);
} public Object firstElement() { // Return the appropriate element
return model.first(); } public Iterator iterator() {
return model.iterator(); } public Object lastElement() {
// Return the appropriate element return model.last(); }
public boolean removeElement(Object element) {
boolean removed = model.remove(element); if (removed) {
fireContentsChanged(this, 0, getSize()); } return removed; }
public static void main(String[] args) {
final SortedListModel model = new SortedListModel();
model.add("my first");
model.add("my second");
JList jlist = new JList(model);
JFrame jframe = new JFrame();
jframe.getContentPane().se
jframe.setBounds(0, 0, 200, 200);
final JTextField tField = new JTextField();
JButton button = new JButton("add");
button.addActionListener(n
public void actionPerformed(ActionEven
model.add(tField.getText()
}
});
jframe.getContentPane().ad
jframe.getContentPane().ad
jframe.getContentPane().ad
jframe.setVisible(true);
}
}
This code demonstrates that you can add more choices in your list after you displayed it. It's also similar to JTable simply use the TableModel.
Jerson
table.repaint();
table.revalidate();
everytime you add a new row, it should work.
All the best.