Java GUI Problem

D

DyslexicAnaboko

I am having a problem that has really got me stuck and I think it is
because I am going about it incorrectly.

Here is my problem in the most plain terms possible:

I wrote a program that has a Class "A" and for class A there is a GUI
class called "A_GUI" in its own package called "GUI".

The following should occurs between the two classes:

I launch Class A, which will in turn invoke class A_GUI.

It is done in this order because class A need to load data from certain
files and what not before A_GUI can be useful.

The A_GUI class has two fields to fill in, for arguments sake we will
call them the:
userName and passWord fields.

When A_GUI is invoked the user is prompted to input his userName and
passWord.
After the user puts in their information they will press login.
Login in turn will send the users input back to class A for validation.
Class A will validate the user and log them on to the system.

The problem I am having occurs at the creation of A_GUI. The specifics
are that the screen launches successfuly, however class A continues to
run after invoking class A_GUI. This is a problem because class A is
trying to validate two strings that have null values. This ends up
making the program stop and throws a null pointer exception for very
obvious reasons.

What I want to do (correct me if I am going about this the wrong way)
is somehow tell class A to WAIT until it gets a response from A_GUI,
and then, and only then will it proceed to function. I think this has
something to do with eventListeners, but I do not know how to make one
class listen for another class.

I would greatly appreciate it if someone could help me with this
because I am at total loss.
 
R

Roedy Green

however class A continues to
run after invoking class A_GUI

A class does not run. A method in that class can run. Normally the
idea is you launch your GUI then return. The GUI part continues to
run when events are created by the user clicking things.Everything is
running on the Swing event thread.

Perhaps if you created a miniature version of you code and posted it,
it would be clearer what you are talking about.
 
T

Tom Leylan

I would say it sounds like you are describing a modal dialog box.

http://java.sun.com/j2se/1.3/docs/api/java/awt/Dialog.html

Also you may want to add a Validate() method to your ClassA (rather than
count on checking things when the dialog closes.) This could be used by
your dialog so it wouldn't have to close and reopen in the event that the
account/password is wrong. You can for instance give the user 3 tries
without closing your modal dialog window.
 
H

hiwa

In the class A, you should instantiate the GUI and do nothing
thereafter.
Basically, you would do in the A_GUI class:
----------------------
loginButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
myClassAobject.validate(pwfield.getText(), idfield.getText());
}
});
 
D

DyslexicAnaboko

Okay, here is a reduced version of what I am doing:

//This is the constructor of the director class (class A)
public Director()
{
//Loads user information into a TreeMap for the class
accessMap = reg.getValidUsers() ;

//While the user is not valid the keep object of the GUI class
//"LoginGUI" alive (A_GUI)
//The object name is "loginScreen"

while( valid == false )
{
loginScreen.setVisible( true ) ; //Launch window

//Source of my problem is here, I want the user
//to be able to type something in before the
//following methods try to validate anything at all.

//String userName gets the input for the
//user name from the login class.

//String passWord gets the input for the
//pass word from the login class.

userName = loginScreen.getUN() ;
passWord = loginScreen.getPW() ;

//This method is not shown, but it validates the userName
//and passWord against the list that was loaded up above
//during the creation of this class (class A/ Director class).

valid = validate( userName, passWord ) ;

//println for testing purposes, is the user valid? true or false.

System.out.println( "User valid? : " + valid ) ;
}

//If the user name and pass word are correct, then disable the GUI.

loginScreen.setVisible( false ) ;

//Direct this user to where ever they are determined to go
//(don't worry about this)
//Direct this login

director() ;
}


==========================================

I really appreciate everyone's help. I will try everyone's suggestions.
 
D

DyslexicAnaboko

Okay, I think you got the classes mixed up, but you say to put a
validate method in my GUI class? I'll try that.

As for the modal dialog box, thanks for the suggestion, but I don't
think I will be able to do that since I am using Net Beans for creating
all of my GUI. Which visually, it does nicely, but in my opinion the
code it generates is sloppy. Thanks anyhow though =).
 
D

DyslexicAnaboko

I see what you are saying, and I think I tried doing something like
that, but is it possible to send an instance of the current class to
another class so that a new class is not instantiated?

In other words:

Is it possible to do this:

class A
{
public A()
{
//Constructor
B otherClass = new B() ;

otherClass.getInstance( this ) ;
}

public static void main( String [] args )
{
A myA = new A() ;
}
}

class B
{
public B()
{
//Constructor
}

public void getInstance( A fromA )
{
//does something with class A instance
}
}

Is this what you are talking about?
 
R

Roedy Green

public Director()
{
//Loads user information into a TreeMap for the class
accessMap = reg.getValidUsers() ;

This is not valid code. You can't have assignment statements except
inside methods or inside static or instance init blocks.

You made a peculiar comment earlier about a "class" running. I think
this may be one of the keys to your problem.
 
D

DyslexicAnaboko

Its nice that you don't think this is valid, but it isn't a coincidence
that I have written code like that before and it executes properly and
easily. If that is not proper code then tell Mark Allen Weiss that he
is not coding properly as well. I took data structures with this
professor and he is a very well known programmer. He too makes his
constructors like that and yes you can have assignment statements
inside of constructors, you are wrong.

If you don't believe me then go ahead and try it before you point the
finger.

Plus I don't appreciate your poking fun at me. If that wasn't your
intention then I am sorry to be accusing you of such, but that is how
it comes off.
 
O

Oliver Wong

DyslexicAnaboko said:
Its nice that you don't think this is valid, but it isn't a coincidence
that I have written code like that before and it executes properly and
easily. If that is not proper code then tell Mark Allen Weiss that he
is not coding properly as well. I took data structures with this
professor and he is a very well known programmer. He too makes his
constructors like that and yes you can have assignment statements
inside of constructors, you are wrong.

When I saw your code, I thought that was a class declaration, not a
constructor (and I suspect Roedy made the same mistake). He's right that you
can't have arbitrary code directly inside a class, and you're right that you
can have arbitrary code inside a constructor. To avoid confusion, you might
want to format your code into an SSCCE so everyone knows exactly what's
going on. http://mindprod.com/jgloss/sscce.html

As for Mark Allen Weiss and good coding style... well, let's just say I
personally wouldn't put "while(valid = false) { loginScreen.setVisible(
true ) ; /* ... */ }" inside of a constructor.

- Oliver
 
O

Oliver Wong

DyslexicAnaboko said:
Okay, I think you got the classes mixed up, but you say to put a
validate method in my GUI class? I'll try that.

As for the modal dialog box, thanks for the suggestion, but I don't
think I will be able to do that since I am using Net Beans for creating
all of my GUI. Which visually, it does nicely, but in my opinion the
code it generates is sloppy. Thanks anyhow though =).

If NetBeans won't let you use modal dialog boxes, throw away NetBeans.
Seriously. Modal dialog boxes are a pretty important topic in GUI design.
They specifically address the problem you're trying to solve (that is,
display a dialog box, and *WAITING* until the user responds to the dialog
box before proceeding).

More likely, there's a way to get NetBeans working with modal dialog
boxes (or else it'd be a joke IDE instead of a serious one), you just gotta
figure out how. I don't use NetBeans so I don't know how either. Perhaps
another poster more familiar with NetBeans can chime in here?

- Oliver
 
O

Oliver Wong

DyslexicAnaboko said:
I see what you are saying, and I think I tried doing something like
that, but is it possible to send an instance of the current class to
another class so that a new class is not instantiated?

In other words:

Is it possible to do this:

class A
{
public A()
{
//Constructor
B otherClass = new B() ;

otherClass.getInstance( this ) ;
}

public static void main( String [] args )
{
A myA = new A() ;
}
}

class B
{
public B()
{
//Constructor
}

public void getInstance( A fromA )
{
//does something with class A instance
}
}

This is possible. Have you studied "static" methods yet? If so, just
slap the static keyword in front of getInstance().

- Oliver
 
A

Alex Hunsley

DyslexicAnaboko said:
Its nice that you don't think this is valid, but it isn't a coincidence
that I have written code like that before and it executes properly and
easily.

As Oliver points out in his reply, I think Roedy thought that you were
posting the code for a class, and not for a constructor. I believe he
thought that because of a combination of a) you didn't post the outer
class definition, only a constructor, and b) the code in that
constructor looked very non-constructor indeed!
The code is 'valid' in that it will run, yes. However, the code is not
well written, in that it is doing waaay too much inside a constructor.
If that is not proper code then tell Mark Allen Weiss that he
is not coding properly as well.

Mark Allen Weiss, your code (as posted here) is not well written. There
you go. Please show him this if you feel like it, and invite him to join
the discussion!
Just out of interest, do you always assume that Professors know the
best, at all times? It's not always the case. No en-masse slight
intended to those in academia, but: I've seen staff-written code given
to students on computer science degrees (at the most venerable
institutions) that was questionable in its practices. Lecturers and
professors are not perfect, please disabuse yourself of the notion that
they necessarily create good code! (Contrariwise, I'm sure some of them
are very good...)

As to why that code is not well written - here is why:
Classes in OO represent nouns (or 'things'), and are named as such. You
can still have a class as a noun while also suggesting what it does
(i.e. provide a verb). An example is the code you posted, which was for
a class called Director: a director being a thing (noun) that directs.
As another example, a class that handles FTP connections might be called
FTPAgent, or FTPConnectionManager, or similar. Examples of poorly named
classes might be Direct or Connect. These are verbs.

(Bear with me a little longer...)

Now, because classes should be nouns (things), and constructors are for
constructing these things, it doesn't make a lot of sense for a
constructor to actually *do* many things. A constructor should construct
something - an object instance - that's the semantics of construct. The
*actions* (think verbs) an object performs should generally be presented
as methods (and that includes populating a non-trivial GUI component or
showing a GUI component).

You may think this is all wishy washy hair splitting, but there are
good reasons to follow these guidelines. People sometimes get burnt
trying to do too much in constructors: to see why, try the following:

Q: What does the code below output to the screen? (You're not allowed to
run it. Just answer by reading the code.)

(I'd like you to ask your professor too. And if he knows the correct
answer, why does he think it's a great idea to encourage people to shove
reams of code into a constructor?)


class A {

public A() {
setUpSomeThings();
}

protected void setUpSomeThings() {
// some setup code in here
}

}

public class B extends A {
private int memberVariable = 0;

public B() {
super();

System.out.println("memberVariable = "
+ memberVariable);
}

protected void setUpSomeThings() {
memberVariable = 7;
}

public static void main(String[] args) {
B bInstance = new B();
}

}


Bonus question: What is the output if we change the line:

private int memberVariable = 0;

to:

private int memberVariable;

?

I took data structures with this
professor and he is a very well known programmer.

.... with really bad habits vis a vis Java constructors, apparently. I am
not impressed with this style.

I've just looked up your Professors site. Here's a sample of his code:

private static int max3( int a, int b, int c )
{
return a > b ? a > c ? a : c : b > c ? b : c;
}

Can you see anything wrong with this style? Put simply, it's a confusing
mess. Maybe putting this into one line, without even any brackets, gave
him warm fuzzies and made him feel clever. Great for him, crap for
anyone else who comes along (including him at a later date, possibly).
Just the act of adding some brackets makes that code vastly more readable:

return a > b ? (a > c ? a : c) : ( b > c ? b : c);

And arguably you could even have:

if (a > b) {
return a > c ? a : c;
}
return b > c ? b : c;

Some people would cry "but it's obvious what the method does from the
name, it gives you the maximum of three params!" - no, it's obvious from
the name what method *should* (and may, or may not) do.

(Observation: your Prof seems to be used to writing and handing out code
for others, but not so used to being on the receiving end of code
written by others.)

Plus I don't appreciate your poking fun at me. If that wasn't your
intention then I am sorry to be accusing you of such, but that is how
it comes off.

I'm very sure that he didn't mean it as poking fun. He just got the
wrong end of the stick about the meaning of the code you posted (i.e.
thinking that it was a class, not a constructor).


As a final note, there are some guidelines for a misery-free existence
regarding constructors:

1. Do as little as possible in constructors. If you need lots of logic
concerning object creation, favour factory methods or similar.

2. Ideally, don't call non-final and non-private methods from
constructors of non-final classes.

3. If you break rule 2, document this, and don't access any object state
from these methods.

These rules may seem a little esoteric but they avoid the sorts of
shenanigans illustrated by my code question above.
 
D

DyslexicAnaboko

Yeah, don't pin that on Mr. Weiss, he would know better I suppose. All
i'm saying is he does decalre varaibles and what not in a constructor.
What ever I did doesn't mean he would do the same, all I was trying to
say is that i've seen it done.

I will do my best to take a glance at the SSCCE, I am not familiar with
it and I am very pressed for time, however I will get to it when I can.
 
H

hiwa

I see what you are saying, and I think I tried doing something like
that, but is it possible to send an instance of the current class to
another class so that a new class is not instantiated?

In other words:

Is it possible to do this:

class A
{
public A()
{
//Constructor
B otherClass = new B() ; B otherClass = new B(this) ;

otherClass.getInstance( this ) ;
}

public static void main( String [] args )
{
A myA = new A() ;
}

}

class B
{
A insA;
 
D

DyslexicAnaboko

Will do, yeah I am very new at NetBeans, it was recommended to me since
it is very easy to create GUI with it. More than likely it does have
modal, I just meant that it will do what does, I am not in control of
what it uses, I am using it because I know squat about formatting. I
will look around to find out more about that, but right now I am just
using a very simple JFrame as a test, I am not actually using the
NetBeans GUI. That could also be the problem.
 
D

DyslexicAnaboko

Yes I am familiar with static methods. Great I wasn't aware of that
then, I will indeed use this idea if I am pressed to use it. Thank you.
 
D

DyslexicAnaboko

Mr. Wong, you seem to be very good at this, possibly you can help me
out with another problem I just ran into:

Is it possible for a class inside of a package to access classes
outside of its package?

I do not know how to do this and I have been trying to find it via
google but no luck at all.

If you could unlock that mystery it would help me a great deal.

Thank you for your help thus far.
 
D

DyslexicAnaboko

Thanks for the long reply that won't help me with anything at all. I am
just trying to get questions answered and I don't have time to have a
flame war. So please, if you aren't going to help me understand what I
did wrong and just ridicule me, I would rather you save your time and
mine by not replying.

Thank you.
 
R

Roedy Green

Thanks for the long reply that won't help me with anything at all. I am
just trying to get questions answered and I don't have time to have a
flame war. So please, if you aren't going to help me understand what I
did wrong and just ridicule me, I would rather you save your time and
mine by not replying.

He did not ridicule you. He pointed out why that code snippet was not
idiomatic Java. Supposedly you are here to learn idiomatic Java.

When you post your code, you are implicitly asking for us to tell you
how to improve it. You don't have to take the advice.

You have a lot of nerve to complain about someone going to the effort
of telling you how to fix your code just because you are too lazy/busy
to do the work.

It is kind of like going to the doctor and he recommends 30 minutes of
exercise a day. You whack him over his head with his clipboard
because you are too busy to exercise 30 minutes a day. That is not his
fault.

Please see http://mindprod.com/jgloss/newsgroups.html#GIFTHORSE
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top