This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes JTable issues Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "JTable issues" Watch "JTable issues" New topic
Author

JTable issues

David Winters Junior
Ranch Hand

Joined: Oct 30, 2007
Posts: 47
Hi All,

I have 2 issues which i am currently facing regarding displaying and using the Jtable component in urlybird 1.3.1

1.
I am having an issue at present whereby i need to ba able to hide from the user the data in a particular column in the Jtable as the data is not for user viewing purposes.

I have a seperate class for the TableModel which represent the data which is stored in the JTable so

TableModelClass temp = new TableModelClass;
JTableClass main = new JTable(temp);

However i only want to display 9 of the 10 columns in the JTable. Does anyone know the code which will allow me to do this. By the way i need to just hide this column data from the user but it needs to be in the JTable as
the column data contains primary keys which are used for data retrieval in other parts of the application.

Is there any code to size the reduce a particular column width size to zero?

2. I would like to know how anyone else worked around the issue of trying to display a HashMap in a JTable. I am storing bookings from the database file in a hashmap whereby the key value is the filepointer to the record on file and the booking instance is stored with this key. I suspect i may need to store this data in an ArrayList rather than in a HashMap in my TabelModel class in order for the JTable to display the Bookings correctly here but i am not sure?? I am wondering how others worked around this issue?

Any help would be appreciated on either of these issues.

david
Thomas Hubschman
Ranch Hand

Joined: Sep 25, 2002
Posts: 88
Hi David,

(First Question)
However i only want to display 9 of the 10 columns in the JTable. Does anyone know the code which will allow me to do this. By the way i need to just hide this column data from the user but it needs to be in the JTable as
the column data contains primary keys which are used for data retrieval in other parts of the application.


In your implementation of the TableModel you can extend javax.swing.table.AbstractTableModel and then provide your own definitions of:


This should allow you to hide the column but keep the data in the model.

(Second Question)
I would like to know how anyone else worked around the issue of trying to display a HashMap in a JTable. I am storing bookings from the database file in a hashmap whereby the key value is the filepointer to the record on file and the booking instance is stored with this key. I suspect i may need to store this data in an ArrayList rather than in a HashMap in my TabelModel class in order for the JTable to display the Bookings correctly here but i am not sure?? I am wondering how others worked around this issue?


There is constructor of DefaultTableModel as follows:



Personally I kept the record data as an array which made my implementation of the getValueAt() method listed above very easy.



Hope that is of some help.

Regards,

Tom
[ January 22, 2008: Message edited by: Thomas Hubschman ]

SCJP '02, SCWCD '02, SCJD '08, SCSNB '09, SCEA '10
David Winters Junior
Ranch Hand

Joined: Oct 30, 2007
Posts: 47
Thanks for the reply tom.

For the first issue i have found the solution to hide a particu;ar column


I have decided to use the TableColumn api which has a setMaxwidth() method which will allow me to set the column width to zero.

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/TableColumn.htm

Regarding the second issue at hand im not sure if it will work in my case. I figure i best use an ArrayList type in a TableModel class to store data to be displayed in the JTable.

Data in my Hashmap which i use a cache to store records from the database looks like this:

key object

74 Booking
140 Booking


The key value above points to the location on file where the record is stored and the booking contains the releavnt database fields.

So i need to have the key as part of the data which i display in the Jtable so that when the user clicks the button on the UI to remove the record after selecting it the underlying record key is assocaited with the row object so that i can then use this key value to remove this record from the hashmap and db file.

I wonder if changing my design not to use a hashmap and instead use an arraylist to store records in my cache whereby the Booking object will contain a field to store the key or pointer to the record on file??

David
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable issues
 
Similar Threads
JTable
JTable
JTable(!!!)
JTable
how to hide a column in JTable view