JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I guess we all should have noticed that one!
ps your code is a long version of

public boolean isClicked(int a, int b) {
   return (a >= x) && (a <= x+width) && (b >= y) && (b <= y+height);
}

(I also rearranged the tests to make it clearer what they were doing, and to include clicks right on the boundary line)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I simply do not believe you. I googled "JMF documentation" which gave 318,000 hits; these were all on the first page...
http://www.oracle.com/technetwork/java/javase/index-141145.html
and this
http://www.javaworld.com/javaworld/jw-04-1997/jw-04-jmf.html
and this
http://www.codemiles.com/java/what-is-jmf-t628.html
... you get the idea

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Maybe a logic error in isClicked? print the params, and values in the loop, to check it out.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I'm sure we'll get there in the end.

I'm sure we will.
And in the process you are getting a really good grounding in good Java practice.

As for the current problem - use lots of print statements to confirm exactly what code is being executed and what data values are being calculated and stored. Debugging by just looking at the code is a fool's game.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Control Panel > Default Programs > Set Associations

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

how do you display the store that is clicked as a string

Store clickedStore= null;
for (int i = 0; i < Stores.length; i++) {
   if (Stores[i].isClicked(e.getX(), e.getY()) {
      clickedStore = Stores[i];
      break;
   }
}
String storeName = clickedStore.getName();

... or have I misunderstood your question?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can catapult this to a higher level by creating a class for the stores and their coordinates - something like:

class Store {
  String name;
  int x, y, width, height;
  int doorX. (etc)

Pass all the coords to the constructor.
Then have methods in the class like

public void drawStore(Graphics g) {
  // do the drawrects for this store and its door  here
}
public boolean isClicked(int x, int y) {
  // compare these coords with the stores coords, return true if inside
}

In the main program create an array of Stores, initialised with 3 new Stores (names, coordinates etc.
Now the code in you main routine collapses down to simple loops like

Store clickedStore= null;
for (int i = 0; i < Stores.length; i++) {
   if (Stores[i].isClicked(e.getX(), e.getY()) {
      clickedStore = Stores[i];
      break;
   }
}
...
public void paint(Graphics g) {
  super.paint(g);
  for (int i = 0; i < Stores.length; i++) {
    Stores[i].drawStore(g);
  }
  ...

For closest store you could do a variant on isClicked(...) to return how far the x,y coords are from the center of the door of the store. You can then use these in a simple loop to find which Store is closest.

It's a bit of an up-front effort, but it pays huge dividends as the app gets bigger & more complex. It's also the "right" object-oriented Java way to do things.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I'm sorry. I don't even know what that sentence is supposed to mean.

Ezzaral commented: You aren't alone in that. +14
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

There isn't a "Directory" class. Both files and directories are represented by the File class, so all you need is is an ArrayList<File>

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Suggest you think about extracting that code into an ordinary method then calling it (1) somewhere in your initialisation and (2) in the columnMarginChanged method

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Are you sure that columnMarginChanged in your TableColumnModelListener has actually been called before it drops thru into the problem code - or is txtAdminTextFields initialised somewhere else?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You're confusing the array and the index. In the original code:

if(scan != null) {
while(scan.hasNext()) {
String word = scan.next();
array[index] = word;
++index; // this is also a count of the number of elements filled!
}
}
return array;

just replace the return statement with something like

return Arrays.copyOf(array, index); // returns a new array containing just the first "index" elements of "array"
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I can't say that you have no other problems in your code ;-) but the setVisible thing is pretty standard.

You are right, one class in your package can have the main method that would set the ball running for the entire program. But that doesn't mean that you can't have multiple mains in multiple classes, as long as you pick one of them to be the one that's invoked when you execute the program (by picking the class that you execute).

The main in NewContact is an example of another good standard practice. It is a minimal piece of code to test and/or demonstrate what that particular class does. In this case, because it's a window, it creates one and makes it visible. This is a really good idea because you can check out that class's code before trying to integrate it into the rest of the system. Also, if you change it later, you can check out your changes without running the whole app.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Line 24 you split on " " but I suggested split on the commas "," so try that.
Your file should NOT have any { or }

If you get an error you must ALWAYS post the full message, including the line number where it happened.

I'm sticking with this because you do seem to be trying, I'm hoping that when you get this working you will then understand the things that are currently blocking your progress, but Masijade has a good point.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Keep debugging. That example is the right solution. Put lots of print statements in your code so you can see what it's doing.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Two ways to go:
1. Define the whole ActionListener in the second class but add it in the first
myBotton.addActionListener(new OtherClass.OtherActionListener());
2. Define the ActionListener in the first class in the usual way but in the actionPerformed method just call a method in the second class

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

getData is an instance method, so you call it via an instance of the Manager class (eg "first"). You have called it via the name of the class, not an instance. You can only do that for static methods.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It's a mistake. It should read
byte[] b=new byte[255];
byte is a numeric type - an 8 bit integer. This declares an array of 255 bytes.
Because b was incorrectly declared, the methods that have it as a parameter also don't compile. The method that was supposed to be used was

public int read(byte[] b, int off, int len) throws IOException
Reads up to len bytes of data from the input stream into an array of bytes.

If you fix the declaration the read methods should be OK.

Incidentally the use of l as a variable name, especially for a number, is sheer lunacy. Line 14 is l (ie the variable) -2, but it looks like 1 (the number) -2, and baffles everybody. The color-coded syntax in the Daniweb code tags is the only thing making this obvious.

Xufyan commented: thanksalot :) +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

1. The x,y,z on line 23 are the parameters passed into that constructor (line 22). Their values are whatever the calling code passed as parameters when it called this constructor.

I answered before you added the second q, so I didn't see it.
The compiler knows which constructor to use because they all have different numbers and/or types of parameters.
new sphere(4.0,1,1,1) has 4 parameters so it is a call to the constructor on line 22 because that's the one with exactly 4 parameters.
this(x,y,z) refers to the constructor on line 14 because that's the one with 3 parameters.

Xufyan commented: ++++++++= +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It's so easy to fall into that kind of trap, we've all done it. One assumes, without even explicitly thinking about it, that X is necessary, so you always use it, and never discover that it's not.

The official tutorial on GridBagLayout is a good place to start. Once you've used it a couple of times you build up a little collection of examples that you can just copy/paste in future projects. IMHO its the only layout manager to use if you're serious about GUI appearance.
http://download.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

new ImageIcon(String fileName) is a real pet hate of mine. Throws no exceptions, just silently returns a null. Then JLabel silently accepts null as an image parameter (ie "no image"), and hew presto! no image, and you have zero diagnostics.
Why oh why doesn't it throw a file not found exception?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

"conceptually" I would stick with thinking of them as static final variables containing references to the different enum instances. Its only constant-specific methods that don't fit the simple summary. They are generated by the equivalent of something like this:
enum Month{ JAN(31), etc
is like:
enum Month {
public static Month JAN = new Month(31);
etc

I'm away now till tomorrow. Good luck.

NewOrder commented: okay, thanks james +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

One small suggestion:
This code will crash and burn horribly if presented with parameters like (3,2) or, more subtly, (-2,-3).
A simple replacement of the == on line 4 with a >= will protect against these nastys, as will the even simpler replacement of lines 4,5 with
if (n1>n2) return 0;

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In order to be able to sort instances of some class (such as Scores) you need to be able the compare two Scores to see what order to sort them in. Scores is a class you define, so there's no sensible guess that Java can make about what order you want. To fix this you must implement the Comparable interface in your class. This consists of only 1 method to compare two Scores and say which sorts first. Have a look at this:
http://onjava.com/pub/a/onjava/2003/03/12/java_comp.html

Kerrai commented: Greatly helpful post +1
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

i searched over google but didn't find the suitable answer...!

The technical OO terminology is "accessors and mutators" - googling these will give you lots of good info.

Here's a good article to start with:

http://java.about.com/od/workingwithobjects/a/accessormutator.htm

Xufyan commented: thanks - i didn't knew about accessors and mutators ! :D +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Can you suggest some smaller problems that help me to be familiar with this class so that i can implement this in my project.

Firstly, that's the right question.

So, create a small graphic image file (use any paint program you like),, maybe 10x10 pixels, with a small block of white, one of black, a bit each of R, G and B. Save it as a bmp, not a jpeg so it says intact when saved.
This test file is small and simple enough for you to be able to check that you are getting the right results.
Now write a simple class with a main method that just reads the file into an Image, pixel grabs it, and prints the RGB values to System.out.
This is as simple to write and test as any pixel app can possibly be. If you find this too much at your current level of expertise, then set aside any thoughts of pixels for the moment and build up your basic Java skills with some simpler programs. If you look in this board you will find lots of students posting problems around learning exercises that you could try for yourself.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

19. Because the user of this class doesn't need to know about it, and if they did get access to it they would probably screw up the list.
28. When the return statement is executed
32. This stops execution of the method immediately and returns to the wherever the method was called from. This therefore makes it exit the loop.
57. Because its the recursive implementation for the public print() method. Users should call print(), but how that works is private to this class. If they had access to the print(Node node) method who knows what damage they may do?

NewOrder commented: Thank you for clearing this out for me +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Separate the mortgage logic & calculations from the user interface. Put them in their own class, with its own variables, and provide public methods that the user interface can call to set the variables and get the calculated mortgage figures.
Now you have 2 classes; put each one in its own file.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Is line 2 part of a method? (it should be)

the dreaded serialVersionUID is because the API says any Serializable class should declare a version number so that if serialization changes you will know which version was serialised when you try to de-serialise it. Eclipse is just picking up every low-level warning that a default stand-alone compile may suppress. Just let it put the version variable in and don't worry.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Sorry - no time now. I was wrong - that IS an anonynmous inner calss. I;ll post more in th emorning.

NewOrder commented: thanks for helping +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can use anything clickable (JButton, JLabel etc) and respond to the click by opening the URL.
Or, if you want clickable links embedded in the text, use a JEditorPane in which you can display HTML and handle hyperlinks via a HyperlinkListener.
Have a look at this then head on over to the API doc for the full details

peter_budo commented: Good resource +15
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I think you have misunderstood the purpose of this forum. We are here to help you do your homework, not to do it for you.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Have a look at Apache "Tika" http://tika.apache.org/
It may be a bit over the top for what you need now, but it's probably the comprehensive file metadata utility you will find in Java.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It is valid code.
Class B will inherit v and String sayHello(), but nothing in the code makes use of that.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

share me a lick please.. so i can do it.

http://www.google.com

tux4life commented: Yeah! +8
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

nevermind

NewOrder commented: thanks James +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, or:

sessions = new collection of Dates
for each vote
  if sessions does not contain this vote's session date 
      add session to sessions

This kind of in-memory processing should be safe and v v fast for thousands of votes, but maybe not millions.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Once you've read this in its easy to loop thru all the entries & build an arraylist of unique session dates - which you can then display in an list box for user selection.
What kind of data volumes are you expecting?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
for (a = 0; a <9; a++) {
   for (b = 0; b < 9; b++){

Am I the only one who plays chess on an 8x8 board?

tux4life commented: Of course not, there's still me :P +8
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Unless there are privacy issues, maybe its a good idea to keep all the data instead of a summary, ie for each vote cast add a record to the list showing who voted/when voted/what voted for. That way each record is unique and never updated. Then derive the summary statistics on the fly or (if that turns out to be slow) build them when you read the data at startup and increment it as you go, but don't save it.
If multiple clients can increment a set of shared counters then you also have to get very serious about Thread issues.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Maybe time to mark this as solved and start a new thread for connection issues if required?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I can't see where you add those fields to anything.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, open an output file and it overwrites the previous.
Try opening the fileoutput stream in append mode, eg
new FileOutputStream(fileName, true)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

No, just in the Java VM's memory. But they can be written & read in a single method, so its no big deal.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If you get all your results into some object (eg an ArrayList<Result>) then it's very easy to write/read the whole object to/from a file in a single staement using ObjectOutputStream/ObjectInputStream. That's probably the easiest way if you don't need to do record-based I/O

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Presumably you are not passing any args to the main method.
In Eclipse you can do this in the Run Configurations dialog, in the tab labelled (x)= Arguments

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Its a special method that is called when a new instance of a class is created. It's used to initialise anything that needs to be initialised for a new instance. Every class needs at least one, and if you don't supply one the compiler gives you a "default" constructor that does nothing. You can define constructors with different parameters, depending on how you want to initialise your new instance.

Megha SR commented: very useful info +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Its not common to see one package per class like that. Normally you would have a package, say "chess", with all those classes in it. That way you can make best use of the different scopes (private/protected/public etc) to share the things you want to share between your classes while keeping them secure from other classes that are not in your package..
To "move" an object reference from one place to another in an array you need to
1. copy the obejct reference from the old position to the new position, then
2. Remove the reference in the original position (eg overwrite it with "null")

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

is that how it works,
1) create a reference to the interface (reciever).
2) use a constructor to allocate a reference (r) and make it equal to the object reference,
3)then call the interface by a method (sendText), which would put an argument/parameter in it
4)implement teh interface on a class(TickerTape).

Not really. Read norm's first post again.
This is a better version of your post:
1. create a reference variable that holds references to objects that implement the interface
2. Use a constructor to create an instance of a class that implements the interface
3. Call the sendText method on that object. The compiler knows that's ok because the class implements the interface and the interface defines that method.

Think of it as if the interface only exists at compile time. It tells the compiler what methods your object has, but at runtime the calls are always to actual methods in an actual object, not to the interface.

It's useful becuase you may have many different classes that share something in common and the interface is a way of defining that. Eg. The Java API defines an interface List which defines methods for adding, removing, retrieving (etc) items from a list of items. There are many classes in ther API that can hold a list of items (ArrayList, Vector, LinkedList, Stack etc), but they all implement, in their own different ways, the methods of the List interface. That means …

NewOrder commented: THANKS . i saved your comment on a word file +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I knew I'd seen this before...

Autoboxing uses Integer.valueOf, which internally caches Integer objects for small integers (by default -128 to 127, but the max value can be configured with the "java.lang.Integer.IntegerCache.high" property - see the source code of Integer.valueOf), so it is different from calling new Integer directly.

BestJewSinceJC commented: good post +5
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

On which line (using the version you posted in your first post)?