• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Need help with Mini Telephone Directoy program please

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
What's gotten into you? Could it be this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic