Antonio Ciccia
Antonio Ciccia

Reputation: 736

Java Swing GUI for a software, best way to do it

I need to create a GUI for a program with a panel to login that will show the real software after the login is set.

So the question is:

Should i create two frames and show the first for login and the second for program? Or is better to add two different panels and hide the login when the program starts?

Upvotes: 2

Views: 712

Answers (4)

Nick Rippe
Nick Rippe

Reputation: 6475

Using the GlassPane of the frame is an option (assuming you don't need the use of dropdowns). Some people may argue that it's an abuse of the pane system, but if the login screen is simple enough, I think it's legitimate.

The benefit is that it's already there for you to use (It doesn't require significant changes to your application), and that it eats any events (so they don't get passed to the actual application). It's also very easy to show/hide.

The downfall is a few swing components don't work on the glass pane (JComboBox is an example).

Here's an example of using the GlassPane:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class QuickTest {

    public QuickTest(){
        //Setup Frame
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
        frame.pack();
        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);

        //Setup Main Content
        JPanel main = new JPanel();
        main.add(new JLabel("Here's the application!"));
        frame.setContentPane(main);

        //Setup login Screen
        Box login = new Box(BoxLayout.Y_AXIS){
            // Makes the main screen faded
            public void paintComponent(Graphics g){
                g.setColor(new Color(255,255,255,200));
                g.fillRect(0,0, getWidth(), getHeight());
                super.paintComponent(g);
            }
        };
        login.add(new JLabel("Username here:"));
        login.add(new JLabel("Password here:"));
        JButton loginButton = new JButton("login");
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.getGlassPane().setVisible(false);
            }
        });
        login.add(loginButton);
        login.add(Box.createVerticalGlue());
        frame.setGlassPane(login);
        frame.getGlassPane().setVisible(true);

        //Show Frame
        frame.setVisible(true);


    }

    public static void main(String[] args){
        new QuickTest();
    }
}

Upvotes: 2

Frank D.
Frank D.

Reputation: 1306

Personally I would create a jFrame for the actual program first, then (immediately) open a jDialog window and open it in front of your jFrame. The jDialog class has a handy property that when it's opened, you cannot perform actions on your other frame. So it has to be closed (after some action, like filling in a password) first.

Upvotes: 4

user1839857
user1839857

Reputation: 1

You do a page for login and check the user right with session enable. If the user is identified then you can redirect to another page.

Upvotes: -4

MadProgrammer
MadProgrammer

Reputation: 347334

This is a little subjective. For me, it would come down to how complex the application is and whether you would need to support multiple sessions and if those sessions shared a single frame or not.

If you application relative simple (has one or two views for example), I'd consider it acceptable use something like a CardLayout and simply show the login pane within the main application frame, when the user is successfully authenticated, switch to the main view.

If the application is more complex, I'd consider using a separate dialog.

Even if you're allowing the user to have multiple sessions, it would come down to how complex the actual application is (IMHO) as to whether I would use a separate dialog or not.

I'd also consider if the user can do anything before they login, for example, can they update settings? If so, using a panel would be suitable as the login dialog is most likely going to be modal.

Upvotes: 5

Related Questions