Tag Archives: java

More Java Fun

The TV-as-monitor fad went out this week. I really just need that machine to do more than I make it do, so I’m getting ready to throw XP Pro on it and replace my current desktop.

Besides, I couldn’t read the screen from the bed anyway.

School has been interesting and I’m sure you’d like to read all about it, so:

Java is a little less intuitive than the .NET platform. Programming things like default actions and which element gains focus is a little harder in Java. In fact, I haven’t figured it out. This weekend I had to write a program that accepted a list of names while ignoring duplicates, then allowed the user to search for a name in that list. Easy, right?

The flow of the program was easy: Accept a name or a list of names, put them in a list, then turn around and allow the user to search for a name in the list. The project was to have us use the LinkedList data structure and its methods. Very easy in design, but I got hung up on something so simple as sending the signal when the user was done typing names. I couldn’t do this simple little procedural program, so I decided to do a full-fledged desktop application.

It’s called (for lack of a better name) Name Reader.

Way easier to manage the input with an event-driven model rather than a command-line-based procedural program. The user puts a name in and presses the “Add” button, and voila!

When the list is big enough (or whatever), they can search for a name from the search box:

If the name is in the list, it will say so and give a position number. If not, it will also say so, but give a button with the option to add that name to the list.

Genius, huh?

Actually, I don’ t think so either. I wish I could come up with something really grand so I can join the club of cool programmers. I have all the books; I just need that pinnacle of expertise that says “I have arrived!”

Anyway, here’s the project file (NetBeans) if you want to look at it and improve upon it.

Horizontal Thoughts

I’m writing tonight to see how much I can think to tell my audience as I lay in bed with a wireless keyboard and gyroscopic mouse, staring at my beautiful TV/monitor. Looks like I got it all worked out, but I can barely read the screen from across the room and there’s a significant delay between the keystroke and the appearance of the letter on-screen.
I’m really just up late because I can’t figure out Java multithreading (or multithreading in general) enough to complete a project for school due Monday at midnight CST. So if anyone knows how to write a multithreaded chat server in Java, please contact me soon. I have several questions for you, including these:

1. How can more than one client connect on the same port number?
Never mind. I just figured that one out. The server can listen on a port and has exclusive rights to that port. The number of connections on that port matters only to the program listening.

2. How do I create a thread pool with an ArrayList and then pass a Runnable object to it? I don’t see a method to pass the object after instantiation.
Update: I got that one figured out, too. You don’t pass a Runnable object – just extend the Thread class and fill your ArrayList with those. Worked for me. To stop the thread, set it to null. To start it again, pass it what it needs and then call .start().

If you want to see that code, here is the Client, the Server, and the ServerThread. It’s not pretty and there are some errors when you close a Client, but the multithreading works.

I realize these are not questions for my typical audience, but hey – what else should I do in bed at this hour?

…But I Don’t.

I wrote earlier about programming on this cute little Mac, but there was something I didn’t realize at the time. I found that Java development was already available on this machine and I could compile and run my programs with ease. Fantastic.

But actually writing the code on the Mac has become a bit more than trivial. The only text editors I have are in AppleWorks 6, TextEdit, and vi (a command-line text editor). I can’t figure out how to save a file in TextEdit to a raw text file (it only saves in RTF), it’s virtually impossible to do in AppleWorks, and I’m not taking the time to write programs in vi. Looks like I can’t put the Windows box in the closet yet.

I tried Eclipse, but it never tells me it won’t run – it just doesn’t run. Turns out it only works on 10.4. Damn it. I thought I had it all worked out.