aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Need help with Mini Telephone Directoy program please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Need help with Mini Telephone Directoy program please" Watch "Need help with Mini Telephone Directoy program please" New topic
Author

Need help with Mini Telephone Directoy program please

Alex Reynders
Greenhorn

Joined: Nov 16, 2008
Posts: 13
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

Joined: Oct 13, 2005
Posts: 36599
    
  16
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

Joined: Nov 16, 2008
Posts: 13
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

Joined: Aug 07, 2007
Posts: 1950
    
  28
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

Joined: Nov 16, 2008
Posts: 13
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

Joined: Aug 07, 2007
Posts: 1950
    
  28
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

Joined: Jun 09, 2003
Posts: 4632
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with Mini Telephone Directoy program please
 
Similar Threads
JScrollPane within JTable cell issue
Please run the program in pure Unix Box
Need help with assignment - Mini telephone book program please
problem in specifying file path
textfield and listener