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.