Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8434407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:38:47+00:00 2026-06-10T06:38:47+00:00

** SOLVED ** I’m fairly new to Java and so far I love it!

  • 0

** SOLVED **

I’m fairly new to Java and so far I love it!

So I’m just asking if someone has a idea that could help me out. So here’s what I would like to do.

What I’m working on right now is a application that can interact with my local website (change titles, content, etc). So what I like to do is show a JOptionPane.showConfirmDialog, and enter a username and password.

So basically if the username or password is wrong I would like to display a JOptionPane.showMessageDialog but when they click Ok to let them know that there information is wrong the showConfirmDialog disappears!

Any idea guys?! Here’s my code.

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

public class javaTesting extends JFrame {
    public JFrame mrFrame;
    public int enter;
    public JPanel mrPanel;

    public javaTesting() throws Exception
    {
            Class.forName("com.mysql.jdbc.Driver");
            try {
                Connection con =  DriverManager.getConnection("jdbc:mysql://localhost:3306/cms","root","");
            } catch (SQLException e){
                System.out.println(e.getMessage());
            }
            mrFrame = new JFrame();
            mrPanel = new JPanel();

            mrPanel.setLayout(new GridLayout(4,1));

            JLabel user = new JLabel("Username");
            mrPanel.add(user);

            JTextField user_input = new JTextField(30);
            mrPanel.add(user_input);

            JLabel pass = new JLabel("Password");
            mrPanel.add(pass);

            JTextField pw_input = new JPasswordField(30);
            mrPanel.add(pw_input);

            mrFrame.setSize(700,700);
            mrFrame.setLocationRelativeTo(null);
            mrFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //mrFrame.setVisible(true);
            mrFrame.setResizable(false);

            input();

            if(enter == JOptionPane.OK_OPTION)
            {
                JOptionPane.showMessageDialog(null, "You clicked ok!");
                input();
            } else {
                System.exit(1);
            }
    }
    public void input()
    {
         enter = (int) JOptionPane.showConfirmDialog(mrFrame,mrPanel,"Login Cridantiels",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
    }
    public static void main(String agrs[]) throws Exception
    {
        new javaTesting();
    }
}

So this is what I did and it seems to work fine for me don’t know if its incorrect. However it works :]

 do{
        input();

        if(enter == JOptionPane.OK_OPTION)
        {
            JOptionPane.showMessageDialog(null, "You clicked ok!");
        } else {
            System.exit(1);
        }
     } while(enter != JOptionPane.CANCEL_OPTION);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:38:49+00:00Added an answer on June 10, 2026 at 6:38 am

    As shan has suggested, you need to loop you credentials gathering portion of you code. Here’s an example

    JPanel pnlDetails = new JPanel(new GridBagLayout());
    JTextField userNameField = new JTextField(10);
    JPasswordField passwordField = new JPasswordField(10);
    
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.anchor = GridBagConstraints.WEST;
    gbc.insets = new Insets(2, 2, 2, 2);
    
    pnlDetails.add(new JLabel("User name:"), gbc);
    gbc.gridy++;
    pnlDetails.add(new JLabel("Password:"), gbc);
    
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.anchor = GridBagConstraints.EAST;
    pnlDetails.add(userNameField, gbc);
    gbc.gridy++;
    pnlDetails.add(passwordField, gbc);
    
    // The result from the dialog, will be OK or CANCEL
    int operation;
    // Flag used to determine if the credentials are okay or not
    boolean badCredentials = true;
    do {
    
        operation = JOptionPane.showConfirmDialog(null, pnlDetails, "Login", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
    
        String userName = userNameField.getText();
        char[] password = passwordField.getPassword();
    
        // You would valid you credintals here :P
        if (userName.equals("super") && new String(password).equals("happy")) {
    
            badCredentials = false;
    
        } else if (operation != JOptionPane.CANCEL_OPTION) {
    
            JOptionPane.showMessageDialog(null, "Invalid Credentials", "Error", JOptionPane.ERROR_MESSAGE);
    
        }
    
    } while (operation != JOptionPane.CANCEL_OPTION && badCredentials);
    
    if (!badCredentials && operation != JOptionPane.CANCEL_OPTION) {
    
        System.out.println("Continue program");
    
    } else {
    
        System.out.println("Exit program");
    
    }
    
    System.exit(0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Solved: i just managed to solve the problem by creating a new table and
SOLVED. Code has been edited to reflect solution. Given the following GridView : <asp:GridView
- Solved - I Know they exist, but I haven't found a slider that
SOLVED See my answer below. Question left unchanged for anyone else who has trouble
SOLVED - see Bish below I've got a list of checkboxes that all dump
Solved: It turns out that the affected machine had .NET 4.5 beta installed. I
SOLVED! This works, I need to tell the compiler that T implements IEquatable of
[SOLVED] created symlinks from /usr/lib/lib/* to /usr/lib* [UPDATE 3] NEW VERSION: Ok, I think
SOLVED MY OWN QUESTION! THANKS EVERYONE FOR THE HELP :) Ok. I'm having trouble
SOLVED. See at bottom. Just upgraded to OSX Lion and trying to get my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.