Dialog Boxes - a challenge set by myself

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brendanmcdonagh
    New Member
    • Nov 2007
    • 153

    Dialog Boxes - a challenge set by myself

    Hi all,

    I'm just getting to grips with java and have set my self a challenge to consolidate my learnings so far as well as implement new learnings.

    I want to enter a load of possible things to do into sets and then randomly generate a set value, which is a suggestion of what to do(Geeky - know!) There will be numerous sets, a set for me and my partner - my dog - my son. A set for me and my partner - my dog - no son, etc - basically what i want is to be asked if son and dog are with us in modal boxes and then navigate to a relevent set depending on input. Then generate a suggestion.

    I will add my code thus far - it may be ver different to what others would do but i'm using what i ve learnt so far. Now, I haven't use modal/dialog boxes in java but have in VB - a while back. So first thing i need to do is learn how to use them. What code do i use and what do i import? I want to add a yes/no box in the launch method. I have learnt how to set the result into a variable but I was learning with the open university and they shown us with there own classes which were prob a sub class of some sort.Any help would be greatly appreciated. Here is my code:

    import java.util.*;
    import java.awt.*;
    import java.awt.event. *;
    import java.swing.*;

    public class Suggestion
    {
    Frame theFrame = new Frame();
    Button addSuggestion = new Button();
    //this order - Dan&Bren-Arial-Dylan
    Set<String> YesYesYes = new HashSet<String> ();
    Set<String> YesYesNo = new HashSet<String> ();
    Set<String> YesNoYes = new HashSet<String> ();
    Set<String> YesNoNo = new HashSet<String> ();
    Set<String> BrenNoNo = new HashSet<String> ();
    public Boolean arial;
    public Boolean dylan;

    public void launch()
    {
    theFrame.setSiz e(500,500);
    theFrame.setLay out(newFlowLayo ut());
    addSuggestion.a ddActionListene r(new ButtonListener( ));
    theFrame.add(ad dSuggestion);
    theFrame.setVis ible(true);

    public static void main(String[] args)
    {
    Suggestion newObject = new Suggestion();
    newObject.launc h();
    }
    }

    Thanks for looking
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) Please use code tags when posting code.
    2.) Do not mix awt and swing.
    3.) Read the specs for the JOptionPane class.

    Comment

    • brendanmcdonagh
      New Member
      • Nov 2007
      • 153

      #3
      But Don't I need Awt for something with this project and Swing for the dialog boxes?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by brendanmcdonagh
        But Don't I need Awt for something with this project and Swing for the dialog boxes?
        What you get from awt is available in swing. That Frame for example should be replaced by a JFrame.

        Comment

        • brendanmcdonagh
          New Member
          • Nov 2007
          • 153

          #5
          I get you now - thanks

          So Frame aFrame = new Frame();
          should be JFrame aFrame = new JFrame();

          and I delete java.awt.*;??

          Is this the same for button() JButton()??

          Comment

          • brendanmcdonagh
            New Member
            • Nov 2007
            • 153

            #6
            The JButton and JFrame works with Swing instead of awt but now I can't have new flowlayout as an argument, is there a way around this?

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by brendanmcdonagh
              I get you now - thanks

              So Frame aFrame = new Frame();
              should be JFrame aFrame = new JFrame();

              and I delete java.awt.*;??

              Is this the same for button() JButton()??
              Right on track.

              Comment

              • brendanmcdonagh
                New Member
                • Nov 2007
                • 153

                #8
                Hi I have got myself lost. I have a class as a button listener that does the job asked of it by the main class correctly. Now I want an text area in the frame that allows the user to input into a set. What i am struggling with is the code for a input text field and then on press of button get the text entered as a variable. I know my way around from there.


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

                public class Suggestion
                {
                JFrame theFrame = new JFrame();
                JButton addSuggestion = new JButton("Add Suggestion");
                //this order - Dan&Bren-Arial-Dylan
                Set<String> YesYesYes = new HashSet<String> ();
                Set<String> YesYesNo = new HashSet<String> ();
                Set<String> YesNoYes = new HashSet<String> ();
                Set<String> YesNoNo = new HashSet<String> ();
                Set<String> BrenNoNo = new HashSet<String> ();





                int arialComing = JOptionPane.sho wConfirmDialog
                (// Parent component
                null,
                // String message
                "Is Arial coming?");


                int dylanComing = JOptionPane.sho wConfirmDialog
                (// Parent component
                null,
                // String message
                "Is Dylan coming?");


                public void launch()
                {

                theFrame.setSiz e(500,500);
                theFrame.setLay out(new FlowLayout());
                addSuggestion.a ddActionListene r(new ButtonListener( ));

                theFrame.add(ad dSuggestion);
                theFrame.setVis ible(true);
                }



                public static void main(String[] args)
                {
                Suggestion newObject = new Suggestion();

                newObject.launc h();
                }
                }

                If the person who helped me earlier reads this, I am struggling to enter my code in the code tag - it only enters a few words when I paste. Also I have kept java.awt.* in for now as I don't know how to work with layout s without it.

                Regards

                Comment

                • Nepomuk
                  Recognized Expert Specialist
                  • Aug 2007
                  • 3111

                  #9
                  Originally posted by brendanmcdonagh
                  Now I want an text area in the frame that allows the user to input into a set. What i am struggling with is the code for a input text field and then on press of button get the text entered as a variable. I know my way around from there.
                  Do you mean something like a JTextField or a JTextArea?
                  Originally posted by brendanmcdonagh
                  ...
                  If the person who helped me earlier reads this, I am struggling to enter my code in the code tag - it only enters a few words when I paste....
                  To use the code tags, just write [code=java] before and [/code] after your code. That way, your code will look like this:
                  [code=java]import java.util.*;
                  import java.awt.*;
                  import java.awt.event. *;
                  import javax.swing.*;

                  public class Suggestion
                  {
                  JFrame theFrame = new JFrame();
                  JButton addSuggestion = new JButton("Add Suggestion");
                  //this order - Dan&Bren-Arial-Dylan
                  Set<String> YesYesYes = new HashSet<String> ();
                  Set<String> YesYesNo = new HashSet<String> ();
                  Set<String> YesNoYes = new HashSet<String> ();
                  Set<String> YesNoNo = new HashSet<String> ();
                  Set<String> BrenNoNo = new HashSet<String> ();





                  int arialComing = JOptionPane.sho wConfirmDialog
                  (// Parent component
                  null,
                  // String message
                  "Is Arial coming?");


                  int dylanComing = JOptionPane.sho wConfirmDialog
                  (// Parent component
                  null,
                  // String message
                  "Is Dylan coming?");


                  public void launch()
                  {

                  theFrame.setSiz e(500,500);
                  theFrame.setLay out(new FlowLayout());
                  addSuggestion.a ddActionListene r(new ButtonListener( ));

                  theFrame.add(ad dSuggestion);
                  theFrame.setVis ible(true);
                  }



                  public static void main(String[] args)
                  {
                  Suggestion newObject = new Suggestion();

                  newObject.launc h();
                  }
                  }[/code]Greetings,
                  Nepomuk

                  Comment

                  • brendanmcdonagh
                    New Member
                    • Nov 2007
                    • 153

                    #10
                    Thank you for that. I have succeeded now!

                    Am I being really stupid or is the following complex?:

                    When a textField.getTe xt() is added to a map, when the program finishes running, that Map will go back to default won't it? What do programmers do to avoid this to make sure the amendment to the map is permanent?

                    Comment

                    • Nepomuk
                      Recognized Expert Specialist
                      • Aug 2007
                      • 3111

                      #11
                      Originally posted by brendanmcdonagh
                      Thank you for that. I have succeeded now!

                      Am I being really stupid or is the following complex?:

                      When a textField.getTe xt() is added to a map, when the program finishes running, that Map will go back to default won't it? What do programmers do to avoid this to make sure the amendment to the map is permanent?
                      What instance creates that map? How is it saved?

                      If you want some part of your program to change a map, that's no problem. The changes will continue to exist after that function has finished.
                      If the complete program is stopped any you want changes saved, you'll have to write a file to the hard drive. This howto explains the basics of reading and writing files in Java. The choice of format depends on what sort of information you're going to be saving.

                      Greetings,
                      Nepomuk

                      Comment

                      Working...