Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Java Newsletters Archive: 7

JavaFAQ Home » Java Newsletters Go to all tips in Java Newsletters


Bookmark and Share

The Java FAQ Daily Tips is a newsletter that is only sent to those
who have specifically subscribed to it.
******************************************************************
*
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* > The Java FAQ Daily Tips, week edition <
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
*
* Issue No: 7 18 September 2000
* http://www.javafaq.nu/
******************************************************************

Table of Contents

> How can I let dial a phone number with Java
> About serialization...
> Again about difference between AWT and Swing
> Can a java application be run off of a CD...?
> How to cut, copy & paste?
> How does my applet read images from jar file?
> Essential difference between an abstract class
and an interface.
******************************************************************

Tip 1

How can I let dial a phone number with a modem in a Java app.?
Is there a way without a System.exec() call and without any M$
classes?

A: You could use javax.comm to do it manually via the serial
port and the good old AT command set on the modem. Alternatively,
you might look at JTAPI, but that might have its own problems
and a lot of overkill.

******************************************************************

Tip 2

Question: If I have a class that implements the Serializable
interface, but it has member variables that reference objects that
do not implement the Serializable interface, it appears that I
can't serialize an instance of the class. I keep getting:

java.io.NotSerializableException

for one of the objects referenced by a member variable.

Am I correct, or am I just missing something. Also, if anyone
knows a work-around to serialize non-serializable objects,
I'd like to hear about it. Unfortunately, I have no control over
the classes I'm trying to serialize, so I tried putting a
serializable wrapper around them, but that didn't work.

Answer: Do you really need to serialize those members of your
class that aren't serializable?
In other words, make them private:

class Foo implements Serializable {
private Bar bar;
}

Do you *need* to maintain the state of the 'bar' variable when
serializing/deserializing Foo? If not, simply declare 'bar'
as 'transient' and it will be ingored during serialization.

******************************************************************

Tip 3

I have a question: What are the architectural differences between
Swing and AWT?

There are no big architectural differences, the class hierarchy
is almost the same. The reason is that Swing is built upon AWT.

The most significant difference is how the components are drawn to
the screen. AWT is so called heavyweight components and have their
own viewport, which sends the output to the screen. Swing is
ligthweight components and does not write itself to the screen,
but redirect it to the component it builds on. Heavyweight
components also have their own z-ordering. This is the reason
why you can't combine AWT and Swing in the same container. If you
do, AWT will always be drawn on top of the Swing components.

You can combine AWT and Swing, just don't do it in the same
container (e.g. panel, groupbox, etc.) and don't put a heavyweight
component inside a lightweight.

Another difference is that Swing is pure Java, and therefore
platform independent. Swing looks identically on all platforms,
while AWT looks different on different platforms.
by Odd Vinje odvinjee@online.no

******************************************************************
Extra Tip from our sponsor.

When you want to bring files from job or your friend home simply
copy them to your free Internet 100Mb X: drive and forget about
floppy/zip disks and other staff!

It is better to keep saved money in your pockets than diskettes!

Just add it to your computer as an aditional drive (easy auto
plugin!) and use with ususal file explorer or any other kind of
file management browser.

Do not leave it for tomorrow! Do it here right now!
http://service.bfast.com/bfast/click?bfmid=31874963&siteid=34480596&bfpage=1
*******************************************************************

Tip 4

Can a java application be run off of a CD without installing
anything (i.e. runtime, etc) on the target computer?
I would like to put my application and hand it out as a demo, but
I want to make it easy to view.

Answer1: The JRE was made so that it didn't need to be "installed".
What I did in one case was to simply put the JRE into a jre folder
in the same directory as my application then invoke it from that
directory using:

jreinjre.exe -cp MyJar.java MyClass

That was for JDK1.1 and you have to modify it slightly for Java 2.
But this did not require any installation of environment variables
to be set up. The JRE was smart enough to know how to get to its
system classes relative to where the jre.exe file was located.
by Dale King KingDo@TCE.com

Answer2: you could try a Java to native compiler.

******************************************************************

Tip 5

I've got a (simple) menu on a new application and am trying to put
in the works behind the cut, copy & paste menu options - does
anyone know how I can do this - what's the code or can you point
me in the right direction?

A: Look at java.awt.datatransfer package. It contains much of the
tools necessary to implement cut. copy, paste.

******************************************************************

Tip 6

I am writing an applet that will use images. I would like to ship
out the images using a jar file that contains all the images that
the applet is going to use. I have seen a piece of code that
does that in the past, but I don't remember where.

A: The following is from:
http://developer.netscape.com/docs/technote/java/getresource/getresource.html

import java.applet.*;
import java.awt.*;
import java.io.*;

public class ResourceDemoApplet extends Applet {
Image m_image;

public void init() {
try {
InputStream in = getClass().getResourceAsStream("my.gif");
if (in == null) {
System.err.println("Image not found.");
return;
}
byte[] buffer = new byte[in.available()];
in.read(buffer);
m_image =
Toolkit.getDefaultToolkit().createImage(buffer);
} catch (java.io.IOException e) {
System.err.println("Unable to read image.");
e.printStackTrace();
}
}

public void paint(Graphics g) {
if (m_image == null)
return;

Dimension d = getSize();
g.drawImage(m_image, 0, 0, d.width,
d.height, Color.white, this);
}
}
by David Risner drisner@eskimos.com

******************************************************************

Tip 7

What is the essential difference between an abstract class and an
interface? What dictates the choice of one over the other?

A: You can only extend one class (abstract or not) whereas you
can always implement one or more interfaces.
Interfaces are Java's way to support multiple inheritances.

Please recommend us to your friends and colleagues!


The Java FAQ Daily Tips is a newsletter that is only sent to those
who have specifically subscribed to it.


John Andersson, Editor mailto:info@javafaq.nu

To subscribe to The Java FAQ Daily
print in subject field: "Subscribe" and
mailto:subscription@javafaq.nu

To unsubscribe print in subject field: "Unsubscribe"
mailto:subscription@javafaq.nu

You can find our tips on site later also!
Copyright (c) 2000 John Andersson
-------------------------------------------------------------------


 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy