Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!

Marlon Churchill

Greenhorn
+ Follow
since May 08, 2011
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 Marlon Churchill

Rob Spoor wrote:

Marlon Churchill wrote:I suppose I could align components a bit better, but its still better than what GridBagLayout would give.


Then you haven't used GridBagLayout correctly yet. It can be used to create that UI just fine, with a few extra JPanels perhaps. I'm not saying you should use it though; I've used JGoodies Forms for years because I didn't like GridBagLayout.




You are correct. I put in some inner JPanels, and used the GridLayout manager in them instead of FlowLayout.

Also drew up a sketch and found some mistakes on my GridBagConstraints.

I think I might try MigLayout next still. Thanks to Adam for the tip.

Thanks to all who replied.

Thanks to Rob for pushing me to look again at my GridBag code.
8 years ago
Here is a jpg file for the panel I wouldlike to create.

I suppose I could align components a bit better, but its still better than what GridBagLayout would give.

I will take a look at MigLayout.

8 years ago
Here is a sample code: In the designer everything is aligned and looks nice. When I run it, the textFieldInitial is not aligned with the textFieldBounday. The line border of timeOptions overlaps the textfields contained within it.

Main Class


The frame:


and finally TestPanel:



8 years ago

Rob Spoor wrote:This is one of the reasons why I never let any GUI builder create my GUI for me, and I still do it manually like you used to do.



I just got tired of GridBagLayout not aligning components, or rearranging the whole panel in a jumble for some unknown reason.

Figuring out Eclipse has been actually more work than hand coding. WindowBuilder is temperamental and slow to work with.

8 years ago
Hi,

I have recently switched from GridBagLayout and hand coding in notepad++ to using Eclipse with the WindowsBuilder. My Panel looks fine when I create it but when I run the program textfields shift subtly and components are not aligned.

Is there some secret to actually get the components you layout? I have been using the GroupLayout.

Or will I have to post an example with code?



8 years ago
Hi,

Sorry for the late reply. Thanks to both of you for your assistance.

@Joe - The questions on the SJCP are deliberately obfuscatory and use poor programming practices to enhance difficulty. Or so the Sierra and Bates SJCP books states.

@Ron - diagramming it out helped a lot. Unfortunately, I have another question about question 10 of the same chapter. Here is the code

The question asks which are true about ojbects created within main and eligible for garbage collection at line 14 (the comment line)

The correct answers are 5 objects created and two eligible for GC at line 14. I can figure out the GC part.

So we have a dozens array da. At first I thought it created a Dozens object in each element of the array when da is created, but I don't think it does. only at line
da[0]= new Dozens();

is the Dozens object in da[0] created. A int array da[0].dz (also an object) is also created. Is this correct?

then Dozens d= new Dozens (); creates another two objects. I think I get it.

Sorry for the questions. I worked my way through many of the sample problems in the Murach Java 6 book and thought I had a good handle on Java, but these questions are very detailed.
9 years ago
This question is about how Java creates objects and when. I think I'm missing some basic concept. the code and question are from the Sierra and Bates SCJP 6 book, Chapter 3, question 11.


Given

When line 16 is reached, how many objects are available for garbage collection?

The correct answer is 1, but I seem to count 3. b1, b2, and a1.

Backing up a bit, how many objects were created? I'm not really sure.
Is it seven? a1, a1.b1, a1.b2, b1, b1, a2, and a2.b2 ? Since Alpha.b1 is static, I think there is no separate a2.b1 object, the static Beta b1 belongs to the class Alpha.
or is it four? a1, b1, b2 and a2. The variables in the Alpha class are reference variables, not objects.

still, setting b1 and b2 both equal to null, seems to make them eligible for GC.

Does setting a2.b2 = b2 mean the code can still reach b2, so b2 (although null) is not eligible for GC?

As you can see, I'm a little confused.
9 years ago

Rob Spoor wrote:Try the following:
Options go before the source file(s).
By using -cp the current directory is no longer automatically part of the class path. You need to add it manually. That's what the extra ".;" is for.

Also, in both examples: either use .\jr.jar or just jr.jar. A slash (/) doesn't work within the Windows command prompt.



Thanks! The option -cp .;jr.jar worked! Didn't know that about the / in windows - thought they were interchangeable for some reason.
9 years ago
Hi,

I have a class that uses packages inside a jar file.

The class compiles but when I try to run java I get the error message ClassNotFoundException.

Here are the commands:

C:\Users\Marlon\workspace\javacow>javac DaysOld.java -cp ./jr.jar

C:\Users\Marlon\workspace\javacow>java DaysOld 2007/3/17
Exception in thread "main" java.lang.NoClassDefFoundError: com/javaranch/common/
GDate
at DaysOld.main(DaysOld.java:9)
Caused by: java.lang.ClassNotFoundException: com.javaranch.common.GDate
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more

C:\Users\Marlon\workspace\javacow>java -cp ./jr.jar DaysOld 2007/3/17
Exception in thread "main" java.lang.NoClassDefFoundError: DaysOld
Caused by: java.lang.ClassNotFoundException: DaysOld
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: DaysOld. Program will exit.



In the first command it seems java can't find the GDate class, and in the second example java can't find the DaysOld class. It seems odd it can compile but can't run. DaysOld and jr.jar are in the javacow directory.
9 years ago
oh yes, and just using the command

javac Apple.java

or even

javac Apple.java food/Fruit.java

Leaving of the -cp food helped. Thanks for the tip on where the compiler is looking for packages.

Thanks to all who responded.
9 years ago
Hi,

Yes, adding public did it. Thought I'd tried that and it didn't work.

Maybe I was thinking of methods in interfaces that don't require the public modifier.
9 years ago
Hi,

I'm working through Exercise 1.1 of the Sierra and Bates SJCP book. I have an abstract file Fruit.java:


and the class Apple :


Apple.java is in directory Exercise 1.1 and Fruit.java is in Exercise 1.1/food

I try various attempts at compiling and get:
C:\Users\Marlon\workspace\Exercise 1.1>javac Apple.java food\Fruit.java -cp food

Apple.java:1: food.Fruit is not public in food; cannot be accessed from outside
package
import food.Fruit;
^
food\Fruit.java:2: duplicate class: food.Fruit
abstract class Fruit
^
Apple.java:2: cannot access Fruit
bad class file: food\Fruit.java
file does not contain class Fruit
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
class Apple extends Fruit {
^

C:\Users\Marlon\workspace\Exercise 1.1>javac Apple.java -cp food
Apple.java:1: package food does not exist
import food.Fruit;
^
Apple.java:2: cannot access Fruit
bad class file: food\Fruit.java
file does not contain class Fruit
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
class Apple extends Fruit {

I'm not sure what is going on. Fruit is abstract so that implies public, correct? I have tried adding public explicitly and it didn't make a difference.
9 years ago