Forums Register Login

Need help with Mini Telephone Directoy program please

+Pie Number of slices to send: Send
Hi guys,

I please need your help with a few issues that I have with this Mini Telephone Directory program.

Firstly I would like to know if is it possible to convert a TreeMap into an array?

The reason why I'm asking this is because I've used a TreeMap to store the names(as keys) and the numbers(as values),

According to my assignment specs I have to: " * Use a JTable inside a JScrollPane to show all the name-number pairs. "

I've created a new class called myTable and this is what it looks like so far:



I'm not sure if what I've done so far is correct as well as what code I should add at line 21 and line 24, could you please help me out on this one?

Here is all my code I did so far for this program:



Thank you very much

P.S. - Any other hints or tips are also welcome

Regards
+Pie Number of slices to send: Send
Please be careful with long lines; they fall off the edge of the screen. I had to add a newline to avoid left-and-right scrolling.

TreeMap implements the Map interface and the Map interface has a method which puts all the Entries into a Set and you can change a Set to an array . . .
Do you need a TreeMap? Is the sorting necessary to your application? If not, can you use a HashMap instead?

JTables are usually discussed in the Swing forum, so I shall have to move you there.
+Pie Number of slices to send: Send
 

Please be careful with long lines; they fall off the edge of the screen.
I had to add a newline to avoid left-and-right scrolling.



Sorry Campbell Ritchie it won't happen again. Thanks for mentioning it.

Well I think it will look better in the JTable when all the entries are sorted, but don't think it is really necessary...

If I use HashMap how will this change things? Please explain...

Thanks.

+Pie Number of slices to send: Send
 

If I use HashMap how will this change things? Please explain...


It won't change things are far as your problem is concerned. The reason Campbell mentioned a HashMap is it is more efficient than a TreeMap (particularly for large data sets) and so unless ordering is important it is better to use a HashMap. The get, put, containsKey type methods run in constant time in a HashMap and log(n) time in a TreeMap.

In your case, unless performance becomes a real issue, I'd use a TreeMap because as you say displaying sorted data looks better than displaying random entries.
+Pie Number of slices to send: Send
Tony Docherty, thanks for the reply.

Well performance isn't going to be an issue here because its only a small assignment
I have to do so there isn't a large data set in this case.

How would you deal with this problem and how can I change this code
so that the entries that were added will be shown in the JTable?

Could you please give some examples or some help to overcome this problem that I have.

Thanks.
+Pie Number of slices to send: Send
Campbell has outlined one way. Another is to create a 2D array where one dimension is 2 and the other is the size of the Map and then iterate over the Map's keys and fill the array with each of the keys and it's associated value as you go.
+Pie Number of slices to send: Send
personally, I think you're over-complicating it.

some things to consider:
1) you're only after name and number, so just use the JTable's model.
2) JTable also has a built in rowSorter (java 1.6+)
3) a JFormattedTextField might be better than your JTextField/regex checking/error generation
4) use a better layout manager for TelephoneDirectory() - check what happens when the frame is dragged wider/leaner
5) clean up windowClosing - get rid of minimize, and the only code there should be to exit on 'YES', meaning the frame
should be set to DO_NOTHING_ON_CLOSE initially
Who among you feels worthy enough to be my best friend? Test 1 is to read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1720 times.
Similar Threads
Need help with assignment - Mini telephone book program please
problem in specifying file path
textfield and listener
JScrollPane within JTable cell issue
Please run the program in pure Unix Box
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:02:57.