Question

I use this 3 class for show data from database into JTable.

public class TableContent {

private final Vector<String> headers;
private final Vector<Vector<String>> content;

public TableContent(final Vector<String> headers, final Vector<Vector<String>> content) {
this.headers = headers;
this.content = content;
  }

public Vector<String> headers() {
return headers;
}

public Vector<Vector<String>> content() {
return content;
}

And:

public class TableData {

public TableContent getData() {
Vector<String> headers = new Vector<String>();
Vector<Vector<String>> content = new Vector<Vector<String>>();

try {
    Connection conn = DriverManager.getConnection("");
    Statement statement = conn.createStatement();
    ResultSet rs = statement.executeQuery("Select * from table");

    headers = buildHeaders(rs);
    content = buildContent(rs);

} catch (SQLException e) {
    e.printStackTrace();
}

return new TableContent(headers, content);
 }

private Vector<String> buildHeaders(final ResultSet rs) throws SQLException {
Vector<String> headers = new Vector<String>();

int col = rs.getMetaData().getColumnCount();
for (int i = 1; i <= col; i++) {
    headers.add(rs.getMetaData().getColumnName(i));
    }
return headers;
}

private Vector<Vector<String>> buildContent(final ResultSet rs) throws SQLException {
Vector<Vector<String>> content = new Vector<Vector<String>>();

while (rs.next()) {
    int col = rs.getMetaData().getColumnCount();
    Vector<String> newRow = new Vector<String>(col);

    for (int i = 1; i <= col; i++) {
        newRow.add(rs.getString(i));
    }
    content.add(newRow);
 }
return content;
  }
 }
}

And:

public class TableGUI extends JFrame {
// Create GUI and Show table
 }

Formerly i add my methods to my extended DefaultTableModel Class, Now how can i define this methods and where should do it?

Update:

for e.x I test this method:

public class TableContent {
String dbUrl = "...";

private final Vector<String> headers;
private final Vector<Vector<String>> content;

public TableContent(final Vector<String> hdr, final Vector<Vector<String>> cnt) {
    headers = hdr;
    content = cnt;
}

public Vector<String> headers() {
    return headers;
}

public Vector<Vector<String>> content() {
    return content;
}
public void removeRow(int modelRow, Object rowID) {
    String removeQuery = "delete from table where id=?";
    Connection conn;
    PreparedStatement ps = null;
    try {
        conn = DriverManager.getConnection(...);
        ps = conn.prepareStatement(removeQuery);
        Object idValue = rowID;
        ps.setObject(1, idValue);
        if (ps.executeUpdate() == 1) {
            content.remove(modelRow);
         fireTableRowsDeleted(modelRow, modelRow);   // Error
        }
    } catch (SQLException sqle) {
        sqle.printStackTrace();
    }
}
}

Now in here, Problem is fireTableRowsDeleted(...); !

Problem means that IDE say that cannot resolve methodfireTableRowsDeleted(int,int)`

Was it helpful?

Solution 2

What is this the 10th question on this topic? Why does your posted code to create a TableModel not look like the code I suggested in your 9th posting? In that code example I showed you how to return a TableModel from a method. Instead your code is creating two Vectors and trying to maintain a reference to those Vectors. That is NOT the way to do it. The DefaultTableModel contains the data, you don't need a separate copy of the data. You can access the data in the model by using the getValueAt(..) method.

As I have been suggesting you have two options:

  1. Create a helper class with methods to updated the database and TableModel
  2. Extend the DefaultTableModel to add these methods.

For a helper class you would define a method like:

public void removeRow(DefaultTableModel model, int modelRow)
{
    Object rowID = model.getValueAt(modelRow, theColumn);

    // your code to delete row from database

    if (row delected successfully)
        model.removeRow(modelRow);
}

You don't need the rowID as a parameter because you can get the data from the model by using the getValueAt() method. The idea is to use the methods of the DefaultTableModel to manage the data and not reinvent the wheel. All you are doing is adding code to update the database.

If you want to create a CustomTableModel by extending the DefaultTableModel, then your removeRow() method does not need the DefaultTableModel as a parameter and you can invoke super.removeRow() method of the DefaultTableModel.

OTHER TIPS

I want to add more methods to my table model. for example i want to be able to select a row in table

This code could be in a ListSelectionListener that is added to the JTable if you want to activate some action on selection of a row/column/cell. If on the other hand you just want to act on the currently selected row and/or column when some other event occurs, such as the press of a button or the right click of a mouse, then you don't even have to add a ListSelectionListener, but rather query the table for the selected row/column, and then convert the row and/or column index to the model using the appropriate convertXxxxIndexToModel(...) method. Be sure to set your table's selection mode appropriately with setSelectionMode(...) as per the tutorials.

and delete or edit it.

The delete code would begin in whatever event that you wish to use to trigger the delete, probably in an AbstractAction class that can be added to a JButton or a pop up menu, or both. Then this Action would call a deleteRow(...) method in your TableModel.

For more specific help, you will want to show us your TableModel code.


Edit
Regarding your update. You state:

Now in here, Problem is fireTableRowsDeleted(...); !

Please understand that we can only understand that which you tell us, and a statement such as "problem is..." tells us close to nothing. What kind of problems are you having? Does it not compile? If not, please show the compiler error message. Does it compile but throw exceptions? If so, please post the full exception? Does it make your monitor put out white smoke? If so, turn of the computer, unplug it and walk away from it. Does it slap you and call you nasty names? What?

And you still haven't posted any table model code. i.e., there is no code for classes that extend any of the TableModels, neither AbstractTableModel nor DefaultTableModel. Your model code cannot work by magic and will need either one of these two for parent classes.


Edit 2
After reading your comments, I'm getting the impression that you believe your TableContent to be your TableModel and are expecting it to be able to compile with method calls such as fireTableRowsDeleted(...) even though your class extends nothing. Some advice:

  • For a class to behave as a TableModel it first must be a TableModel. Translated into OOPs, that means you must use inheritance since this class must satisfy the "is-a" test of object oriented programming.
  • All TableModel classes should at the very least extend the AbstractTableModel class, or it not that class, then a class that ultimately extends from it, since this class contains wiring that is essential for JTables to work with the model.
  • Some classes can get by with extending the DefaultTableModel class which can save you some work since you can use the ready made methods of this class easily.
  • But to extend the DefaultTableModel, you must put all model data into the super class, usually by calling the appropriate super constructor. You cannot have a separate data model nucleus from the DefaultTableModel super class. So I don't think that this will work well for your purposes.
  • Instead you will probably need to extend AbstractTableModel, and then write your own model methods, taking care to fire the appropriate notification methods (they begin with fire***(...)) whenever you change data.
  • Another option is to use a model class that has been created by others if they fulfill your purpose, such as Rob Camick's table model class that works well with SQL databases that can be found on his blog.
  • No matter what you do, it is imperative that you not just read but study the Swing JTable Tutorial as it contains a veritable chit-load of useful and even necessary information that you will need. You can find this and other tutorials at The Really Big Index of Java tutorials.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top