1

Issue ended up not being what I thought. Final comments give correct answer...

I'm trying to get a program to input employee information, and output the information when asked.

By hitting "New Employee" on a custom showOptionDialog button, the user would be prompted to enter information about the employee.

By hitting "Retrieve Information", the user would be able to retrieve the information by inputting the Employee's name or number. (this part has not been implemented yet in my code, however)

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

    public class InputList {
        public static void main(String[] args) {

            JFrame frame = new JFrame("EmployeeDatabase");

            Object[] options = {
                    "New Employee",
                    "Retrieve Information"
                };

            int selectedValue = JOptionPane.showOptionDialog(
                    null,
                    "Select an option",
                    "Employee Database",
                    JOptionPane.DEFAULT_OPTION,
                    JOptionPane.PLAIN_MESSAGE,
                    null,
                    options,
                    options[0]
                );

            if(selectedValue == 0) {

                String inputStringName = JOptionPane.showInputDialog(
                        "Employee Name",
                        "Last Name, First Name"
                    );

                String inputStringIdentificationNumber = JOptionPane.showInputDialog(
                        "Employee Number",
                        "---"
                    );

                String inputStringAge = JOptionPane.showInputDialog(
                        "Employee Age",
                        "--"
                    );

                String inputStringWorkLevel = JOptionPane.showInputDialog(
                        "Reported Employee Skill Level",
                        "1 - Best : 20 - Worst"
                    );


                if(inputStringName == null) {
                    return;
                }

                String employeeName = inputStringName;
                String employeeIDNumber = inputStringIdentificationNumber;
                String employeeAge = inputStringAge;
                String employeeWorth = inputStringWorkLevel;

                JOptionPane.showMessageDialog(
                        null,
                        employeeIDNumber
                            + "\n"
                            + employeeName
                            + "\n"
                            + employeeAge
                            + " years old"
                            + "\nRating: "
                            + employeeWorth,
                        "Employee Information",
                        JOptionPane.PLAIN_MESSAGE
                    );


            } else if(selectedValue == 1) {
                JOptionPane.showMessageDialog(
                        null,
                        "Program Incomplete",
                        JOptionPane.PLAIN_MESSAGE
                    );
            }
        }
    }

So, I have my custom "New Employee" and "Retrieve Information" buttons, as set up first.

I attempted to create a showOptionDialog, but every time I do, I get this error message:

InputList.java:75: error: no suitable method found for showMessageDialog(<null>,String,int)
                                        JOptionPane.showMessageDialog(
                                                   ^
    method JOptionPane.showMessageDialog(Component,Object) is not applicable
      (actual and formal argument lists differ in length)
    method JOptionPane.showMessageDialog(Component,Object,String,int) is not applicable
      (actual and formal argument lists differ in length)
    method JOptionPane.showMessageDialog(Component,Object,String,int,Icon) is not applicable
      (actual and formal argument lists differ in length)
1 error

I've attempted to replace the first null with frame, but I can't seem to understand how to use JFrame with JDialog and JOptionPane, as told to use by the Java API... any help is appreciated.

Since comments won't work properly:

 Object[] options = { "OK", "CANCEL" };
 JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning",
             JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
             null, options, options[0]);

This is the stencil I'm attempting to use

and this is the information that's messing me up:

The JOptionPane constructors do not include this argument. Instead, you specify the parent frame when you create the JDialog that contains the JOptionPane, and you use the JDialog setLocationRelativeTo method to set the dialog position.

  • 1
    All that for just a `"non-static variable this cannot be referenced from a static context"` question. Please [search on the error message](https://www.google.com/search?q=site%3Astackoverflow.com+java+non-static+variable+this+cannot+be+referenced+from+a+static+context) **before** posting yet another question on the topic. – Hovercraft Full Of Eels Sep 24 '17 at 02:15
  • As for the other errors -- study the [JOptionPane API](http://docs.oracle.com/javase/8/docs/api/javax/swing/JOptionPane.html) to see what methods are available for this class, and what parameters these methods require. – Hovercraft Full Of Eels Sep 24 '17 at 02:17
  • **Again** your answer lies in the JOptionPane API, and API study skills are necessary for Java programmers. Also feel free (again) to [search this site for similar questions](https://www.google.com/search?q=site%3Astackoverflow.com+java+method+JOptionPane.showMessageDialog+is+not+applicable). – Hovercraft Full Of Eels Sep 24 '17 at 02:21
  • You're trying to call a method that doesn't exist -- passing in the wrong parameters. The API will show you what parameters are required. – Hovercraft Full Of Eels Sep 24 '17 at 02:22
  • OK what error messages are you seeing with the last most recent attempt, the new code that you've posted? – Hovercraft Full Of Eels Sep 24 '17 at 02:46
  • And `null` should work fine as the first parameter, and must be used if your program doesn't have a window already visible. – Hovercraft Full Of Eels Sep 24 '17 at 02:49
  • This works fine for me: [pastebin](https://pastebin.com/xDRrwBmz). What are you doing differently? Post a complete small program similar to what I've posted. – Hovercraft Full Of Eels Sep 24 '17 at 02:53
  • ...ok figured out part of my issue, apparently I misunderstood my error. The issue isn't with the original OptionDialog, it's apparently how I do the MessageDialog. This is a small-scale version of [my program](https://pastebin.com/VCTvJuqM) – Fire Dragon Sep 24 '17 at 03:02
  • Aight, so I figured out my issue now. I had no title for the messageDialog, and I was trying to make it a string instead of just it occurring... Thanks for your help! – Fire Dragon Sep 24 '17 at 03:09
  • Good, or even better [get all the data in one joptionpane](https://pastebin.com/LSkqL3jU) – Hovercraft Full Of Eels Sep 24 '17 at 03:10
  • Oh cool, that works much better, thank you – Fire Dragon Sep 24 '17 at 03:16

0 Answers0