ming fan

Greenhorn
+ Follow
since Nov 15, 2000
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by ming fan

Error: 500
Location: /carl/scwcd/scwcd_mock.jsp
Internal Servlet Error:
java.lang.NoClassDefFoundError
at com.javaranch.jspbeans.scwcd.QuizBean.(QuizBean.java:277)
at java.lang.Class.newInstance0(Native Method)
at java.lang.Class.newInstance(Class.java:237)
at java.beans.Beans.instantiate(Beans.java:207)
at java.beans.Beans.instantiate(Beans.java:51)
at carl.scwcd.scwcd_mock_1._jspService(scwcd_mock_1.java:109)
at org.apache.jasper.runtime.HttpJspBase.service(Unknown Source)
at javax.servlet.http.HttpServlet.service(HttpServlet.java)
at org.apache.tomcat.facade.ServletHandler.doService(Unknown Source)
at org.apache.tomcat.core.Handler.invoke(Unknown Source)
at org.apache.tomcat.core.Handler.service(Unknown Source)
at org.apache.tomcat.facade.ServletHandler.service(Unknown Source)
at org.apache.tomcat.core.ContextManager.internalService(Unknown Source)
at org.apache.tomcat.core.ContextManager.service(Unknown Source)
at org.apache.tomcat.modules.server.Http10Interceptor.processConnection(Unknown Source)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(Unknown Source)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(Unknown Source)
at java.lang.Thread.run(Thread.java:484)

What's wrong?
Hi,
I have a question regarding the deployment descriptor. I found such questions in the mock exams:
<web-app>
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>com.test.Test</servlet-class>
</servlet>
<listener>
<listener-class>com.test.Listener</listener-class>
</listener>
</web-app>
And the question is asking whether this is valid. I thought this is valid, but the answer says no because the order of <listener> and <servlet> is not correct (<listener> should be before <servler> ). I am not very familiar with XML, so I am not sure whether this order really matters (the DTD does show listener before servlet, but I still doubt). Does anyone know where in the SUN's specification that indicates this ordering specifically or this is just the rule in XML, whatever attribute appears first in the DTD for an element, it should appear in the XML document first for that element also.
Many thanks.
Ming
Question for Java Beans.
The contents of beans class are as follows:-
package projsp;
public class trying
{
private String name;
private int age;
public void setName(String n)
{
name=n;
}
public void setAge(int a)
{
age=a;
}

public int getAge()
{
return age;
}
public String getName()
{
return name;
}
}
The contents of test.jsp file are as follows:
<%@ page language="java" import="projsp.*" %>
<jsp:useBean id="test" class="projsp.trying" />
<html>
The Name is <getProperty name="test" property="name" />
The Age is < jsp:getProperty name="test" property="age" />
</html>
If a user types in http://servername:8080/test.jsp?name=Anand&age=22 the out put is

A The Name is Anand
The Age is 22
B The Name is Null
The Age is 0
C It gives a compilation error as the properties are not set
prior to retrieving them
D The Name is null
The Age is nul
The correct answer says D, but why not B? age is integer, so when not initialized, it will be 0 by default. Isn't it?
Add a mouse listener to the list, and in mouseClicked method detect for right mouse click, then decide whether should pop up the JPopupMenu or not.

Originally posted by Dave Donohoe:

Hi all - can anyone tell me the easiest way to add a Right Click Popup Menu onto the items in a JList?
Thanks,
D


18 years ago
I think you have several options:
1. Change your parsing code, so when it's parsing, it also appends whatever you parsed to the text area.
2. Do the parsing, when it's done and the file is created, trigger some kind of parsing done event and make your text area listen for that event so it knows when to read the file in.
3. After you start to parse, open a file input stream to the file you created and keeps reading and appends the input to the text area, but for this you can't close your input stream when you read the end of line (null in java input stream) since your read speed maybe faster then your parse speed, so you need some kind of customized end of line symbol to indicate the file is completed (like put "END" at the end of file, etc.)
I personally preferred the second way. Hope this helps.

Originally posted by wasif akbar:
The application I working on allows you to parse thru xml files. It looks for certain tags within the xmls and then writes out the value for the "Name" attribute of that tag to a file. On my gui, I have a textfield, a textarea and two buttons, one button to browse for the xml file and one called parse. When I parse, all of objects that I am parsing for get written to a file that is created. Basically what I want is for the user to be able view the contents of the file as it is being generated or as soon as it is generated in the textarea on my gui. What I don't want to do is create another button called "View File" and then when you click on it, the file gets opened in the textarea. I want the contents to be displayed in the textarea as soon as the file has been parsed or while it is being parsed.
Any help would be great. Sample code would be even better. The gui is mostly awt components except for the filechooser when I browse.


18 years ago
If you want to use HTTP to send file, you have to send it as a multipart request, Jason Hunter gave out some sample code on how to write a multipart request in his book Java Servlet Programming. You can make changes to his MultipartRequest class sample, and that should work.

Originally posted by tom tran:
Hi,
I am writing a Java app (thick app) and I'd like to send
files to a web server using HTTP protocol. If you know how
to do it, I would very much appreciate if you can tell me.
I already have an example of sending files using ftp but
what I want to use is HTTP since the requirement of communication
is HTTP.
Thanks in advance,
Tom Tran
trant@onr.navy.mil


18 years ago
Two things, one thing is in your init method, when you want it to run as stand alone program, you should say if (standalone) instead of if (!standalone).
The other thing is when you use it as an applicaiton, it won't get the code base for the applet since it doesn't have an applet context supplied, so maybe you should try to just passing the file url to the audio in the format like new URL("file:///c:/directory/1.wav").
Hope this helps.

Originally posted by Andrew Hoffman:
I can run this as an applet but not as an application. Can someone help make this program do either or?
import java.applet.*;
import java.awt.BorderLayout;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import Chapter8.MyFrameWithExitHandling;
public class Lab14_1 extends JApplet implements ActionListener
{
// Delcare Audio Files
static AudioClip playAudio;
protected AudioClip loopAudio;
protected AudioClip stopAudio;
// Declare Buttons
private JButton jplay = new JButton("PLAY");
private JButton jloop = new JButton("LOOP");
private JButton jstop = new JButton("STOP");
private boolean isStandalone = false;
public static void main(String [] args)
{
// Create a frame
MyFrameWithExitHandling frame = new MyFrameWithExitHandling(
"Lab14_1 Application");
// Crate an instance of the applet
Lab14_1 applet = new Lab14_1();
applet.isStandalone = true;
// Add the applet instance to the frame
frame.getContentPane().add(applet, BorderLayout.CENTER);
// Invoke init() and start()
applet.init();
applet.start();
// Display the frame
frame.pack();
frame.center();
frame.setVisible(true);
}
public void init()
{
if (!isStandalone)
{
JPanel p = new JPanel();
p.setLayout(new FlowLayout());
p.add(jplay);
p.add(jloop);
p.add(jstop);
getContentPane().add(p);
// Create audio clips for playAudio, loopAudio
playAudio = getAudioClip(getCodeBase(),
"Chapter14/Airplane.wav");
loopAudio = getAudioClip(getCodeBase(),
"Chapter14/Chimp.wav");
// Register Listeners
jplay.addActionListener(this);
jloop.addActionListener(this);
jstop.addActionListener(this);
}
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == jplay)
{
playAudio.play();
}
if (e.getSource() == jloop)
{
loopAudio.loop();
}
if (e.getSource() == jstop)
{
playAudio.stop();
loopAudio.stop();
}
}
}


18 years ago
Here's some sample code about how to use JTextPane:
JTextPane textPane = new JTextPane(new DefaultStyleDocument());
SimpleAttributeSet s = new SimpleAttributeSet();
StyleConstants.setFontFamily(s, "Times New Roman");
StyleConstants.setFontSize(s, 16);
StyleConstants.setBold(s, true);
StyleConstants.setForeground(s, Color.red);
textPane.getDocument().insertString(0, "hi, I am here\n", s);
Hope this helps.

Originally posted by anukampa malhi:
you are correct that i shd u use JTextPane but i m not able to get how can i actually use it. I have spend almost 3 days to work thru JTextPane but of no use. Can u please help me in same.
Thanx
Anukampa


18 years ago
If you don't want the title bar, use Window instead, but you can't set icon on that either.

Originally posted by Felix Devasia:
Hi,
Can anyone tell me how I could get rid of the default titlebar of the frame while running swing applications? Also, can I change the default icin of the "teacup", in the upperleft corner of the titlebar?
thanks..


18 years ago
Another way to handle that is to have your own local buffer image and draw on that first, then draw the image onto the graphics object passed in by the system.
code example:
/* These are your buffers */
Image buffer;
Graphics bufferGraphics;
public void paint(Graphics g)
{
Dimension d = getSize();
if (buffer == null)
{
buffer = createImage(d.width, d.height);
bufferGraphics = buffer.getGraphics();
}
bufferGraphics.drawLine(0, 0, 100, 100);
g.drawImage(buffer, 0, 0, this);
}
You can also use the bufferGraphics in other methods according to your needs.

Originally posted by Sergei Voropay:
I'm working on an applet derived from JApplet class. When running the applet in browser and scrolling the page in any direction, the applet flickers. Is there a way of getting rid of this annoying behavior?


18 years ago
I am not very sure what your exact problem is, but I am supposing that when you add your Account object to the list through some list modal, the info contained in the account object is not showing correctly. I think one solution can be overwrite the Account class's toString method, use that to return whatever info you want to show on the list, hope that helps your problem.
code sample:
public String toString()
{
return "Account: " + acctId + " Amount: $" + acctAmt;
}

Originally posted by James Lechte:
Hi all,
im trying to extend basic design of a console based system to a gui interface. I am having a problem with an array within an 'Account' class which stores details and amounts of accounts. The problem is: when i try and tie up the class which builds up the interface, panels, panes, container etc. to the 'Account' class it cannot pass the info properly. I have looked at numerous examples of swing and AWT but cannot see examples of pluggin in other classes!
please any advice would help!
Cheers James


18 years ago
I am not sure how you add this image to the JPanel, but I am supposing that you just paint this image on top of this panel. One way I think you can try is to set the preferred size and minimum size dynamically according to the image size you loaded, then call revalidate, and that should force the JScrollPane to scroll.
code sample:
public Dimension getMinimumSize()
{
return new Dimension(image.getWidth(panel), image.getHeight(panel));
}
in your load function, then call revalidate().
Hope this helps.

Originally posted by Skantha Kandiah:
Hi,
I am trying to create an application that can display different images in a single frame. The main frame contains a JScrollPane that contains a JPanel. Initally there is no image on the JPanel and there are no scrollbars on the scroll pane. When I add a large image to the JPanel the scrollbars don't appear on the scroll pane. What function do I need to call for the scroll bars to appear? I tried revalidate() and it did not work.
Thanks,
SK


18 years ago
I think you can try to capture the adjustment event generated by the scroll bar instead of mouse event, that should be the more preferred way to detect scroll bar status change.

Originally posted by rajesh purohit:
Hi
We have vertical and horizontal AWT scroll bars in out applet. We are trying to capture the mouse clicked and released events on that component. But the component generates only pressed, entered and exited events. But the right mouse button generates clicked and released events (weird!!). Is there any way to fix this sothat the left mouse button generates the clicked and released events?
Any help would be appreciated.
Thanks
Rajesh Purohit


18 years ago
Well, I have tried your code, and it works fine on my NT dos prompt. Which dos prompt are you using? Check the Process javadoc, it says the following:
The Runtime.exec methods may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Win32, or shell scripts.
If you are running win16/dos processes on win32, then that could give you some problem. Hope this helps.

Ming Fan
-----------------------------
Sun Certified Java Programmer
-----------------------------
18 years ago
Hi,
I would like to thank JavaRanch for the help they provided for me to pass the exam. Do the mock exams, and also do the Sun sample exam, they help a lot. You will see many similar questions on the exam.
The exam itself is not too hard, I didn't do well on Thread and Utililty though. Be careful on dead lock of thread, what stops thread, stuff like that. For Util, need to know the class hierarchy clearly, I didn't do well on that.
IO isn't too hard, constructors, how to chain stream onto each other (Filter stream, Buffer stream, etc.), also File, RandomAccessFile, what's the effect after using them, etc.
Inner class is a little tricky, need to know how to use constructor for anonymous inner class, actually if you remember all the characteristics for inner class, you should be fine.
AWT isn't hard, be careful on GridBagLayout though, you should know what this layout is for, and how it affects the positioning of components, read the JavaDoc on this class, that's pretty good.
All the rest isn't very hard. Wish you all good luck.
-- Java Rules --
18 years ago