Archive for the ‘ Frequently Asked Questions ’ Category

Question by James: How to open Java applet in browser instead of new window on Macbook?
Hey

Sometimes when I use Java things online, instead of the java running in the browser window, it opens a new window called ‘Java Applet’. This has happened in both safari and google chrome. I was wondering how I could run it in the window. Java is enabled.

Best answer:

Answer by Alice
yes.

What do you think? Answer below!

Question by Alex: Using semaphores in Java? How can I make threads run at different frequencies to one another?
I have three different threads running in my Java program. One prints “1″, another “2″, and the last “3″. I need to make the “1″s and “2″s print less frequently than the “3″, using semaphores, but I’m very stuck. Any ideas?

Best answer:

Answer by McFate
You could wait on one semaphone between all three threads, and then:

(1) Print “3″ every time you get the semaphore in the “3″ thread
(2) Print “1″ only every second or third time you get the semaphore in the “1″ thread

Add your own answer in the comments!

Question by ??: How to make a Java application display a file chooser and allow users to choose a file ?
How to make a Java application display a file chooser and allow users to choose a file ?

Many thanks!

Best answer:

Answer by McFate
Java tutorial on File Choosers, with examples:

http://download.oracle.com/javase/tutorial/uiswing/components/filechooser.html

What do you think? Answer below!

How to compare strings of characters in Java?

Question by Ashley Johnson: How to compare strings of characters in Java?
I am new at Java and at a lost, I need to compare two strings of characters in java to find which one comes first in the alphabet and don’t know where to start. Any suggestions where I can start? Any and all help would be appreciated.
Thank you so much.
Also can you have more than one compareTo in a program? I need it for two methods but the first methods results will override the second even if I do not initiate the first method.

Best answer:

Answer by michaeljhuman
Try str.compareTo().

I don’t think == works, as that compares the object reference, not the string contents.

Know better? Leave your own answer in the comments!

Question by Steven: In Java: How can I make a Timer based repaint, instead repaint as fast as the processor can handle?
I have a Timer, which repaints every 30 milliseconds. It runs a simulation, and I want it to run the simulation as fast as possible. I know I can make the timer refresh at a quicker rate, however Java cuts off functions that did not complete in time. So i would simply like to run it at capacity.

Best answer:

Answer by Jared
repaint means you’re doing an animation, you should run an animation without some sort of a timer.

I would suggest using Threads. Here’s a basic animation thread

public class MyPanel extends JPanel implements Runnable{

//the only method you need for runnable
public void run(){
while(true){
repaint();
try{
Thread.sleep(sleepTime);
}
}
}

}

So you define sleepTime in milliseconds. So if you want 30 frames per second, then you do:

1000 / 30 ~ 33 milliseconds

If you don’t want ANY sleep time (which is what you described), then just take out the sleep, i.e. just have a loop that calls repaint() without waiting on anything.

If you’re running some sort of simulation (like say gravity or something), then you should separate the panel from the simulation.

Basically, you should have another class for .the simulation (which also implements runnable and looks like an animation). The class should hold the position and shape (if applicable) of all of its members.

Then pass this simulation object into the animation panel, then during repaint (paintComponent(Graphics g) really), just paint the objects based on where they are now.

So you should have 2 threads, one running the animation and one running the simulation, this will, theoretically, run concurrently. Then you can make the simulation faster by either lowering the sleep time or eliminating it altogether, but your animation should never be more than around 60 frames per second.

What do you think? Answer below!

Question by Othella Shao: What is the standard way to instantiate a Java Bean?
We can treat a Java Bean as a common Java object, and instantiate it by Java key word – new. But Java Bean is different from common Java object. So what is the standard way to instantiate a Java Bean?

Best answer:

Answer by Otis Shou
a example is below:
try {
MyBean bean = (MyBean)Beans.instantiate(ClassLoader.getSystemClassLoader(), “MyBean”);
} catch (ClassNotFoundException e) {
} catch (IOException e) {
}

Give your answer to this question below!

JAVA??????????????????

Question by Nick Williams: JAVA??????????????????
Ok…. i know javascript, html, css, xml, but i have no fricken clue how to do java, i had tried to figure out… but i dont get it… if you could give me a tutorial of everything i need that would be great….

please use specific details(:

Best answer:

Answer by Dsp Team
JAVA!!!!!!!!!!!!!!!!!!

Add your own answer in the comments!

How does Java take in values from person?

Question by pencilwars: How does Java take in values from person?
I know that C++ takes in values from a person typing on a keyboard with cin for numerical data and getline(cin, variable) for strings, but how does Java take in values? I mean, what is Java’s equivalent of cin, and how do I put that value into a variable? Thanks!

Best answer:

Answer by radishdominator
Java uses InputStreams and OutputStreams. These are essentially low-level wrappers around a stream of data that you can read and write to. You can add wrappers around that for various functionality.

You want something like:
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line = in.readLine();

Note that readLine() can return null on an end of stream and throw an IOException if an error occurs.

Add your own answer in the comments!

Question by Yashpal Singh: What facility does java enabled mobile phones provide to its users?
What facility does java enabled mobile phones provide to its users? Does it allow to watch and download videos from internet. What specification should a phone have to allow watching and download of videos from internet.Name few models in the range of Rs.2500.00 to Rs.3500.00 for watching and downloading videos.

Best answer:

Answer by Endang Artanti
endang.artanti@yahoo.com

Give your answer to this question below!

Question by miichan: What should I do to be able to compile java in command prompt?
For command prompt to be able to compile java, we need jdk, right?
But after i installed jdk, the command prompt still can’t recognize the java compiler command.
It says, ‘javac’ is not recognized as an internal or external command, operable program or batch file.
What should i do so that command prompt can read the java compiler?

Best answer:

Answer by neal0on14
Read this page:

http://www.cs.colostate.edu/helpdocs/JavaInDOS.html

Give your answer to this question below!