aspose file tools*
The moose likes Beginning Java and the fly likes putting code into JFrame Form Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "putting code into JFrame Form " Watch "putting code into JFrame Form " New topic
Author

putting code into JFrame Form

Karen Wilson
Ranch Hand

Joined: Nov 19, 2012
Posts: 68
I am creating a chat application and found a good tutorial online that explains everything.
The code works perfectly but instead of using a JFrame Form to make the GUI for the application they just typed out all the GUI code in a java class file.
I want to use a JFrame GUI so it can be easily changed. I tried making the GUI but I wasn't sure where to put the actionPerformed code
heres the original file

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38405
    
  23
You mean you want something like this?Good idea. Better than making your class extend JFrame.
As far as I am concerned making a display class implement ActionListener is an abomination against object‑oriented programming. Even if you find it in lots of books etc.
Making a GUI class connect to a port is another abomination of design. You should have one class which connects to the port and transmits all the messages. You should be able to run apps using instances of that class without using a GUI at all.etc etc.

You now have code which will send and receive the messages. Do not even attempt to get a GUI working until you have all that running nicely. Make sure your ChatConnector has an API with sendMessage(), receiveReply(), logIn(), logOut() (or similar), etc. methods visible.
Karen Wilson
Ranch Hand

Joined: Nov 19, 2012
Posts: 68
no that's not what i am trying to do. Most of the code that i showed is automatically generated when created a GUI by dragging and dropping the items eg buttons, onto the form.
so my problem is i don't know where to put some of the code. I created my own GUI and tried putting the code from the first post into it but now its not working.

here is the code. the part that is commented out is the part i need to place into my code. Put i am not sure if i put everything in the right place
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38405
    
  23
Karen Wilson wrote:. . . Most of the code that i showed is automatically generated when created a GUI by dragging and dropping the items eg buttons, onto the form.
Yes, you can tell you are using NetBeans from the pages of unintelligible group layout code.
. . . i am not sure if i put everything in the right place . . .
I have already told you you hadn’t. And just to make things worse, you have two lots of field declarations, at each end of the class. As far as I know, NetBeans usually adds individual listeners to your buttons, which is correct, rather than addActionListener(this). You want something like this:-You put the actual logic in the login, logout, sendMessage, etc methods. You write one at a time and try it, with print statements for debugging in the called methods.
Please always use // to comment out code, never /*...*/

Apart from repeating what I said yesterday about different classes which do different things, I don’t think there is a lot more I can say.
Karen Wilson
Ranch Hand

Joined: Nov 19, 2012
Posts: 68
ok so i am trying one button at a time starting with the the log in button.
my code now looks llike this:



but nothing is happening when i click the button. i don't know what the problem is
 
jQuery in Action, 2nd edition
 
subject: putting code into JFrame Form