Solved

JTable

Posted on 2000-02-18
8
630 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:93MH1351
8 Comments
 

Expert Comment

by:theoneandonly
ID: 2534381
I think that if you use :

table.repaint();
table.revalidate();

everytime you add a new row, it should work.

All the best.
0
 

Expert Comment

by:108093233315
ID: 2534386
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();
0
 
LVL 7

Expert Comment

by:vladi21
ID: 2534407
0
 
LVL 2

Expert Comment

by:mwibbels
ID: 2534408
>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.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 2

Expert Comment

by:mwibbels
ID: 2534410
Sorry vladi21, didn't see your comment.
0
 
LVL 7

Accepted Solution

by:
vladi21 earned 100 total points
ID: 2534418
dont' use repaint and validate!

call fireTableRowsInserted()

look at javax\swing\table\DefaultTableModel.java
0
 
LVL 7

Expert Comment

by:vladi21
ID: 2534423
if you still have problems - post ur code
0
 
LVL 4

Expert Comment

by:jerch
ID: 2538305

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().setLayout(new BorderLayout());
        jframe.setBounds(0, 0, 200, 200);

        final JTextField tField = new JTextField();

        JButton button = new JButton("add");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                model.add(tField.getText());
            }
        });
        jframe.getContentPane().add(tField, BorderLayout.NORTH);
        jframe.getContentPane().add(button, BorderLayout.CENTER);
        jframe.getContentPane().add(jlist, BorderLayout.SOUTH);
        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
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now