• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

JTable(!!!)

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say, add those rows to the model of second table and you should be fine.
Garandi
 
Gir Gor
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Gir Gor
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jose B
I am very thankful. I will try it.
Regards
GG
 
Gir Gor
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic