I am new in JAVA PROGRAMMING and I really need your help. PLEASE HELP ME WITH MY PROBLEM.
I have two Jtable with two TableModel.My first Table is just like the (ExpenseReport table)
that you can find here :
18.8 Custom models, editors, and renderers.
I have three buttons(Insert, Delete and Copy).I want that user be able to choose some rows in first table by clicking on checkboxes that I have in my last Culomn in FirstTable and then pressing the copy butten and copy those row to the second Table.
I have tried so hard to find a way for this problem but I find nothing.PLEASE HELP ME.
How can I do this??? I want that user be able to copy one or more rows to another JTable with pressing a copy button. If I add rows to the second table model can stil user use the copy button???
And would yo please give me an example of adding row to second table model.
There are some excellent examples in the chapter of the book you linked. Once that applies is 18.4.
In 18.4 we have:
myTable.tableChanged( new TableModelEvent(m_data);
m_data is an implementation of TableModel. The method retrieveData adds to the model a new row (obtained from a database) like this:
m_vector.addElement(/* an instance of StockData representing a new row */);
m_vector is the structure that holds the data in the m_data object. After modifying it with the addition of a new row, a TableModelEvent is created and passed to the tableChanged method. myTable object is then repainted. Because tableChanged method was declared in TableModelListener it must be invoked from within the Event-dispatching thread:
You can use the same technique. Add an action listener to the copy button that finds out the rows to add.
In the link that I gave you :Swing
there are two retrieveData methodes one in the StocksTable class and one in the StockTableData class.What is the reason for that?
Is it possible for you to explain more to me what I have to do? I am new in Java and I treid hard but still is very difficult to anderstand.
Please help me to find the way for that problem.
Say StockData is the class whose instances represent rows contained in the vector that holds the data. The action listener of the copy button will need to find out the rows of the first table whose checkbox is marked. Those rows are the ones that need to be added to the second table. Make new StockData objects representing the content of the previous mentioned rows and add them to the model of the second table in the way shown above.
The Java Tutorial has a section called How to use Tables I am quotting for the section Detecting Data Changes.
An example of updating a table's data without directly editing it is in the BINGO application. The BINGO application, which is presented in BINGO!, has a table that displays some information about each user who is signed up to play the game. When a new user signs up to play BINGO, the table needs to add a new row for that user. More precisely, the table model needs to get the data for the new user, and then the table model needs to tell the table to display the new data.
To notify the table model about a new user, the BINGO application invokes the table model's updatePlayer method. You can see the code for that method in PlayerInfoModel, which contains the implementation of the table model. The updatePlayer method records the new user's data and fires a table-model event. Because every table listens for table-model events from its model, the user-information table automatically detects the change and displays the new data.
To fire the table-model event, the model invokes the fireTableRowsInserted method, which is defined by the AbstractTableModel class. Other fireXxxx methods that AbstractTableModel defines are fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, and fireTableStructureChanged.
How to use Tables will help you a lot to understand JTables.