Home > Net >  How to set an existing JTable equal to a new JTable in JSwing?
How to set an existing JTable equal to a new JTable in JSwing?

Time:02-01

So I have an existing JTable that is created upon creating the app and that has values in it. When a button is clicked to sort it, I then create a copy of all the values on the JTable and then sort it and put the sorted array in a 2D array. My existing JTable is called j and my existing model is called model. I created a new table model called sortedModel and now I want to set that equal to j so that it can display the updated sorted table on the screen.

I've tried this so far but it doesn't update it on to the screen so I'm assuming I'm doing something wrong in terms of setting it equal to j (my existed JTable).

Here is my code:

    // Converting 2D arraylist to normal 2D array for JTable
    String[][] sortedData = tempUsersToAList.stream().map(u -> u.toArray(new String[0])).toArray(String[][]::new);


    DefaultTableModel sortedModel = new DefaultTableModel(sortedData, columnNames);
    j = new JTable(sortedModel);

CodePudding user response:

//j = new JTable(sortedModel);

Don't create a new JTable.

Just reset the model of the existing table:

j.setModel( sortedModel );
  • Related