Click to See Complete Forum and Search --> : JTable
Rose Zhou
March 6th, 2000, 07:03 PM
Hi,everyone:
I want to design a table in Java, this table has 3 fixed columns,more than 20 non-fixed columns, and I want to support sorting by column. I searched the Internet, and found somebody designed a table which suppots sorting but doesn't support fixed columns, and somebody designed a table which supports fixed columns but does not support sorting. I am wondering if somebody can give me an idea how to design a table which supports sorting by column and supports fixed columns?
Thanks in advance!
Rose Zhou
mjpell
March 7th, 2000, 12:19 PM
If you are just wanting to prevent the user from moving columns:
this.getSavedTransfersTable().getTableHeader().setReorderingAllowed(false);
For sorting, see http://www.hmravens.com/index.html?http://www.hmravens.com/source/source.shtml
and
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#sorting
Rose Zhou
March 7th, 2000, 08:34 PM
Hi, mjpell:
Thank you very much for your response!
I don't want to prevent the user from moving all the columns, instead I only want to set some of the columns fixed, and others are non-fixed.There is an example here:
http://codeguru.developer.com/java/articles/128.shtml
Actually in this example, the author used two tables. If I sort one of the two tables, I must adjust another table in the same order. I don't know how to do this. Any idea?
Thanks in advance!
mjpell
March 8th, 2000, 06:39 AM
I don't have any great ideas. You might poke around the JTable code and try to figure out how it implements making all columns fixed (see prev post). Maybe you can subclass JTable and reimplement this behavior?
Maybe you can disable being able to select individual columns? and this would prevent user from being able to move it?
If you want to try the 2 tables idea, try using the same instance of a tableModel for both tables. When the table gets sorted, sort the data in the tableModel, then call fireDataChanged() (or something similar) to repaint the tables.
At least this is where I'd start looking. Or I might re-look at my requirements. :-)
Good luck,
Rose Zhou
March 9th, 2000, 12:53 PM
Hi,mjpell:
Thank you very much!
I sorted the problem. I used the two tables idea. When one of the table is sorted, I get indexes of this table, and pass the indexes to another table to force another table to sort in the same order, and fireTableDataChanged event. It works.
I have another problem. I want to make some of the column names into two lines, for example the column name is 12:15-12:30, I want to put 12:15- in one line, and put 12:30 in another line, this will make the column thinner. Any idea?
Thanks!
mjpell
March 9th, 2000, 02:51 PM
Try putting a "\r" or "\r\n" or "\n" in your column name. So your column name would be "12:15 -\r12:30"
I've not tried this, but it seems as though it should work.
Please let me know if it does.
Rose Zhou
March 10th, 2000, 07:58 AM
Hi,mjpell:
Thank you very much!
Putting a "\r" or "\r\n" in the column name can not force the column into two lines.
I found an very good example here:
http://codeguru.developer.com/java/articles/126.shtml
Cheers!
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.