2

I've been looking for the past hour, but I haven't been able to find the solution I am looking for.

I'm wanting to take multiple inputs from the user using JOptionPane, but I don't want them to all be in one dialog window. I'm wanting it to transition to the next or just make the next one pop up. Is there a way to do that using JOptionPane?

Here's what I have so far:

import java.util.Scanner;
import javax.swing.*;
public class HomeWork2 {


    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        Scanner input2 = new Scanner(System.in);
        Scanner input3 = new Scanner(System.in);
        Scanner input4 = new Scanner(System.in);
        int days, assignments;
        double temperature;
        boolean isRaining;

        JOptionPane.showInputDialog("How many days are left?");
        days = input.nextInt();

        JOptionPane.showInputDialog("How many assignments are due?");
        assignments = input2.nextInt();

        JOptionPane.showInputDialog("What is the temperature outside?");
        temperature = input3.nextDouble();

        JOptionPane.showInputDialog("Is it raining today?");
        isRaining = input4.nextBoolean();

        if(assignments<=0)
            JOptionPane.showMessageDialog(null, "Why are you asking in the first place?");
        else
            if(days<5)
                JOptionPane.showMessageDialog(null, "You need to hurry up, time is short.");
            else
                if(assignments>4)
                    JOptionPane.showMessageDialog(null, "You need to hurry up before the assignments pile up. Oh wait...");
                else
                    if(temperature<50)
                        JOptionPane.showMessageDialog(null, "You should start working, it's not like it's warm eoungh to do anything.");
                    else
                        if(isRaining==true)
                            JOptionPane.showMessageDialog(null, "It's raining, you might as well start on your assignments.");
                        else
                            JOptionPane.showMessageDialog(null, "It's nice out and you have some time to spare, go have fun.");

        input.close();
        input2.close();
        input3.close();
        input4.close();

    }

}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
Hunter Warner
  • 23
  • 1
  • 1
  • 4
  • Before asking on a coding forum, you should know the difference between java and js – Jonas Wilms Jan 27 '17 at 22:57
  • Sorry... It was under the recommended. Like I said, I've only been coding for a few weeks. – Hunter Warner Jan 27 '17 at 22:59
  • 2
    1) You're mixing 2 paradigms, console applications and GUI applications, if you've been coding for a few weeks I recommend you to start by learning the basics first in console applications. 2) For example `Scanner input = new Scanner(System.in);` you should have only one of these, you can then do like `days = input.nextInt();` and `assignments = input.nextInt();`. 3) To get the user input in a GUI based application you could use a [`JOptionPane#showInputDialog`](https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html#input)... – Frakcool Jan 27 '17 at 23:09
  • 1
    ... 4) Your program is nothing but a `main` method, learn to use methods and parameters. 5) `JOptionPane.showMessageDialog(null, "You should start working, it's not like it's warm eoungh to do anything.");`, the 1st parameter shouldn't be null but a reference to your `JFrame`. 6) Your indenting is wrong, the `elif` should all be at the same level and as a recommendation use curly braces (`{` and `}`)for them, they will save you a lot of future problems – Frakcool Jan 27 '17 at 23:11
  • I know how to make it work in console applications, I had that working before I made the changes to include JOptionPane. My assignment for my programming class requires me to use the JOptionPane dialog boxes for input and outputs though so I have to find a way to get that working. I'll take a look through that link though, thanks. And I know my indenting is wrong, that's the main reason I was apologizing in the first part of the message. – Hunter Warner Jan 27 '17 at 23:14
  • 1
    7) It seems that you might want to get all user input in just one window (IMHO it would annoy me to get multiple windows popping to ask me for 1 field all the time) or if you still want to annoy your final user then try using a [Card Layout](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) – Frakcool Jan 27 '17 at 23:14
  • So there's no way that I can make it transition to the next question in the same window (at least using JOptionPane)? – Hunter Warner Jan 27 '17 at 23:19
  • @HunterWarner see my answer, the other way would be to use a `JFrame` with a `CardLayout` as I said before, that way you could go to the next question in the same window and not popping a new one. (In your case as it's for a homework you can use all the 4 `JOptionPane` w/o problem but in a real application that wouldn't be the best approach) – Frakcool Jan 27 '17 at 23:54

1 Answers1

7

Apart from my above recommendations, here are some others that will be needed to understand the below code (PLEASE READ THEM ALL BEFORE GOING FOR THE CODE PART ONLY)

  1. Read what a layout manager is and how they work, especially take a look at Grid Layout and Box Layout, Google for examples and explanations if you don't understand the tutorial.

  2. Read what methods are and how they work.

  3. Read about the Event Dispatch Thread (EDT) and its function.

  4. Be careful to not mix console application paradigm and GUI application paradigm. Use one or the other.

  5. Learn How to use Dialogs

  6. Read how to convert a String o a int and look how to convert to double.

  7. For your boolean field I would use a JRadioButton including a ButtonGroup and how to get which radiobutton was selected in a buttongroup:


This code should give you a starting point on your way to finish it yourself

  • The annoyingGui while shorter, is not my favorite since it opens a new dialog for the user each time you want to get an imput from them, which is annoying.

  • The singleDialogInformation() displays a more complex GUI using a JPanel and GridLayout for requesting user information and a BoxLayout to show it back to the user, note that I'm not using 2 different variables, but reassigning the pane variable to a new instance of a JPanel with a different layout.


import java.awt.GridLayout;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class UsingDialogsExample {

    private JFrame frame;
    private JPanel pane;
    private JTextField daysField;
    private JTextField assignmentField;
    private int days = 0;
    private int assignments = 0;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                //Comment / uncomment one of them to see the output related to each sample method.
//              new UsingDialogsExample().annoyingGui();
                new UsingDialogsExample().singleDialogInformation();
            }
        });
    }

    public void annoyingGui() {
        frame = new JFrame("My Frame's Title");

        String daysInput = JOptionPane.showInputDialog(frame, "How many days are left?"); //Get user input on the textfield as a String
        String assignmentsInput = JOptionPane.showInputDialog(frame, "How many assignments are due?");

        try {
            days = Integer.parseInt(daysInput); //Convert the string gotten above to an int
            assignments = Integer.parseInt(assignmentsInput);
        } catch (NumberFormatException nfe) {
            nfe.printStackTrace();
        }

        JOptionPane.showMessageDialog(frame, "The number of days left is: " + days);
        JOptionPane.showMessageDialog(frame, "The number of assignments due is: " + assignments);
    }

    public void singleDialogInformation() {
        pane = new JPanel();
        pane.setLayout(new GridLayout(0, 2, 2, 2));

        daysField = new JTextField(5);
        assignmentField = new JTextField(5);

        pane.add(new JLabel("How many days are left?"));
        pane.add(daysField);

        pane.add(new JLabel("How many assignments are due?"));
        pane.add(assignmentField);

        int option = JOptionPane.showConfirmDialog(frame, pane, "Please fill all the fields", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);

        if (option == JOptionPane.YES_OPTION) {

            String daysInput = daysField.getText();
            String assignmentsInput = assignmentField.getText();

            try {
                days = Integer.parseInt(daysInput);
                assignments = Integer.parseInt(assignmentsInput);
            } catch (NumberFormatException nfe) {
                nfe.printStackTrace();
            }

            pane = new JPanel();
            pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));

            pane.add(new JLabel("Days left: " + days));
            pane.add(new JLabel("Assignments due: " + assignments));

            JOptionPane.showMessageDialog(frame, pane);
        }
    }
}

Screenshots of the annoyingGui:

enter image description here enter image description here

Screenshots of the singleDialogInformation:

enter image description here enter image description here

Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • This method would work perfectly if I were doing this for pleasure (especially since I understand this method better than the one I have to use), but sadly I am not. Thank you for the help though, that link you gave is really helpful. – Hunter Warner Jan 28 '17 at 00:11
  • *if I were doing this for pleasure* well if software development doesn't satisfy you, you shouldn't be in a software career anyway... =/ I hope that my answer gives you an idea and if that solves your question be sure to [accept](http://stackoverflow.com/help/accepted-answer) it – Frakcool Jan 28 '17 at 00:13
  • You misunderstand, I do enjoy programming. What I was saying was if this was for my personal project or leisure programming then this would be perfect, it's just that for this assignment we can only use certain commands in the program. – Hunter Warner Jan 28 '17 at 00:18
  • Well, I think `annoyingGui()` part would fit your requirements then (if I'm not wrong), you could give it a try and try to improve it by your own later. I'm not doing your whole homework for you but giving you ideas for the start point, then you need to implement the logic for it to work as you want, hope it helps a bit at least. If in doubt what each method does you can read the `JOptionPane` docs [here](https://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html) it's always a great idea to have them in hand – Frakcool Jan 28 '17 at 00:20
  • Well the requirement is to use JOptionPane. Multiple dialog windows is what I want personally, but I think I'm just going to have to suck it up and have all of the inputs on one window. I'll be sure to accept your answer if no one else has exactly what I'm looking for (which probably doesn't exist). – Hunter Warner Jan 28 '17 at 00:25
  • In this case as you're learning, you could make use of multiple dialogs since I think it would be easier to understand for you, the tutorial and the docs might help you, I won't write more code for you here since otherwise it will hurt you, as you're learning you need to start thinking how to solve your problems, however if you're still stuck later in something else you can ask another question with the specific problem you have. – Frakcool Jan 28 '17 at 00:28
  • Maybe I didn't do a great job of explaining the issue in the question, the code only opens up one input dialog when I run it. It doesn't come back with any errors but it doesn't finish the sequence either. I'll spend some more time researching and reading up on it. Thanks for all of your help and putting up with my ineptitude in programming. – Hunter Warner Jan 28 '17 at 00:39
  • *but it doesn't finish the sequence* (remove the `if-else` and see what happens) I'm still not sure what your problem is. *my ineptitude in programming* we all start the same, so don't say yourself an inept for programming, your brain will start thinking that and **believing that**, instead say: *my lack of knowledge at the moment that I will get in the future*, it sounds better and tells your brain to prepare learning new things. Programming is a challenge, not many people is able to translate their thoughts (correct or not) into a program understandable by a dump computer – Frakcool Jan 28 '17 at 00:43
  • What's exactly what should happen after you've got all the user input? What should the program show? – Frakcool Jan 28 '17 at 00:46
  • OHHHH, I finially understand what you mean by "You're mixing 2 paradigms." The numbers entered in the inputdialog aren't used because I'm using scanner improperly and so the program wasn't continuing until I entered something in the console. I just need to swap that out for something else and it should be fine. – Hunter Warner Jan 28 '17 at 00:56
  • @HunterWarner Exactly! :) Check how I got user input from the `JOptionPane.showInputDialog()` on `annoyingGui()` method. It should give you an idea, that's why you shouldn't be using console + gui :P – Frakcool Jan 28 '17 at 00:58
  • Yeah, it was just my go to for user inputs since most of our work thus far has been using the console. Thanks for the help, I don't think I would have realized what the issue was without you or if I did it would have taken much longer. – Hunter Warner Jan 28 '17 at 01:05
  • Glad to help :) happy programming ^^ – Frakcool Jan 28 '17 at 01:06