I am prepared to be corrected if I am wrong but...

The Jtable is a pretty powerful and useful object.

As I understand it the JTable is designed to hold one object type in each column. For example this matches well with the rows representing the records in a database table and the columns representing the fields.

For this reason the AbstractTable Model supports getColumnClass which is great but sometimes the application needs the record in a column and not a row. In this case we would need a getRowClass method but I don't see one. Of course it would not be sensible to use both methods on the same table!

I also hoped for a getCellClass method but I don't believe there is one in the Abstract Model either.

OK so this must be easy to do.

So how can it be achieved effectively.

Recommended Answers

All 10 Replies

What you're trying to do is not something any grid control in any language will give you.

A column by definition is merely a placeholder for all values of a particular field per row.
You can get those values easily by iterating over each row and taking that particular field.

There is no single record in a column, therefore what you're trying to retrieve doesn't even exist.

To get the class of a cell simply take the class of its column, it will always be the same (or a superclass) of the class of the actual data in the cell you're looking at.

I want to edit one record at a time and display it in a column. There are several fields which are fairly wide and if I use a column for the display then it will fit on the screen.

If I use the first column to display the field name (non-editable) and the second to allow the user to edit the field I only need a two column table. In this case the number of rows is fixed of course to match the number of fields in the data.

My issue concerns the display because if I could use a renderer to show a column from the data model as a row on the display that would be fine.

I thought this might be a familiar issue to JTable users. I guess everyone puts their records into rows and not columns.

You can always write your own renderer, but I'd guess that would be quit difficult.

Anyways, not sure I fully understand your problem.

He seems to think that there is some way that each column is a record in its own right, rather than a collection of values taken from a specific field one from each row.

There is no such thing, the entire idea is counter to what a tabular representation of data means.

I had better describe my requirement in a different way. This is not a different problem so hopefully it will make the issue clearer.

I have some data values which could be compared with fields in a database. One is a boolean, a few numeric and most are string. The set of different types are grouped into an object which might be regarded as a record. Those objects are then stored in a vector which can grow and shrink. The vector could be compared with a database table.

I want to edit one record in a vertical line so that there is plenty of screen area for some long field values which are mostly the strings. If it were an address for example the first line might be the house name, the second line the street etc. But I don't want every line to be a string.

I thought that a JTable might be useful here because it can provide a suitable edit function if the renderer can recognise the type of the data in each cell. Then I realised that if I had one column the JTable does not have a getRowClass. Of course I could edit the record as one row with a JTable but the screen would not be wide enough. Using my illustration you would expect to edit an address in a vertical column not in a horizontal row scrolling off the screen to the right when you get to enter the country.

I still feel this must be possible with the JTable and hence the thread name but as I understand it the renderer can't move cells around because the cell type comes from the TableModel. Maybe it can?

This is all solved if all row references can be treated as column references and all column references can be treated as row references. If I could make a virtual numeric cell say, at row 0, column 5 appear at row 5 column 0 that would do it but it must be possible to edit the cell as a numeric in this case.

Maybe the question could become...

Can a JTable renderer display a conceptual row from the Table Model in a column on the screen and still allow the user to edit different cell types as it can in the default JTable layout.

Sorry if I have still not described this clearly but I hope that it makes sense to you now.

Ah, you want to swap the meaning of row and column, that's all?

No, I don't think that's easily possible.
You might be able to subclass JTable but I don't think that'll work.

Or you would have to create something yourself, probably along the lines of the TDbCtrlGrid in Delphi.

You got it. I was doubting my communicaions skills big time!

The concept of swapping the row column meaning is spot on. Wish I had thought of describing it like that. Could also be described purely as a screen layout issue I suppose.

Sub classing the JTable sounds useful but I'll have to think that through a bit more before I understand what would be invloved.

I imagine that there is a point in the class where all row references could be changed to column references and all column references could be changed to rows but it probably doesn't work like that. This switch must work for the render and edit functions.

The point is that I thought that the JTable could offer row/column or column/row layouts as an initialisation option. To this extent you appear to be saying that this IS a JTable limitation.

If this can be overcome in a fairly trivial way I will accept that the design avoids the potential confusion in documentation etc. but it must be a common requirement for screen edit layouts to require the vertical record format.

I could resort to a stack of Text fields but I still feel the leverage of using a table must be worthwhile somewhere down the line. Even for one column.

It's not all that common or there would have been a component for it.
Delphi includes a component that does more or less what you want maybe but only in the highend versions.
I've found it occasionally useful, but certainly don't miss it.

Your best bet might be to create a custom JPanel derivative containing your own input fields for each field, and another JPanel derivative to display a number of these in some way.

Perhaps it is more uncommon than I realised, yet when you register your address on any mail-order, or bank, site the fields appear in a vertical row every time. I guess they don't use a Jtable! Perhaps they would if it supported the option!?

I expected many more comments on this issue so that speaks for itself too I guess.

A JTable is for one use, what you want is served by the GridLayout.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.