File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes  JTable(!!!) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark " JTable(!!!)" Watch " JTable(!!!)" New topic
Author

JTable(!!!)

Gir Gor
Greenhorn

Joined: Feb 10, 2004
Posts: 6
Hello !
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 :
http://www.cscc.de/books/swingbook/files/uts2/Chapter18html/Chapter18.htm
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.
Thanks
GG
Garandi Garandi
Ranch Hand

Joined: Jan 07, 2003
Posts: 192
I would say, add those rows to the model of second table and you should be fine.
Garandi
Gir Gor
Greenhorn

Joined: Feb 10, 2004
Posts: 6
Hi!
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.
Thanks
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch Gir.
There are some excellent examples in the chapter of the book you linked. Once that applies is 18.4.
In 18.4 we have:

m_data.retrieveData(date);
...
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:
SwingUtilities.invokeLater(updater);
You can use the same technique. Add an action listener to the copy button that finds out the rows to add.


SCJP2. Please Indent your code using UBB Code
Gir Gor
Greenhorn

Joined: Feb 10, 2004
Posts: 6
Hi Jose B
I am very thankful. I will try it.
Regards
GG
Gir Gor
Greenhorn

Joined: Feb 10, 2004
Posts: 6
Hi Jose B
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.
Thanks
GG
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
STockTable.retrieveData is called by the action listener of the JMenuItem mData. This method delegates the proper retrival of the database, and addition to the model to StockTableData.retrieveData
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.
Gir Gor
Greenhorn

Joined: Feb 10, 2004
Posts: 6
Thanks . It was very kind of you to help me with may problem. I have a question : Do I need two table model, one for each table??? or just one is enough for both of them.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I guess this is not the case the two tables hold the same data but present it in a different manner. Two models are needed.
The funny thing is that if the content of one is a subset of the other, the two models could share the same data structure.
Gir Gor
Greenhorn

Joined: Feb 10, 2004
Posts: 6
Hi Jose
I just want to thank you for every thing. I did it and it works very well.
Thanks
Have a nice day
Regards GG
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JTable(!!!)
 
Similar Threads
JTable and sorting
Column in jtable
how do i populate Jtable from my sql
Removing rows from DefaultTableModel
Image in JEditorPane