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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 26

Thread: JOptionPane help

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default JOptionPane help

    Okay so my instructor just told us we needed to use a GUI via JOptionPane with our program. So that we may turn out programs in as executable jar files. However, I have never used it and im kind of new at this stuff so I need some help. Here is my problem and code:

    package waitingroomlinkedlist;
    import java.util.Scanner;
    import javax.swing.JOptionPane;
     
    public class WaitingRoomLinkedList {
     
        public static void main(String args[]) {
            boolean done = false;
            String inputFirst;
            String inputLast;
     
            String[] choices = {"First Name", "Last Name", "Quit"};
            while (!done) {
                int choice = JOptionPane.showOptionDialog(
                        null,
                        "Personal Information:",
                        "Register",
                        JOptionPane.YES_NO_CANCEL_OPTION,
                        JOptionPane.QUESTION_MESSAGE,
                        null,
                        choices,
                        choices[0]);
     
                switch (choice){
                    case 0:
                        inputFirst = JOptionPane.showInputDialog( "Enter First Name");
                        break;
     
                    case 1:
                        inputLast = JOptionPane.showInputDialog("Enter Last Name");
                        break;
     
                    case 2:
                        done = true;
                        JOptionPane.showMessageDialog(
                            null,
                            "...quitting!",
                             "",
                             JOptionPane.ERROR_MESSAGE);
                        break;
                }
            }
            Person p1 = new Person(inputFirst, inputLast);
        }
    }

    Now for some reason I am getting an error when trying to create a new instance of the Person class and pass inputFirst and inputLast. Its telling me that I have not initialized inputFirst or Last. When I allow it to initialize it sets inputFirst = null; and so forth with inputLast. Any idea what's going on here? All I want to do is pass the two Strings to my constructor in my Person class.


  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: JOptionPane help

    I want to do is pass the two Strings to my constructor in my Person class.
    The code needs to make sure those variables have values.
    Because the statements where inputFirst is given a value is in a switch and it is possible that the code might not be executed, the compiler gives the warning.
    To make the compiler happy, give those variables a value when they are defined. For example null or "" would work.
    If you don't understand my answer, don't ignore it, ask a question.

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

    vysero (September 23rd, 2014)

  4. #3
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane help

    Oh I see now thank you. So I have another question. Is it possible to print in a JOptionsPane window?

  5. #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: JOptionPane help

    Not easily. Look at what a JOptionsPane can show and how a program can put a String there to be shown.
    Read about its methods in the API doc.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane help

    JOptionPane.ERROR_MESSAGE
    JOptionPane.INFORMATION_MESSAGE
    JOptionPane.WARNING_MESSAGE
    JOptionPane.QUESTION_MESSAGE
    JOptionPane.PLAIN_MESSAGE

    These are the only thing I could find however I realize how to print a message like this. What im trying to figure out is how to say write:

    JOptionPane.showMessageDialog(null, p1.printName(), JOptionPane.PLAIN_MESSAGE)

    The problem I am encountering is how to get p1 to be a recognized variable. I create an instance of the Person class before I create the GUI then I can't pass info from the GUI to the person class. Also, randomly creating an instance of the person class inside one of the case's doesn't seem like the way to go about it. I want it to act like the Quit button instead rather than have it print "quitting..." I want it to print First name Last name and a number(ticket #).

  7. #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: JOptionPane help

    how to get p1 to be a recognized variable
    Define it in scope where the code is trying to use it.
    Does the printName() method return an object with a toString() method that returns something you want to be shown?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane help

    To answer your question no the print method is just something I made in order to test if I could call my methods inside the JOptionPane. I think this might be what you meant by define it in scope tell me if im wrong:

    case 2: 
    Person p1 = new Person(inputFirst, inputLast); 
    JOptionPane.showMessageDialog(null, p1.printName, JOptionPane.PLAIN_MESSAGE);

    Says that void type is not allowed here any way around that?

  9. #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: JOptionPane help

    void type is not allowed
    The printName() method is expected to return an Object like a String that can be displayed in the JOptonPane's window.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane help

    Okay so I tried changing my printName() method so that it would return fName and lName. However, now in the person class the error is similar in that a String cannot be converted to void. Here is the Person class as is:
    public class Person{
    private String fName;
    private String lName;
    public int counter = 1;
     
    public Person(String fName, String lName){
    this.fName = fName;
    this.lName = lName;
    counter++;
    }
     
    public printName(){
    return (fName + lName);
    }

    I am really kind of lost at this point. What I want is when someone puts in there first name last name. Then I want to give them a number and store (either the number or the names.. or both) into the queue so that when someone hits the next button it dequeues first name, last name and the number and then prints that info in the JOptionsPane. This is the most complicated program I have undertaken so far so please bear with me.

  11. #10
    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: JOptionPane help

    the error is similar
    Please copy the full text of the compiler's error messages and paste it here.
    It explains what the problem is.

    What I want is..
    Make a list of the steps the program needs to do to satisfy what you want.
    Then work through the list one item at a time.
    For each step: Code it, compile it, test it, fix problems and redo until working.
    Then move to the next step
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane help

    I figured out the problem being reported in the Person class. I defined the method as a string to fix it. Here is the error in the main class:

    "error: no suitable method found for showMessageDialog(<null>,String,int)
    JOptionPane.showMessageDialog(null, p1.printName(), JOptionPane.PLAIN_MESSAGE);
    method JOptionPane.showMessageDialog(Component,Object) is not applicable
    (actual and formal argument lists differ in length)
    method JOptionPane.showMessageDialog(Component,Object,Str ing,int) is not applicable
    (actual and formal argument lists differ in length)
    method JOptionPane.showMessageDialog(Component,Object,Str ing,int,Icon) is not applicable
    (actual and formal argument lists differ in length)
    1 error"

    I think maybe I should convert the string to an object or something.. not sure.

  13. #12
    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: JOptionPane help

    no suitable method found for showMessageDialog(<null>,String,int)
    Read the API doc for the JOptionPane class and look at the args that need to be passed to the showMessageDialog method. The compiler can't find a version of the method with the args the code is passing to the method.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane help

    Ha your a genious I knew it. Okay so now I think I have found a way to print the ticket number as well. It writes the message in the upper portion of the dialog window so in the lower portion could be the ticket number. Thanks! I will come back if I get stuck again.

    --- Update ---

    Now the somewhat mind boggling part. How can I make my program create a new person every time a first and last name is entered, so that my program handles multiple people? I am starting to think I shouldn't have tried going the route you have been helping me with... I read that I can do something like create a new (method?) inside my main method that will handle the inputs like this:
    private static void processInputString(String inputFirst);
    then what I do inside the case is say:
    processInputString(inputFirst)
    . Doing it this way might allow me to operate on each input, however I am not sure if this is the right way to go about it either because then I might have trouble with printing again... Any suggestions?

    Also, why might my program compile/build successfully but when I choose to run the program it pops up a message saying error were found? The program still works too.

  15. #14
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: JOptionPane help

    Post error messages that you want help with.

    As for your design question, try doing it "this way" as you suggest and see what happens. If you don't like the results, then change it. If you're not sure how, come back and explain what you're trying to do but can't figure out how to do.

  16. #15
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane help

    Okay so "this" didn't work out. I found some info online on a different method of using JOptionPane. I think I am on the right track but I am running into some problems with data types not being compatible. Here is the full program as is:

    package tester;
     
    import java.util.NoSuchElementException;
    import javax.swing.JOptionPane;
     
    public class Tester{
        public static void main(String args[])
        {
           String firstName; 
           String lastName;
           int i = 1;
           Person personReference;
           boolean done = false;
     
           LinkedList personQueue = new LinkedList();
     
           String[] choices = {"Emter Name", "Next", "empty?", "quit"};
           while (!done) {
                int choice = JOptionPane.showOptionDialog(
                        null, 
                        "Click a choice",
                        "Stack Operations Menu",
                        JOptionPane.YES_NO_CANCEL_OPTION,
                        JOptionPane.QUESTION_MESSAGE, 
                        null,
                        choices,
                        choices[0]);
                try{
                    switch (choice){
                    case 0:
                                firstName = JOptionPane.showInputDialog("Enter first name");
                                lastName = JOptionPane.showInputDialog("Enter last name");
                                personReference = new Person(firstName, lastName, i);
                                LinkedList.push(personReference);
                            break;                   
                    case 1:
                            if (LinkedList.isEmpty()){
                                JOptionPane.showMessageDialog(
                                    null,
                                    "Cannot pop; stack is empty!");
                            }
                            else{
                                personReference = LinkedList.pop();
                                JOptionPane.showMessageDialog(
                                    null,
                                    "The top dog on the stack was "
                                        + personReference.getName() 
                                        + " of breed "
                                        + personReference.getTicketNumber() + ".");
                            }
                            break;
     
                    case 2:
                            if (LinkedList.isEmpty()){
                                JOptionPane.showMessageDialog(
                                    null,
                                    "The stack is empty!");
                            }
                            else {
                                JOptionPane.showMessageDialog(
                                    null,
                                    "The stack is not empty!");
                            }
                            break;
     
                    case 3: 
                            done = true;
                            break;
                    default: 
                            JOptionPane.showMessageDialog(
                                    null,
                                    "Invalid selection; try again!");
                    } //end switch
                } // end try
                catch (NoSuchElementException e){
                    JOptionPane.showMessageDialog(
                            null,
                            "The Stack is Empty",
                            "",
                            JOptionPane.ERROR_MESSAGE);
     
                } // end catch
     
           } // end while
        }
    }

    package tester;
     
    public class LinkedList {
     
        public static ListNode firstLink;
     
     
        LinkedList()
        {
            firstLink = null;
        }
     
        public static boolean isEmpty()
        {
            return(firstLink == null);
        }
     
        public static push(Person parentheses)
        {
            firstLink = new ListNode(parentheses, firstLink);
     
       public static pop()
        {
            firstLink = firstLink.next;
            return(firstLink);
        }
     
     
    }

    package tester;
     
    class ListNode
    {
        public ListNode(Person parentheses)
        {
            this(parentheses, null);
        }
     
        public ListNode(Person parentheses, ListNode n)
        {
            element = parentheses;
            next = n;
        }
        public Object element;
        public ListNode next;
    }

    package tester;
     
    public class Person {
     
        private final String fName;
        private final String lName;
        public int counter;
     
        public Person(String fName, String lName, int inCounter)
        {
            this.fName = fName;
            this.lName = lName;
            this.counter = inCounter;
            counter++;
        }
     
        public String getName()
        {
            return (fName + lName);
        }
     
        public int getTicketNumber()
        {
            return(counter);
        }
     
    }

    The problem atm is with the pop method. In the LinkedList class the error reports this when I highlight the first line for the method: "invalid method declaration; return type required" Followed by: "Incompatible types: unexpected return value" when I highlight the return statement. This doesn't make sense to me on two fronts a) there is a return being made and b) the method is not void

    Then in Tester when I try to call
    personReference = LinkedList.pop();
    on line 43 the error says this when I highlight that line: "void cannot be converted to person". Now im not sure what's void personReference?

  17. #16
    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: JOptionPane help

    Can you copy the full text of the compiler's error messages and paste them here?
    They should include the full text of the statement with the error along with the error message.
    The message should show the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my answer, don't ignore it, ask a question.

  18. #17
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane help

    Yes sorry here is the full error:

    "ant -f C:\\Users\\rob\\Documents\\NetBeansProjects\\Teste r -Dnb.internal.action.name=rebuild clean jar
    init:
    deps-clean:
    Updating property file: C:\Users\rob\Documents\NetBeansProjects\Tester\bui ld\built-clean.properties
    Deleting directory C:\Users\rob\Documents\NetBeansProjects\Tester\bui ld
    clean:
    init:
    deps-jar:
    Created dir: C:\Users\rob\Documents\NetBeansProjects\Tester\bui ld
    Updating property file: C:\Users\rob\Documents\NetBeansProjects\Tester\bui ld\built-jar.properties
    Created dir: C:\Users\rob\Documents\NetBeansProjects\Tester\bui ld\classes
    Created dir: C:\Users\rob\Documents\NetBeansProjects\Tester\bui ld\empty
    Created dir: C:\Users\rob\Documents\NetBeansProjects\Tester\bui ld\generated-sources\ap-source-output
    Compiling 4 source files to C:\Users\rob\Documents\NetBeansProjects\Tester\bui ld\classes
    C:\Users\rob\Documents\NetBeansProjects\Tester\src \tester\LinkedList.java:23: error: invalid method declaration; return type required
    public static pop()
    1 error
    C:\Users\rob\Documents\NetBeansProjects\Tester\nbp roject\build-impl.xml:923: The following error occurred while executing this line:
    C:\Users\rob\Documents\NetBeansProjects\Tester\nbp roject\build-impl.xml:263: Compile failed; see the compiler error output for details.
    BUILD FAILED (total time: 1 second)"

    I tried changing the pop method to type Person but then it talks about not being able to convert a node to a person.

  19. #18
    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: JOptionPane help

    invalid method declaration; return type required
    public static pop()
    All methods are required to define what they return. void is used to say nothing is returned.
    See the tutorial: Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    If you don't understand my answer, don't ignore it, ask a question.

  20. #19
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane help

    Right but the method isn't actually void. That's why I am so confused. Here is the method:
    public static pop()
        {
            firstLink = firstLink.next;
            return(firstLink);
        }

    Edit: For instance, if I change it to:
    public static Person pop()

    Then the error goes away but like I said now I am trying to convert a node type to a person. Which of course would be a problem even if the void error didn't occur.
    Last edited by vysero; September 24th, 2014 at 01:00 PM.

  21. #20
    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: JOptionPane help

    return(firstLink);
    The method's return type should match the type that is returned. What type is firstLink?
    If you don't understand my answer, don't ignore it, ask a question.

  22. #21
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane help

    Ah ic so I changed the method type to ListNode but now I can't use the method since its of type Node and Node cannot be converted to Person. Is there any way around this or am I not going to be able to call my methods on an instance of Person? Seems odd since push returns.

  23. #22
    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: JOptionPane help

    If the pop() method is supposed to return a Person object, Where in the pop() method is there a reference to an instance of the Person class?
    What is in the node?
    If you don't understand my answer, don't ignore it, ask a question.

  24. #23
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane help

    Well when I push a person and set it equal to firstLink doesn't that make firstLink a reference to a person object?
    firstLink = new ListNode(person, firstLink);

  25. #24
    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: JOptionPane help

    firstLink = new ListNode(
    Look at that part of the assignment statement to see what is assigned to firstLink.

    new ListNode(person, firstLink);
    Look at this part of the statement to see what args are passed to the ListNode constructor.
    If you don't understand my answer, don't ignore it, ask a question.

  26. #25
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane help

    Okay so now I am thinking I shouldn't be trying to return anything at all. Which puts me back at square one were void type cannot be converted to Person.

Page 1 of 2 12 LastLast

Similar Threads

  1. JOptionPane
    By Kazeth in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 29th, 2014, 11:42 PM
  2. [SOLVED] JOptionPane
    By colognem in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 13th, 2014, 06:45 AM
  3. need help in JOptionPane
    By Zuckerberg in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 28th, 2014, 07:08 AM
  4. Still having problem with JOptionPane
    By runkerr in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 28th, 2013, 07:40 PM
  5. JOptionPane using If and Else
    By Liuric in forum Member Introductions
    Replies: 7
    Last Post: October 1st, 2010, 12:05 AM