Should this work?

Here's part of my code:

import java.awt.*

Panel bottomPanel = new Panel(new CardLayout(10,10));

Panel nullPanel = new Panel(new FlowLayout());

Panel messagePanel = new Panel(new FlowLayout());

Panel choicePanel = new Panel(new GridLayout(3,1,10,10));

Label messageBox = new Label();

Button ok = new Button("OK");

Button cancel = new Button("Cancel");

messagePanel.add(messageBox);

choicePanel.add(messageBox);

choicePanel.add(ok);

choicePanel.add(cancel);

bottomPanel.addLayoutComponent(nullPanel,"nullPanel");

bottomPanel.addLayoutComponent(messagePanel,"messagePanel");

bottomPanel.addLayoutComponent(choicePanel,"choicePanel");

My question is about addLayoutComponent(Component comp, Object obj). Am I using it correctly? It says in the docs that obj must be a string. I currenly get a compiler error:

cannot resolve symbol

symbol :

method addLayoutComponentjava.awt.Panel,java.lang.String)

location: class java.awt.Panel

What am i doing wrong?

[1097 byte] By [mwstein] at [2007-9-26 3:32:24]
# 1
it has to be an string object, not just a string. Try String whatever = new String("nullPanel");thenbottomPanel.addLayoutComponent(nullPanel,whatever);see if that works.
gwgeller at 2007-6-29 11:59:53 > top of Java-index,Archived Forums,Java Programming...
# 2
addLayoutComponent is a method of the layout, not the component. You should use something like bottomPanel.add(nullPanel,"nullPanel"); or bottomPanel.getLayout().addLayoutComponent("nullPanel", nullPanel);
jsalonen at 2007-6-29 11:59:53 > top of Java-index,Archived Forums,Java Programming...
# 3

I think gw is right. I've seen this before. I think it is kind of weird. Most of the time you can call functions like this:

method("test");

but sometimes you have to this:

method(new String("test"));

If someone could explain this to me (schapel?) I would be interested to know.

dubwai at 2007-6-29 11:59:53 > top of Java-index,Archived Forums,Java Programming...
# 4
No, gw is not right. A String is a String is a String. Unless you're doing something very tricky, you never need to write anything like new String("test"). Basically, when I see new String(...) in a Java program, it's a flag that the programmer doesn't know much about Java.
schapel at 2007-6-29 11:59:53 > top of Java-index,Archived Forums,Java Programming...
# 5

> No, gw is not right. A String is a String is a String.

> Unless you're doing something very tricky, you never

> need to write anything like new String("test").

> Basically, when I see new String(...) in a Java

> program, it's a flag that the programmer doesn't know

> much about Java.

I agree gw wasn't right here. I am positive, however, that I have had to do this in rare cases to get the code to compile. Compiler glitch?

dubwai at 2007-6-29 11:59:53 > top of Java-index,Archived Forums,Java Programming...
# 6
Post one of the cases (both the non-working version and the version with new String() that does work), and tell us what compiler you used. Then we can determine what the problem was...
schapel at 2007-6-29 11:59:53 > top of Java-index,Archived Forums,Java Programming...
# 7

> Post one of the cases (both the non-working version

> and the version with new String() that does work), and

> tell us what compiler you used. Then we can determine

> what the problem was...

I don't remember where it occurred. I've seen it and another member of my team has seen the same thing. It really wasn't a big deal at the time. We just worked around it (deadlines and everything.)

I just mentioned it because I was wondering if it was an error or if there was a good reason for it. It sounds like what you are saying is that shouldn't happen. It may have been the development environment we are using. I think it does some precompile tinkering with the code and it isn't the most stable application.

dubwai at 2007-6-29 11:59:53 > top of Java-index,Archived Forums,Java Programming...
# 8

It could be that simply making any change, such as adding a space, would have make the program suddenly compile.

The reason is that dependancy analysis for Java is far more complicated than for C. In C and C++, the compiler simply looks at the #include files and includes them, and dependancy analysis tools make the .c and .cpp files depend on the included .h files.

In Java, you need not use import or include to reference things in other source files. The fact that Java is dynamically linked adds to the problem, because you can use reflection or other schemes to decide which classes to use at runtime. The only way to guarantee that Java code will run correctly is to compile every source file every time you compile.

It could be that in the cases you remember, the compiler needed to recompile a particular file in order to compile the program properly. By changing "test" to new String("test"), you make the compiler recompile the source file, and suddenly the compile worked.

I recommend against using dependancy analysis tools in Java for this reason. Just recompile everything each time. If you use a newer version of javac and give it all the files to compile on one line, it's very fast. The free compiler jikes is faster still. If your program starts getting so big that it takes too long to compile with jikes, then you might think compiling one package at a time.

schapel at 2007-6-29 11:59:53 > top of Java-index,Archived Forums,Java Programming...
# 9
Now that's the answer I was looking for.Don't you ever get tired of being right all the time?
dubwai at 2007-6-29 11:59:53 > top of Java-index,Archived Forums,Java Programming...
# 10

What version of the JDK are you using? I didn't recognize the method so checked My Java2 API's... which don't indicate that that method even exists!

If it doesn't exist, that could easily cause the problem that you're seeing!

The closest thing in Java2 is

add( Component comp, Object constraints)

which doesn't indicate very clearly what 'Object' should be. However, my guess would be that it requires a specific String, such as java.awt.BorderLayout.NORTH to indicate where it goes.

--David

sage_sam at 2007-6-29 11:59:53 > top of Java-index,Archived Forums,Java Programming...