954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Creating a table model Question

Hi guys,

First, many thanks for reading my post, happy to have found you!

I have data(objects) that is stored in a hashmap, my goal is to store this data in a JTable, using a separate cell for each entry, rather than starting on a new column each time.

I am creating my own table model (as I want it to detect the correct data type, and customize the data in certain ways). My Hashmap is being initialised and fed in my table model's class.


My question is:

is there a way I can somehow create a new JTable by giving it a hashmap, by customizing my model, or would I have to convert it to either vector or multidimentional array of objects?

- I am imagining a solution would be to convert this hashmap into an arrayList that I will then manipulate to create my JTable. How can I convert my hashmap into a list of objects?

-Any advice on how to achieve this?

Regards,

Dani

daniiii
Newbie Poster
6 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

There is a setValueAt method that could be called in the course of initializing your TableMode, assuming the JTable exists at that point. Otherwise, there are various JTable constructors that you could call. One takes an array of arrays of data: You could either build the double array along with, or instead of the hashMap

griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
 

Can I use the setValueAt method in my table Model class itself? Would it be something like this.SetValueAT(......)

Thanks,

Dani

daniiii
Newbie Poster
6 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Maybe. The details of your code matter. The JTable has to already be constructed (or, I guess, be in the process of construction) when you call that method, otherwise, there is not yet a a real place to set the value "at". My preference would (maybe/probably) be to create a double array instead of or along with the hash map, then pass that double array to the JTable constructor, assuming I could do it that way. Here are a couple of references:
JTable Constructors: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTable.html#constructor_detail
Table tutorial: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
 
Maybe. The details of your code matter. The JTable has to already be constructed (or, I guess, be in the process of construction) when you call that method, otherwise, there is not yet a a real place to set the value "at". My preference would (maybe/probably) be to create a double array instead of or along with the hash map, then pass that double array to the JTable constructor, assuming I could do it that way. Here are a couple of references: JTable Constructors: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTable.html#constructor_detail Table tutorial: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

Thanks for the help, I have managed now managed to create the table I want. I am creating a dummy row when needed (depending on the number of data I have) and then using setValueAt to store the data when it arrives. But unfortunately I am not seing the borders on the cells, the table is like a plain white box. Do you know an easy way to set the borders? Will it have to be through a cell renderer?

Regards

daniiii
Newbie Poster
6 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

No clue. I could read the docs, but so can you. At a guess without even looking, yes, the renderer would be the place to do such things (you would not want rendering information 'contaminating' actual data)

griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
 
No clue. I could read the docs, but so can you. At a guess without even looking, yes, the renderer would be the place to do such things (you would not want rendering information 'contaminating' actual data)

Right, it is not an issue anymore.

Thanks a lot for your help.

All The best

Dani

daniiii
Newbie Poster
6 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: