0

I have the following code for creating a JTable

public void tableTeam()
{
    rl.readRunners();
    String[] sampleHeaders = {"Athlete ID", "Team"};

    JTable myTable = new JTable(rl.teamTableData,sampleHeaders);

    myTable.setAutoCreateRowSorter(true);
    myTable.setRowHeight(20);
    ///////////////
    sorter = new TableRowSorter(myTable.getModel());
    List sortKeys = new ArrayList();
    sortKeys.add(new RowSorter.SortKey(1, SortOrder.DESCENDING));
    sorter.setSortKeys(sortKeys);
    sorter.setRowFilter
    (
        new RowFilter<TableModel, Integer>()
            {
            @Override
            public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry) 
                {
                    boolean included = true;
                    Object cellValue = entry.getModel().getValueAt(entry.getIdentifier(), 0);
                    if (cellValue == null || cellValue.toString().trim().isEmpty())
                    {
                        included = false;
                    }
                    return included;
                }
            }
    );
    myTable.setRowSorter(sorter);
    ///////////////

    teamScrollTable = new JScrollPane(myTable);
    teamScrollTable.setSize(500,300);
    teamScrollTable.setLocation(50,100); 
    System.out.println("Creating team table");



    teamPanel.add(teamScrollTable);
}

And then I have this code to update the table.

public void RefreshTeam()
{
    teamPanel.remove(teamScrollTable);

    rl.readRunners();
    String[] sampleHeaders = {"Athlete ID", "Team"};

    JTable myTable = new JTable(rl.teamTableData,sampleHeaders);

    myTable.setAutoCreateRowSorter(true);
    myTable.setRowHeight(20);
    ///////////////
    sorter = new TableRowSorter(myTable.getModel());
    List sortKeys = new ArrayList();
    sortKeys.add(new RowSorter.SortKey(1, SortOrder.DESCENDING));
    sorter.setSortKeys(sortKeys);
    sorter.setRowFilter
    (
        new RowFilter<TableModel, Integer>()
            {
            @Override
            public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry) 
                {
                    boolean included = true;
                    Object cellValue = entry.getModel().getValueAt(entry.getIdentifier(), 0);
                    if (cellValue == null || cellValue.toString().trim().isEmpty())
                    {
                        included = false;
                    }
                    return included;
                }
            }
    );
    myTable.setRowSorter(sorter);

    teamScrollTable = new JScrollPane(myTable);
    teamScrollTable.setSize(500,300);
    teamScrollTable.setLocation(50,100); 
    System.out.println("Changing team table");

    teamPanel.add(teamScrollTable);
}

This code is activated when a button is clicked on the program. However unlike my expectations this does not update the table. I did some research and found this line of code fireTableCellUpdated(). Although I am unable to implement this into my code so that it updates the table since I do not know how. I would greatly appreciate someone helping me implement this code or showing me a better way to do what I want.

1
  • I am aware of the Java Tutorials however I tried implementing this code but it was unsuccessful
    – Dan
    Apr 20, 2015 at 18:41

2 Answers 2

2

In order to update the table you can use the following:

myTable.setModel(new DefaultTableModel(rl.teamTableData,sampleHeaders));
1
  • 1
    Yes, resetting the model will work and this approach should be used instead of creating all new components (+1). You may lose the sorter, but the data in the table will display properly assuming you have updated the "rl.teamTableData" properly.
    – camickr
    Apr 20, 2015 at 19:17
0

The refreshTeam method (note java methods should begin with lowercase) is not updating the JTable, it is creating a new table entirely, and then trying to add these Components to (what I presume is) an already visible UI. The UI is unaware of this, so after adding/removing Components you should call...

revalidate();
repaint();

...on the components. There are alternatives to this approach - you can change the model rather than recreating the table. You can do so by implementing your own AbstractTableModel (you can then call the fireTableDataChanged that you refer to on the instance to update listeners that the data has changed).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.