0

I've set up a Server which runs and accepts connections from my Remote Client, and now I'm working on my GUI.

Before anything else, my goal here is to create a nice looking client that will have a login screen (login/pw), and then a nice layout with my options/perhaps a chat box after the user has logged in.

I've searched a lot online and used this site to set up my server and get things working, but I've got a bit of a problem with the GUI/theory and hope someone here can guide me a bit.

At the moment, I've set up a class called ClientGUI which is called from my main class, and this produces a 420x240 size screen. After placing my login/password JTextField boxes here, is it "proper" to set up the other GUI's the way I've outlined below? I'm not sure if I should be putting them under one class or how I would advance from one GUI to the next. I'm thinking I should repaint and resize the screen as necessary, but I am not sure how to set it all up. A brief outline would be helpful (you don't need to give me exact code).

public class ClientGUI extends JFrame {

    public ClientGUI() {
       setSize(420,240);
       setVisible(true);
       setTitle("Title");
       setResizable(true);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setLayout(null);
    }

    public loginGUI() {
       //code for my login/pw boxes, images, listener for entering information
    }

    public afterlogginginGUI() {
    }

    paint() {
    //not too sure about how this should be setup either.  Should I do all my textfield
    //and image work in paint()? 
    }

    }

I have never made anything like this, so I have the feeling I'm not setting this up in an ideal way.

An alternative is to have a different java class extending JFrame for each 'screen' I want, but if I do it this way, would I do it like this?

In my main RemoteClient class:

main {
   ClientGUI();
   //display whatever
   LoginGUI();
   //listen for login info
   if (loginIsValid) {
      afterlogginginGUI();
   }
}
Rahul
  • 83
  • 9

1 Answers1

1

I think you're thinking in to much of a linear fashion, where the code flows from A then to B then to C ... where in fact, Swing (and GUI's in general) are event driven...

C happens, so you do B, which triggers F so you do E ...

Start by creating a JPanel, onto this add your JTextField and JPasswordField, this will act as you basic login view. You could then add this to a JFrame or JDialog depending on your needs. You will need some way for the user to either "cancel" or "validate" their credentials.

Typically, I do this a separate view, as I never know where my poor "user details" pane might end up, but you could do this a single view (including the buttons within the "user details" pane), that will come down to your requirements.

You can use a CardLayout to switch from the "login" view to the "application" view. This has the benefit of maintaining only a single frame and prevents windows from been splashed all about the place...

I would, personally, separate the core functionality of the views to separate classes, this means you can simply create an instance when you need it and add it to whatever container you want.

I would recommend against extending from JFrame directly. This locks you into a single container (making it hard to re-use components or extend the program later) and you're not adding any new functionality to the class anyway...

Start by having a look at Creating a GUI With JFC/Swing.

You'll probably also be interested in How to Use CardLayout, How to Make Dialogs, How to Use Buttons, Check Boxes, and Radio Buttons and How to Write an Action Listeners

You'll also need to have a look at Laying Out Components Within a Container

Because you're likely waiting for a response from the server at some point, you will need to have a look at Concurrency in Swing and Worker Threads and SwingWorker wouldn't hurt

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I've got a general understanding of how I'll be validating login information (I've established my server to client writing/reading), so I do kind of know how I'll be triggering certain events. When you say to separate the core functionality to separate classes, do you mean separating everything (such as 1 class for login view, 1 class for whatever comes afterwards, etc), or only separate the functions (like validation but not the layout)? I assume you mean separate everything. I will read up on those links. – Rahul Oct 08 '14 at 04:55
  • *"I assume you mean separate everything"* - Yes, generally, this is the best approach. This is the premisis of the [Model–view–controller](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) paradigm. Basically, your UI (view) would collect the data from the user, when the press the "login" button, the button's `ActionListener` would then ask the controller to perform the validation. This means that you can actually change the way that the validation works, without need to change the view, as a basic example. If you're wondering, the model might contain the credentials – MadProgrammer Oct 08 '14 at 04:59
  • I'm designing my GUI for my login screen right now with a JFrame. Is there a specific reason for why you said to use JPanel and not JFrame? Also, when creating my 2nd screen, could I simply create another JFrame and design it separately in its own class, and then exit out of my login window and create an instance of the new class? I know you suggested to use CardLayout, and I may end up doing so, but I looked at an example online and am wondering if that's the best way to transition to the next view. – Rahul Oct 08 '14 at 06:06
  • [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) would be a start, it limits the re-usability of your components and you're not actually adding any functionality to the base class. Generally speaking, `CardLayout` is one of the best choice when dealing with multiple views. It's useful when you deal with navigable views (where the user can move between them) and non-navigable views, where you control the navigation – MadProgrammer Oct 08 '14 at 06:08
  • Okay that makes sense. Multiple JFrames would result in multiple windows, while multiple JPanels will allow me to spread out my functions and access them within a single JFrame. Is this correct? So I would create a single Client's JFrame (a more basic class with my main functions of size, visibility, title, etc.) and then add multiple panels to it (each within their own class for cleanliness) as needed. I still need to mess with the CardLayout class so I will do that in the meantime. Thank you for being so quick and thorough with your responses by the way! – Rahul Oct 08 '14 at 06:20