Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Getting rid of Extra Dialog Boxes

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Getting rid of Extra Dialog Boxes

    I'm currently working on some code for an applet and I have it up and working but am trying to get rid of the extra JOptionPane Message Dialogs and replace them with just a simple JLabel within the already running applet.

    Here is my current code:

    import javax.swing.*;
     
    import java.awt.event.*;
    import java.awt.*;
     
    public class JPasswordC extends JApplet implements ActionListener {
     
        Container PW = getContentPane();
        JLabel password = new JLabel("Enter Password(and click OK):");
    	 Font font1 = new Font("Times New Roman", Font.BOLD, 18); 
        JTextField input = new JTextField(7);
        JButton enter = new JButton("OK");
     
        public void start() {
            PW.add(password);
    		  password.setFont(font1);
            PW.add(input);
            PW.add(enter);
            PW.setLayout(new FlowLayout());
            enter.addActionListener(this);
        }
     
        public void actionPerformed(ActionEvent e) {
            String pass1 = input.getText();
            String passwords[] = {"Rosebud", "Redrum", "Jason", "Surrender",  "Dorothy"};
           for(int i=0;i<passwords.length;i++) {
               if (pass1.equalsIgnoreCase(passwords[i])) {
                JOptionPane.showMessageDialog(null, "Access Granted");
            }
      			 else {
           JOptionPane.showMessageDialog(null, "Access Denied");
            	}
    		}
        }

    The part I'm trying to isolate is in the actionPerformed after the array runs. I currently have it having the Message Dialogs popping based on the if else statement but as I mentioned prior I'd rather clean it up and just add a JLabel to it.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Getting rid of Extra Dialog Boxes

    get rid of the extra JOptionPane Message Dialogs and replace them with just a simple JLabel
    Where is the JLabel that is to show the message?
    Have you tried replacing the dialogs with using the JLabels?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Getting rid of Extra Dialog Boxes

    Well I just went in and tried

     public void actionPerformed(ActionEvent e) {
            String pass1 = input.getText();
            String passwords[] = {"Rosebud", "Redrum", "Jason", "Surrender",  "Dorothy"};
           for(int i=0;i<passwords.length;i++) {
               if (pass1.equalsIgnoreCase(passwords[i])) {
                JLabel response = new JLabel("Access Granted");
            }
    			  else		  
        			{  		
    				JLabel response = new JLabel("Access Denied");
       			}
     
    			PW.add(response);
    			} 
    	 }

    but it doesn't show the response regardless of the input dialog. I want the dialog to appear after the OK button, hmm.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Getting rid of Extra Dialog Boxes

    Can you post some code that compiles and executes and shows the problem?

    After changing the contents of a container you need to have the container re-do its layout so the new group of components are displayed.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Getting rid of Extra Dialog Boxes

    I'm not quite sure which part of the code you want since I had posted all of it in my prior comments.

    When running the applet it displays and I enter the correct password to test it but instead of displaying a new JLabel it just stays blank as showing in my attached screenshot

    applet.jpg

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Getting rid of Extra Dialog Boxes

    post #1 has the JOptionPane code
    Post #3 is not a complete program that can be compiled and executed.
    You need to post a complete version of the new code. The reason is that there have been many cases where OPs make changes to some of the code and don't post all of the changes. If a complete program is posted the chances are better that it will be up-to-date and not missing some changes.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Getting rid of Extra Dialog Boxes

    Ah see that makes sense. Well thank you, I tinkered with the code some more and found the solution, I had to create a setText within the if else statement. Corrected Code is below if you're interested. Thank you for the help.

    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
     
    public class Password extends JApplet implements ActionListener {
     
        Container PW = getContentPane();
        JLabel password = new JLabel("Enter Password(and click OK):");
        JLabel message = new JLabel();
        Font font1 = new Font("Times New Roman", Font.BOLD, 18); 
        JTextField input = new JTextField(7);
        JButton enter = new JButton("OK");
     
        public void start() {
            PW.add(password);
            password.setFont(font1);
            PW.add(input);
            PW.add(enter);
            PW.add(message);
            PW.setLayout(new FlowLayout());
            enter.addActionListener(this);
        }
     
        public void actionPerformed(ActionEvent e) {
            String pass1 = input.getText();
            String passwords[] = {"Rosebud", "Redrum", "Jason", "Surrender",  "Dorothy"};
            for(int i=0;i<passwords.length;i++) {
                if (pass1.equalsIgnoreCase(passwords[i])) {
                    message.setText("Access Granted");
                                return;
                }
                else {
                    message.setText("Access Denied");
     
                }
            }
        }
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Getting rid of Extra Dialog Boxes

    Glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    ZBixby (March 16th, 2013)

Similar Threads

  1. How to get rid of this?
    By tai8 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 12th, 2012, 07:54 PM
  2. Proper way to get rid of breaks.
    By Massaslayer in forum Loops & Control Statements
    Replies: 6
    Last Post: December 23rd, 2011, 07:53 PM
  3. Modifying a complicated object with dialog boxes
    By hunter555 in forum Java Theory & Questions
    Replies: 7
    Last Post: January 27th, 2011, 03:02 PM
  4. Swing Dialog Boxes --- Have no clue why its not working
    By jap2008 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 18th, 2010, 04:20 PM
  5. cant get rid of http connection
    By kartik in forum Java Networking
    Replies: 1
    Last Post: July 21st, 2009, 03:09 AM