0

I apologize for my lack of background in Swing, but I am coming here with a JTable question. My table is structured as:

12 columns across and the amount of rows depend on the incoming data.

I was wondering if there's a way to take the first column's data (Col1) and to check to see if the value exists in the table already, and if so, update the remaining 11 columns for that row, if necessary, and if not add it to the table.

Here's a snippet of my GUI Class

public class ShowGUI extends JFrame {
String [] colNames = new String[] {"Col1", "Col2" "Col3","Col4","Col5","Col6", "Col7", "Col8", "Col9", "Col10", "Col11", "Col12"};
public showGUI() {
PublicTransportation example = new Public Transportation();
example.getRouteTaken();

List<Object> routeList = example.getList();
final Object[][] rowData = new Object[routeList.size() / colNames.length][colNames.length];
    for (int i = 0; i < rowData.length; i++) {
        for (int j = 0; j < rowData[i].length; j++) {
            rowData[i][j] = routeList.get( i * colNames.length + j);
    }
}

DefaulTableModel myModel = new DefaultTableModel(rowData, colNames);
final JTable table = new JTable(myModel);
table.setAutoCreateColumnsFromModel(true);

// JFrame, JPanel
    }
}

As it stands, my table displays all of the rows, of course, does not factor in duplicates. I have looked into Hashing the column, but I am having trouble implementing it. Does anyone have any recommendations?

EDIT: As I receive data from the getRouteTaken method from the Public Transportation class, I want to display the data on the table. I need to call this method to populate the table. A new row will be inserted if the first value of the first column does not exist already in the table. If the value does already exist, then I want to check if any of the values for the rest of the columns for that particular row have changed for a previous state. If so, I want to update them.

Example:

Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8 Col9 Col10 Col11 Col12

data1 data2 data3 data4 data5 data6 data7 data8 data9 data10 data11 data12

If data1's value exists in the table already, then check to see if data values 2-12 have changed. If so, update them. If data1's value does not exist, then add an entirely new row.

Thank you in advance.

  • So after getting the data from PublicTransportation object, you want to add new data or what? please clarify your question on what it is you are trying to do. – Gherbi Hicham Jun 27 '16 at 23:20
  • Hi, sorry, I added an edit. –  Jun 27 '16 at 23:38
  • You'l need to identify the primary key of the relation displayed in the table; a `Map` in your `TableModel`, for [example](http://stackoverflow.com/a/9134371/230513), will make this easier. – trashgod Jun 28 '16 at 01:32
  • If the data is coming from a DB table then the updated/new data should already be in the DB table and if that's the case you can just repopulate the JTable with the data from the DB table and your table will be correct. Unless there is some requirement that you have to check the column data. – ChadNC Jun 28 '16 at 13:39
  • Sorry, this is not a database table. –  Jun 28 '16 at 21:35

0 Answers0