Ease JTable Development - Java SE (Archived)

To customize each JTable by hand coding the model into the JTable class requires too much manual work. I want to fill data into the JTable without creating a new TableModel class. There should be some convenience methods. Customization should be done without inheritance, but by composition.
Mert Nuhoglu
http://jroller.com/page/mnuhoglu/

Related

An explanation on JTables

Hello,
I am having a bit of trouble getting the full concept of JTables. Currently I am constructing a JTable from vectors, but I need to do updates fairly frequently, and also have the ability to sort the list.
JTable newtable = new JTable(vectorrows,vectorcols);
I have no problems with the construction, but when it comes to updating the data, I feel like I am approaching the solution with minimal elegance. Once I create the table, if I need to update the table, I create a new instance of the table, and drop the table into my container (usually a JScrollPane). This will obviously not help me when it comes to sorting the list, or updating the data on the fly.
From what I have been reading I should implement an interface for the Table, and using that interface to access the abstract concept of the table. Overall I have a decent understanding of some core Java topics, but I need someone to just explain in plain words what I am missing on this topic.
I am not able to post any code right now, but I will if someone needs it. Basically I just need an explanation of why we go abstract and how that works conceptually.
Thank you! 
WhitworthBot wrote:
Hello,
I am having a bit of trouble getting the full concept of JTables. Currently I am constructing a JTable from vectors, but I need to do updates fairly frequently, and also have the ability to sort the list.
JTable newtable = new JTable(vectorrows,vectorcols);
I have no problems with the construction, but when it comes to updating the data, I feel like I am approaching the solution with minimal elegance. Once I create the table, if I need to update the table, I create a new instance of the table, and drop the table into my container (usually a JScrollPane). This will obviously not help me when it comes to sorting the list, or updating the data on the fly.
From what I have been reading I should implement an interface for the Table, and using that interface to access the abstract concept of the table. Overall I have a decent understanding of some core Java topics, but I need someone to just explain in plain words what I am missing on this topic.
I am not able to post any code right now, but I will if someone needs it. Basically I just need an explanation of why we go abstract and how that works conceptually.
Thank you!Look at the DefaultTableModel class.
This will give you tools to update an existing table.
� {�                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
In a typical MVC architecture like Java Swing library, you should manipulate the M part for updating data and reflecting the update to the view, the GUI component. For JTable, methods defined in the DefaultTableModel would do everything you need.
Or, for a more drastic update of the data, you would do:
myTable = new JTable(new MyTableModel(newDataSet));

import simple .csv into jTable

Hi all,
Newbee to Swing, and have been trying to find a simple way of importing .csv files into Jtables...any ideas? 
If I were you, I would create a subclass of AbstractTableModel. You will have to override and implements few methods.
As far as the loading of the csv files is concerned, I think there are numerous java api out there (such as http://ostermiller.org/utils/ ). 
I would suggest that Swing newbies cut their teeth on simpler components than JTable, but here goes:
1. read input 1 line at a time with a BufferedReader
2. Using String's split method to split line around commas.
3. Add that row to a (subclass of) DefaultTableModel with its addRow method.
4. When done processing input, call JTable's setModel to slide that model into your JTable.
(Doing 4 before 3 will fire off mucho model events to the GUI which you probably want to avoid.) 
Unless you're comfortable whipping up data structures and are au fait with the obverser pattern,
it's probably easier to subclass DefaultTableModel versus AbstractTableModel.
In that case, you're right.
I rarely use default models for a very specific reason : I use a very formal MVC (called Pac-Amodeus) and I need to ensure that the data are in the model and not in the GUI. Using an abstract model is the best solution I found. 
Yes, I often use AbstactTableModel because I need to code an adapter for an existing data structure.
But too often I've seen newbies and generally those in other their heads (like ivtl) subclassing ATM
and slogging through the consequences instead of whipping up something with DTM in a few lines.

About hiding columns in a JTable.

I have read various ideas on how to hide columns in these forums,. Everything from using the removeColumn method in TableColumnModel to writing a wrapper TableModel to handle the details. These ideas work, but in certain situations it is difficult to retro-fit the code in this manner. Being that Swing has a MVC architecture, does it not make since that I should be able to alter the view ( The Table ) any way I like without altering the underlying models? Does anyone have a good solution?
I already had implemented a TableModel which does this, and a TableColumnModel. Probelm is, I have external libraries that make extensive use of the DefaultTableColumnModel in the JTable, and I would rather not have to alter it. those libraries.
Thanks.
AC 
Being that Swing has a MVC
architecture, does it not make since that I should be
able to alter the view ( The Table ) any way I like
without altering the underlying models? Does anyone
have a good solution?I can see your point, but JTable presents the view it does which is customizable in certain ways only.
The hiding of columns just isn't part of what it does.
So you could extend JTable to add such functionality. Having looked at the source of JTable recently, I wouldn't want to do that.
The alternative is to wrap existing models in another model that does some transformation (i.e. hiding a column) without changing the underlying model. See the TableMap and TableSorter examples in the Swing Table Tutorial to see an example of this.
I already had implemented a TableModel which does thisSounds to me like you've broken the MVC relationship by doing this. The DataModel is used to store the data and a JTable is just a view of the data. The default view is to display all data. If you want to change the view of the data you should be changing the JTable not the DataModel.
You can effectively hide a column in a JTable by setting its width to zero....
TableColumnModel TCM=myTable.getColumnModel();
TCM.getColumn(columnIndex).setMinWidth(0);
TCM.getColumn(columnIndex).setPreferredWidth(0);
where myTable is the JTable whose column you want to hide and columnIndex is the column to hide.
;o)
V.V.
Sounds to me like you've broken the MVC relationship
by doing this. The DataModel is used to store the data
and a JTable is just a view of the data. The default
view is to display all data. If you want to change the
view of the data you should be changing the JTable not
the DataModel.The use of a wrapper which implements the TableModel interface and contains the actual model would not break the MVC relationship. This wrapper class would be part of the View in the MVC triad (although to confuse things it would implement TableModel).

100% pure Newbie (Switch Swing Components)

I have a JTable within a GridBagLayout. For whatever reason, TableModel cannot be constructed or set with the flexible inputs that the JTable constructors have. My table represents search results. When the query is returned, it'd be ideal for me to just call the constructor for the JTable and replace the old JTable with the new one. I'm obviously far too great a newbie to accomplish this. The sad thing is that I've actually aggressively looked for this. :-(
Any help is much apreciated.
--Zephryl                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
Recreating the whole table and underlying model just to update things is bad. Don't do that.
You need to put the 'update' logic into your underlying TableModel (extend AbstractTableModel). Once you have updated the data in the table model (how is up to you), fire the appropriate change event so the table is redrawn. 
So, I should use setValueAt() of the TableModel? Why do I need to make my own model class? Can you call setValueAt() of a row that didn't previously exist? Even though it wasn't optimized, I'd still like to know if it's possible to switch out swing components and how you'd go about it if so. 
setValueAt probably won't do what you want. You don't want to update a model one cell at a time, you want to do a mass update. Also, you can't ADD any new data using setValueAt. The DefaultTableModel is very basic, and really isn't suitable for doing more than hacking stuff together for a demo or something ultra simple. You will need to subclass AbstractTableModel and implement the necessary methods, including adding any of your own to add stuff to the model, or to refresh the entire model. This is the normal way it is done.
Read through the JTable tutorial (paying close attention to the model related stuff) and the various table related class documentation.
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
Even better, get a book on swing. Most have good chapters on tables. There really isn't any way someone here can relate all the complexities of using JTable and it's related classes in this forum. Good luck. :-) 
Blah! So, I went and made my own TreeModel class that extends AbstractTreeModel, and then found out it serves the exact same purpose as DefaultTreeModel. Sigh... 
Serves the same purpose, yes. But you have total control over how the data is actually structured, you are not forced to use their underlying data structure. :-)

abstract table model updating

Hi,
I have a client server application using rmi, the client has an jTable which uses an abstract table model, the data source for the table model is updated by the server periodocially and the changes need to be reflected in the jTable.
I am using the fireTableRowsInserted method on the table model but I am a little confused about the parameters which I should suppy to this method. Can anyone point me to some documentation which explains best practise for these fire... methods
Thank ahead 
Swing related questions should be posted in the Swing forum.
jTable which uses an abstract table model,No, you have a custom TableModel that extends AbstractTableModel.
Why are you creating a custom model. The DefaultTableModel will probably work for you without and custom programming and it fires the appropriate events when required. Use the addRow() or insertRow() method.

Categories

Resources