BestJewSinceJC 700 Posting Maven

I'd look through the link you provided, but at this point, I'm still wrapping my head around some of the MVC stuff. I understand the basic definition, and what each part (M-V-C) is supposed to do, but the implementations are still difficult for me right now. For example, on those leepoint notes I linked to, where he does an example of MVC with an observer, I understand exactly how he did it. But it doesn't make sense for me to make 100 inner classes and do that for every button I have. And one more question:

Essentially, the difference between the method you showed me in this thread, and the method in the leepoint link I posted, is that in yours, the View responds to a change in the model's data, whereas in the leepoint, the Controller responds to a request by the View that needs the data from the model. Right?

BestJewSinceJC 700 Posting Maven

Yeah, there are a lot of ways to do this. . you already have all the pieces to do it in front of you, you just need to put them together. Consider the following:

1. You already figured out how to get the countryCode.
2. Now you need to get the area code and "local", whatever that is.
3. You know the index that the area code starts at. That index is phoneNumber.indexOf('-') + 1.
4. You know the index that the area code ends at. That index, which you already calculated, is the variable "sep" in the code you posted, minus 1.
5. Use the substring method and the index the area code starts and ends at to get the area code.
6. The method to get the length of a String is length(). Since you know the index the area code ends at, (sep), sep + 1 is the first index of "local". The last index of local is the last index of the String, which is phoneNumber.length()-1. Minus one because the size of the string might be 10, but it will be indexed by 0-9.

good luck

BestJewSinceJC 700 Posting Maven

I understand how your example works, but it seems like that technique works only when you want to update everything in the GUI all at the same time. Lets say I want to modify that code because my GUI has probably 20 windows in total. And lets say that if something in the model is changed which affects a particular window, I want that window to update, but I do not want everything else to update as well. How would I modify your code so that this could work?

Edit:

Well, I guess I could just have something like the following in the Model class:

class Model{
private Dog dog;
private Cat cat;
private Person person;

List<ModelListener> dogListeners= new ArrayList<DogListener>();
List<ModelListener> catListeners = new ArrayList<CatListener>();
List<ModelListener> personListeners = new ArrayList<PersonListener>();


public void addDogListener(DogListener listener) {
dogListeners.add(listener);
}

//Two more methods, one for adding cat listeners and one for person listeners.

public void fireDogsChanged(){
for (DogListener d: dogListeners)
d.stuffChanged();
}

//Two more methods to do the same as above for cats and people.

}
BestJewSinceJC 700 Posting Maven

It was a joke, relax. But if you really want to learn, you'll take the suggestions you've already been given, learn how to code those individual pieces, then put them together.

BestJewSinceJC 700 Posting Maven

char dash = phoneNumber.charAt(1);

Why do you seem to think the character at that index is 1? That will only be the case if you enter it in the form 1-whatever. Since you determined that your '-' is at phoneNumber.indexOf("-"), the next part of your phoneNumber is going to be at phoneNumber.indexOf("-") + 1.

BestJewSinceJC 700 Posting Maven

P.S.

The reason for all these questions is because I'm currently doing a project using a three tiered architecture. I know how to make the GUI and the data stay correctly (so that the info on the GUI = info in the model) updated at all times, however, I'm researching these things to find the best way to do this. (i.e., the way that would require the least future maintenance, and the way that best separates the tiers into parts)

BestJewSinceJC 700 Posting Maven

Question directed at JamesCherill specifically, (and anyone else who can answer it)

I've never had too much trouble with communicating between the model and the view, in an application that only has a model and a view. However, looking at the observer pattern (at the bottom) of this link, I am somewhat confused. What does it mean for the view to "register a listener with the model"? I thought it meant the pattern that is shown in the example here, but the guy's notes say he hasn't provided an example of it yet, so obviously I am wrong.

BestJewSinceJC 700 Posting Maven

You could start by learning java. :)

BestJewSinceJC 700 Posting Maven

You can use the removeElementAt method to remove the last item. To know where the last item is, your program can either keep track of the size of the Vector, or you can use the Vector class's size() method. http://java.sun.com/javase/6/docs/api/java/util/Vector.html

BestJewSinceJC 700 Posting Maven

The Javadocs are your best friend. Here is the Javadoc for the Scanner class. You'll notice that the next() method does not take any arguments (what goes inbetween the parentheses). (This means that your code, where you have String checkDate = stdin.next(""), should say String checkDate = stdin.next(). Look at the Javadoc to see why - as you can see in the Javadoc, the Scanner class's next() method does not have any arguments.

A little further down, you have the line String name.trim();. This will result in a compiler error, because the syntax (wording) of that statement is not allowed. String is a type of Object. You can create new variables of that type of Object by saying String myString = new String("this is what the string will say!");. What you have done by saying String name.trim(); is told the compiler what type of Object you would like to create (String), but you did not give the String a variable name. Also, what your friend was trying to point you towards can be found here. You will notice, if you read the trim() method, that it removes whitespace from a String. So, in the earlier example, if someone entered " 8/14/1988 ", the String's trim method would return "8/14/1988".

Example of trimming white space from a String:

//Create a new string
String myString = new String("  Unwanted white space!!  ");
//Remove the white space from the String by using the String's trim …
BestJewSinceJC 700 Posting Maven

If you were to make this Friend into a Java class, would it implement Professor?

BestJewSinceJC 700 Posting Maven

Yeah, there is a difference between finding it by accident and looking for it. Although I have heard not to mix the two before (perhaps on this forum?) it is hardly ingrained in my mind, therefore I probably never would have seen it.

BestJewSinceJC 700 Posting Maven

nightGoddess:

1. Prompt the user for the date, and read it into a String.
2. Break the String into a character array. Example:

char[] theDate = yourString.toCharArray();
// If your String was 8/14/88, this char array will contain the following: [8][/][1][4][/][8][8]

3. Check the character array to make sure it matches the format you expected. For example,

//This assumes you have a char array (from above) called theDate
boolean isItADigit = Character.isDigit(theDate[0]);
//theDate[0] looks at the first index of theDate, which happens to be "8" (from above), which means it is a digit, so that method returns true.

Hope that helps.

BestJewSinceJC 700 Posting Maven

wow, Ezzaral, that is a pretty amazing catch. I was going to fool with the code a little more tonight, but. . unnecessary now. Doubt I would have ever noticed that anyway.

BestJewSinceJC 700 Posting Maven

It's painful to try to separate the view from the controller in a typical Swing UI. In practice it almost always compacts down to a UI / Business Objects(s) 2-layer model.
J

It seems to be practically the same thing with three tiered architecture. At least for small applications.

BestJewSinceJC 700 Posting Maven

Its hard to point out any design flaws without seeing your application. If you want to post what you've got again, I'll read through it tomorrow (5 a.m. here, going to sleep). But you definitely do not have to apply any specific design pattern in order to produce a well designed application. And if you follow the basic principles of OOP, including the ones I posted in this thread, chances are that your design will be a good one.

Also, to add to what James was saying, I was reading this link earlier, it has a simple example that contains the Observer pattern for MVC. http://leepoint.net/notes-java/GUI/structure/40mvc.html

BestJewSinceJC 700 Posting Maven

Yes- you do need to use good design to become a good programmer. However, don't let that article discourage you. I think you said you just started programming - a chat application is not something most people who just started programming would have much of a chance at completing successfully. Anyway, there are so many design techniques, it would be almost impossible to explain them in a post. Perhaps read about some of the design techniques mentioned, and ask some questions in this thread? Some of the important ones in Object oriented programming are Encapsulation, Inheritance, and Polymorphism. Of course, the most important thing is that similar data should be grouped together in classes, and methods should be used to manipulate the data in the class.

BestJewSinceJC 700 Posting Maven

GMT-5

BestJewSinceJC 700 Posting Maven

Okie .. thanks for the answers James, I will work it out and let you know if I am having any problems, thanks for the info BJSJC. ;)

You're welcome, but I don't know how much you got out of that (unless you went on to read other articles on the topic), because I'm finding myself feeling like I know less and less about the topic as I read more. I'll be back to ask questions about it on this forum though (not the definition of mvc, but the application of it), so maybe we can all learn something.

:)

BestJewSinceJC 700 Posting Maven

^ I tried that when I edited his code, it didn't change anything (as far as the problem he mentioned). Good catch though, I forgot to add that to my comments.

edit:

And k2k, I'm not sure if it matters or not, but you didn't add everything to panels. You added your two labels directly to the frame. If I were you though, I would try out some different layout managers. Like I said, I think CardLayout would be a good idea considering you're flipping through different same-sized windows.

BestJewSinceJC 700 Posting Maven

I haven't found out the cause of your problem. . I did look through your code, and there are certainly some things I thought should be changed (minor issues).

1. Why are you adding things explicitly to the JFrame, instead of putting them in a panel, then adding the panel to the JFrame? (I'm referring to the text that shows up when you first start the program). I'm not positive that this is bad practice, but I would do it with a panel.
2. I also think the reason the menu isn't showing up might be that you are adding your items directly to the frame, rather than to a panel. Add a panel to the frame, then add everything else to that panel. . so your AddRecord would be in the panel.

You'd have

JFrame
|
PanelWDesiredLayout
|
AddRecordPanel

BestJewSinceJC 700 Posting Maven

Use the String class's toCharArray method, putting the entire String into a character array. Then you can check each index to make sure the correct thing is at the index. For the indexes that contain numbers, you can use the Character class's isDigit method. There is also a more elegant solution that involves Patterns, but I don't know a lot about them, so I'll leave you with that (and my solution, while maybe not the best, will suffice and isn't very hard to implement).

BestJewSinceJC 700 Posting Maven
BestJewSinceJC 700 Posting Maven

Actually what smsamrc is asking is to print at the console something like this:

To have 2 as base and 3 to be smaller and to the upper right corner of 2.

Oops. I understand now.

BestJewSinceJC 700 Posting Maven

Yes, read it in as a String and print it out. ?

BestJewSinceJC 700 Posting Maven

I don't know how insistent you are on using whatever field you're using now, but one example of how you can display a number without the commas:

import javax.swing.*;
public class FormatField {

	public static void main(String[] args){
		JFrame frame = new JFrame();
		JPanel panel = new JPanel();
		frame.add(panel);
		JFormattedTextField field = new JFormattedTextField("");
		field.setText("100000");
		panel.add(field);
		frame.setSize(100,100);
		frame.setVisible(true);
	}
	
	
}

Of course, using this would mean using the Integer class's parseInt method and also converting any ints to Strings to show them in the field.

BestJewSinceJC 700 Posting Maven

So if they type in $PART2, you WANT it to print error postfix expression?

edit: It probably isn't working because you didn't prompt the user for input before the second loop. So it says while(scanner.hasNext()), but you never prompted the user (using a println statement), so it never entered that loop. Good luck.

BestJewSinceJC 700 Posting Maven

Well, post your code. I'll run it and fool with it and try to help you out. Also, I didn't post this yesterday because I don't want to lead you in the wrong direction, but perhaps it has something to do with some of the issues discussed here?

edit:

And this is a guess, but I think JPanels are opaque by default, (see: setOpaque method) whereas JScrollPanes are not. So that would mean that when you add the JPanel, it paints the entire JPanel, covering your menu. But since JScrollPanes are not, this doesn't happen when you add it. Try invoking the JPanels setOpaque(false) method before you add it and see what happens.

set opaque method

BestJewSinceJC 700 Posting Maven

No, certainly do not try any software provided by anyone with 5 posts, especially when there is a solution which involves nothing except the OS. In other words use Jbennet's solution.

BestJewSinceJC 700 Posting Maven

See my post above. .

edit:

No problem, glad I could help. Your code looks good, by the way.

BestJewSinceJC 700 Posting Maven

Ok. I think I see a problem:


E temp = (E)data[top--];
data[top] = null;
return temp;

You set temp to the element at top, then you subtracted one from top, then you set top to null. What you should be doing is setting temp to the element at top, setting top to null, then subtracting one from top. (Which would look like):

E temp = (E)data[top];
data[top--] = null;
return temp;

Ezzaral commented: helpful :) +19
BestJewSinceJC 700 Posting Maven

wait, nevermind. Your code confused me. Your class is a Queue class but you called it 'stack' so I thought it was LIFO. Forget what I said.

BestJewSinceJC 700 Posting Maven

I don't see any problems with your add method. The code in your remove method looks far more complicated than it needs to be. Your 'last' variable already tells you where the last element is (it is at that index last-1) so all you need to do when removing an element is check to see if last > 0 and remove & return the element at the index last-1. Don't forget to do last-- also since you no longer have an element there.

BestJewSinceJC 700 Posting Maven

I'm hardly an expert, but have you tried programmatically "clicking" the menu item again after the JPanel is set visible? That might make it appear on top again. Worth a try anyway, although even if it works, there is probably a better solution. I ran into a similar problem but mine was because I was using netbeans and the panels actually overlapped on each other.

BestJewSinceJC 700 Posting Maven

I took a look but I didn't see anything wrong at first glance. Got tired of looking after a couple minutes though. We aren't really here to debug your code. . most people will not do it at all. You should write tests for each method to make sure they are performing as expected. Figure out the first method that has a problem, then post back here again telling us what method the problem is in, what it is supposed to do, and any other info you have.

BestJewSinceJC 700 Posting Maven

Location start = getStart(), current;

You should check that that line is doing what you want it to.

BestJewSinceJC 700 Posting Maven

if(letter == "$PART2")

You can't test a String's contents by using '=='. It only works for primitive types, String is an Object. Use the String class's equal method

BestJewSinceJC 700 Posting Maven

When you have GUI applications, they usually should run on the Event Dispatch Thread. The EDT is what James is talking about when he says the Swing thread. Since you are waiting for a connection on the EDT, you are blocking the thread, since a Thread can only do one task at once. (Think of it like this: when you run any old program, the code executes in order, so the later code has to wait for the earlier code to execute). If you read the first page of the link, you'll gain a better understanding of the event dispatch thread. Also, there is a link in there on the first page to "memory consistency errors". I personally think it is interesting, you may want to check that out also, although it is slightly off topic.

BestJewSinceJC 700 Posting Maven

http://java.sun.com/docs/books/tutorial/deployment/jar/index.html

Or you could always use google. There are tons of quick tutorials giving you the same information we can offer.

BestJewSinceJC 700 Posting Maven

Rather than looking for consensus, you would be better served by obtaining your own opinion based in practice and understanding how the language works.
Then your confidence will increase, and it will cut through the self-promoted ego BS projected around you.

It doesn't seem like a topic of opinion though. Just trying to learn without going through the effort of multiple test programs. Because I don't remember any C and don't have too much time on my hands right now. And I'm egotistical?

(If so, :( )

BestJewSinceJC 700 Posting Maven

Huh? Are you saying you want to compare the images to see if they are duplicates? If so, you can do that by comparing pixel by pixel, since a blown up image converts every 1 pixel to 4 pixels or something like that. I don't know much about it, but I "studied" (read: didn't pay attention) to a similar topic in class, so I could point you towards some resources that would be helpful if that is what you're trying to accomplish.

BestJewSinceJC 700 Posting Maven

Yeah, looks like it's just a syntax error, not a logic problem.

BestJewSinceJC 700 Posting Maven

In your MenuBar class, you never initialized the scrollPane. This will cause problems in your MenuBar constructor, since you are using the scrollPane Object, which was never initialized (created using new JScrollPane). Hope that helps, if not, I can clarify. If you're having problems still once you fix that, point out what you're having problems with (For example, "my method xxx is supposed to do yyy but it isn't doing it and I don't know why") etc.

BestJewSinceJC 700 Posting Maven

And to follow up on what Masijade said, you can use the Scanner class to do so. Google for Scanner class documentation as well as the java sun tutorial on use of the Scanner class.

BestJewSinceJC 700 Posting Maven

That explanation doesn't make sense to me. Try to work on one issue at a time. And tell us what the overall task you're trying to accomplish is so we can help you by telling you what the best way to accomplish that goal is.

BestJewSinceJC 700 Posting Maven

If it will boot up in safe mode then remove and uninstall the harmful file or program. . ?

BestJewSinceJC 700 Posting Maven

I'm not going to lie, it is pretty frustrating to see such varying opinions on a topic like this. No consensus?

BestJewSinceJC 700 Posting Maven

Right, but I wanted to see it so that I could follow your logic, perhaps see if there is a loop somewhere or some other reason that paintComponent might be getting called repeatedly even if it doesn't need to be, etc

edit:

repaint calls paint which calls paintComponent, just FYI

BestJewSinceJC 700 Posting Maven

What you just said makes no sense. It looks like 3 sentences jumbled into one. In English, please?

BestJewSinceJC 700 Posting Maven

(Disclaimer: I'm a bit rusty on Sockets, you may want to wait until James or someone else concurs)

If you use the same Socket, the second thing you are sending will have to wait until the first thing is received. (In other words, only one thing can go over the connection at a time). But even if you use two different Sockets, due to what you mentioned earlier (about waiting for the file transfer), you would still need to use another thread.

PS

I just ran across this java tutorial related to a question you asked in another thread about encryption. . if you have some free time you might want to check it out.
http://java.sun.com/docs/books/tutorial/security/index.html