Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with Mini Telephone Directoy program please

 
Alex Reynders
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alex Reynders
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Tony Docherty
Bartender
Posts: 2941
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alex Reynders
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tony Docherty
Bartender
Posts: 2941
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic