user3930774
user3930774

Reputation: 16

How would you close a jpanel?

I have the following code:

public static void main(String[] args) throws MalformedURLException {

    // ask player for his username, this is used for login.
    Random rand = new Random();
    int r = rand.nextInt(1000 - 1) + 1;

    Object[] options1 = {"Login", "Random Username",
            "Quit"};

    JPanel panel = new JPanel();
    panel.add(new JLabel("Enter your desired username:"));
    JTextField textField = new JTextField(16);
    panel.add(textField);

    //random usernames
    String words = "username:noob:player:slayer:foreveruser:forevernoob:foreverplayer:lol:wrectlikeaboos:darkalien:gold_in_silver:zeebra:zebra:maddox:canada:canadian:richtard";
    String[] wordsAsArray = words.split(":");
    int index = new Random().nextInt(wordsAsArray.length);
    String randomWord = wordsAsArray[index];
    Random num = new Random();
    int numr = num.nextInt(1000 - 1) + 1;

    int result = JOptionPane.showOptionDialog(null, panel, "Forever: Login",
            JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
            null, options1, null);
    if (result == JOptionPane.YES_OPTION) {
        JOptionPane.showMessageDialog(null, "Logged in as " + textField.getText());
    }
    //randomusername = no
    //quit = cancel
    if (result == JOptionPane.CANCEL_OPTION) {
        System.exit(1)
    }
    if (result == JOptionPane.NO_OPTION) {
        JOptionPane.showMessageDialog(null, "Logged in as " + randomWord + numr);
    }
    //String user = JOptionPane.showInputDialog("Enter your username: ", "User"+r);
    Login client = new Login("hi");
}

public Login(String userName) throws MalformedURLException {
    Launcher launcher = new Launcher();
    launcher.init();
    this.username = userName;
}

Basically, when you run the program, you get a popup asking for your username. If you press quit, it runs the game anyway without any username (not supposed to happen) and if you press X it does too. How would I make it so it just ends the program instead of it running without any username.

Thank you.

Upvotes: 0

Views: 1799

Answers (3)

MadProgrammer
MadProgrammer

Reputation: 347204

First of all, you need to understand what the JOptionPane is telling you...

From the JavaDocs for JOptionPane.showOptionDialog(Component, Object, String, int, int, Icon, Object[], Object)

Returns: an integer indicating the option chosen by the user, or CLOSED_OPTION if the user closed the dialog

This can be a little vaguge, but, essentially what this will do is return in the index of the Object[] options which the user selected. This means that the normal constents won't have any meaning (other then CLOSED_OPTION).

So instead of using NO_OPTION or CANCEL_OPTION, you need to use 0 for Login, 1 for Random Username and 2 for Quit, for example...

if (result != 2 && result != JOptionPane.CLOSED_OPTION) {
    if (result == 0) {
        JOptionPane.showMessageDialog(null, "Logged in as " + textField.getText());
    } else {
        JOptionPane.showMessageDialog(null, "Logged in as " + randomWord + numr);
    }
    //String user = JOptionPane.showInputDialog("Enter your username: ", "User"+r);
    Login client = new Login("hi");    
}

Then you should also only create your Login if a valid response is chosen.

I'm old school, I believe in one entry point and one exit point for the program, littering your code with System.exits will only make it more difficult to debug in the future - IMHO

Upvotes: 1

tobii
tobii

Reputation: 517

You need to check for one more possible result: JOptionPane.CLOSED_OPTION. That is for when the user hits the X or closes it any other way.

In the question, you said it also didn't work right for the CANCEL_OPTION, but I see you edited you question and fixed that (you added the System.exit(1))

Also, I would recommend writing it with an if/else statement so you only need to worry about the actions you care about:

String username;
if (result == JOptionPane.YES_OPTION) {
    username = textField.getText();
} else if (result == JOptionPane.NO_OPTION) {
    username = randomWord + numr;
} else { //Or add if's for CANCEL and CLOSED if you want to do something else
    System.exit(0);
}
JOptionPane.showMessageDialog(null, "Logged in as " + username);
Login client = new Login(username);

Upvotes: 0

NESPowerGlove
NESPowerGlove

Reputation: 5496

Where you handle the action of pressing quit, you could call System.exit(int) (assuming you have nothing to clean up or save off that isn't being done in a shutdown hook).

Upvotes: 1

Related Questions