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 2 of 2

Thread: Parsing information from a Custom Dialogue Box

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Parsing information from a Custom Dialogue Box

    I find images are the best way of getting the point across easier so if you take a quick look at the image, you'll see what I have so far


    The "Custom Dialogue Box" is what comes up after the user clicks "Change Alien Colour" from the Main Window, and that's where the problem arises.

    I need the custom dialogue box to know what name the user has entered and what colour has been chosen from the JRadioButtons, and then once "OK" has been clicked it checks whether a name and colour have been chosen and changes that specific alien colour if both have been chosen, or displays an error Dialogue Box asking the user to input the name/choose a colour if they haven't.

    CUSTOM DIALOGUE BOX CLASS
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class AlienColourDialog extends JDialog
        implements ActionListener
    {
     
        private JTextField alienname;
        private String prompt;
        private JButton OK, cancel;
        private boolean cancelled;
        private JRadioButton bodycolor1;
        private JRadioButton bodycolor2;
        private JRadioButton bodycolor3;
        private JRadioButton bodycolor4;
        private JRadioButton bodycolor5;
        private JRadioButton bodycolor6;
        private JRadioButton bodycolor7;
        private JRadioButton bodycolor8;
     
        public AlienColourDialog(Frame owner, 
                           String title, 
                           String prompt)
        {
            super(owner, title, true);
            setSize(400, 140);
            setResizable(false);
            setDefaultCloseOperation(HIDE_ON_CLOSE);
     
            this.prompt = prompt;
            cancelled = true;
     
            setupGUI();
     
            setVisible(true);
        }
     
        private JPanel setupNorthPanel()
        {
            JLabel aliennamelabel = new JLabel("Alien name:", JLabel.CENTER);
            alienname = new JTextField(10);
     
            JPanel north = new JPanel();
            north.setOpaque(true);
            north.add(aliennamelabel);
            north.add(alienname);
            return north;
        }
     
        private JPanel setupCenterPanel()
        {
            JLabel bodycolorlabel = new JLabel("Body colour:", JLabel.CENTER);
     
            bodycolor1 = new JRadioButton("");
            bodycolor1.setBackground(Color.BLUE);
            bodycolor1.addActionListener(this);
     
            bodycolor2 = new JRadioButton("");
            bodycolor2.setBackground(Color.CYAN);
            bodycolor2.addActionListener(this);
     
            bodycolor3 = new JRadioButton("");
            bodycolor3.setBackground(Color.GREEN);
            bodycolor3.addActionListener(this);
     
            bodycolor4 = new JRadioButton("");
            bodycolor4.setBackground(Color.MAGENTA);
            bodycolor4.addActionListener(this);
     
            bodycolor5 = new JRadioButton("");
            bodycolor5.setBackground(Color.ORANGE);
            bodycolor5.addActionListener(this);
     
            bodycolor6 = new JRadioButton("");
            bodycolor6.setBackground(Color.PINK);
            bodycolor6.addActionListener(this);
     
            bodycolor7 = new JRadioButton("");
            bodycolor7.setBackground(Color.RED);
            bodycolor7.addActionListener(this);
     
            bodycolor8 = new JRadioButton("");
            bodycolor8.setBackground(Color.YELLOW);
            bodycolor8.addActionListener(this);
     
             ButtonGroup bodycolorgroup = new ButtonGroup();
             bodycolorgroup.add(bodycolor1);
             bodycolorgroup.add(bodycolor2);
             bodycolorgroup.add(bodycolor3);
             bodycolorgroup.add(bodycolor4);
             bodycolorgroup.add(bodycolor5);
             bodycolorgroup.add(bodycolor6);
             bodycolorgroup.add(bodycolor7);
             bodycolorgroup.add(bodycolor8);
     
             JPanel center = new JPanel();
             center.setOpaque(true);
             center.add(bodycolorlabel);
             center.add(bodycolor1);
             center.add(bodycolor2);
             center.add(bodycolor3);
             center.add(bodycolor4);
             center.add(bodycolor5);
             center.add(bodycolor6);
             center.add(bodycolor7);
             center.add(bodycolor8);
     
             return center;
        }
     
        private JPanel setupSouthPanel()
        {
            OK = new JButton("OK");
            OK.addActionListener(this);
     
            cancel = new JButton("Cancel");
            cancel.addActionListener(this);
     
            JPanel south = new JPanel();
            south.setOpaque(true);
            south.add(OK);
            south.add(cancel);
     
            return south;
        }
     
        public void setupGUI()
        {
            JPanel northPanel = setupNorthPanel();
            JPanel centerPanel = setupCenterPanel();
            JPanel southPanel = setupSouthPanel();
     
            getContentPane().setBackground(Color.BLACK);
            getContentPane().setLayout(new BorderLayout(0, 0));
     
            getContentPane().add(northPanel, BorderLayout.NORTH);
            getContentPane().add(centerPanel, BorderLayout.CENTER);
            getContentPane().add(southPanel, BorderLayout.SOUTH);
        }
     
        public void actionPerformed(ActionEvent e)
        {
     
            if (e.getSource() == cancel)
            {
                cancelled = e.getSource() == cancel;
                dispatchEvent(
                 new WindowEvent(this,
                                WindowEvent.WINDOW_CLOSING));
            }
     
            else if (e.getSource() == OK)
            {
                try
                {
                    //Try and change a specific aliens colour to what the user has input/chosen.
                }
                catch (Exception ex)
                {
                    //If the user hasn't typed anything/chosen anything/inputs an incorrect name. At the moment this isn't showing if I click "OK" and have nothing entered in the fields....
                    JOptionPane.showMessageDialog(null,
                             ex.getMessage(),
                             "Missing data",
                             JOptionPane.ERROR_MESSAGE);
                }      
            }
        }
    }

    THE MAIN WINDOW (ALIEN JFRAME)
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class AlienJFrame extends JFrame implements ActionListener
    {
        private AlienCanvas canvas;
        private JButton randomColourChange;
        private JButton sameColourChange;
        private JButton alienColourChange;
        private JButton help;
     
        public AlienJFrame(Alien[] alien)
        {
            super("Message dialogs");
     
            setSize(400, 300);
            setLocation(200, 100);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
     
            canvas = new AlienCanvas(alien);
     
            getContentPane().setBackground(new Color(0xabcdef));
            getContentPane().add(canvas, BorderLayout.CENTER);
            getContentPane().add(setupSouthPanel(), BorderLayout.SOUTH);
     
            setVisible(true);
        }
     
        private JPanel setupSouthPanel()
        {
            JPanel southPanel = new JPanel();
            southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.Y_AXIS));
            southPanel.add(createButtonPanel());
            southPanel.add(createColourChangePanel());
            return southPanel;
        }
     
        private JPanel createButtonPanel()
        {
            randomColourChange = new JButton("Random colours");
            randomColourChange.addActionListener(this);
     
            sameColourChange = new JButton("Same colour");
            sameColourChange.addActionListener(this);
     
            JPanel buttonPanel = new JPanel();
            buttonPanel.setOpaque(true);
            buttonPanel.add(randomColourChange);
            buttonPanel.add(sameColourChange);
            return buttonPanel;
        }
     
        private JPanel createColourChangePanel()
        {
            alienColourChange = new JButton("Change alien colour");
            alienColourChange.addActionListener(this);
     
            help = new JButton("Help");
            help.addActionListener(this);
     
     
            JPanel p = new JPanel();
            p.setOpaque(true);
            p.add(alienColourChange);
            p.add(help);
            return p;
        }
     
        public void actionPerformed(ActionEvent e)
        {
            if (e.getSource() == randomColourChange)
            {
                canvas.randomColourChange();
            }
            else if (e.getSource() == sameColourChange)
            {
                canvas.sameColourChange();
            }
            else if (e.getSource() == alienColourChange)
            {
                         new AlienColourDialog(null,
                                    "Selecting an Aliens Colour", 
                                    "Test1");
     
                try
                {
     
                }
     
                catch (Exception ex)
                {
                    JOptionPane.showMessageDialog(null,
                             ex.getMessage(),
                             "Missing data",
                             JOptionPane.ERROR_MESSAGE);
                }      
            }
     
            else if (e.getSource() == help)
            {
                JOptionPane.showMessageDialog(null,
                             "To change an alien's body colour: \n    *enter rhe alien's name in the text box \n    *click the ''change alien colour'' button",
                             "Changing an alien's colour",
                             JOptionPane.INFORMATION_MESSAGE);
            }
     
        }
    }

    ALIEN CLASS
    import java.awt.*;
     
    public abstract class Alien
    {
        /*********************************************************
         * 
         * Class-level variables and methods
         * 
         *********************************************************/
        private static Color[] COLOUR_PALETTE = {Color.BLUE, Color.CYAN, 
                                                 Color.GREEN, Color.MAGENTA, 
                                                 Color.ORANGE, Color.PINK,
                                                 Color.RED, Color.YELLOW};
        private static int NEXT_INDEX = -1;
     
        public static Color getColour()
        {
            NEXT_INDEX++;
            return COLOUR_PALETTE[NEXT_INDEX % COLOUR_PALETTE.length];
        }
     
        /*********************************************************
         * 
         * Object-level variables and methods
         * 
         *********************************************************/
        private int x;
        private int y;
        private Color bodyColour;
        private String name;
     
        public Alien()
        {
            this(0, 0);
        }
     
        public Alien(int firstX, int firstY)
        {
            x = firstX;
            y = firstY;
            bodyColour = COLOUR_PALETTE[0];
            name = "?";
        }
     
        public void setPosition(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
     
        public void setBodyColour(Color c)
        {
            bodyColour = c;
        }
     
        public Color getBodyColour()
        {
            return bodyColour;
        }
     
        public int getX()
        {
            return x;
        }
     
        public int getY()
        {
            return y;
        }
     
        public String getName()
        {
            return name;
        }
     
        public void setName(String name)
        {
            this.name = name;
        }
     
        public abstract void paint(Graphics g);
     
        public abstract int getWidth();
     
        public abstract int getHeight();
    }

    Thanks a lot! And sorry for asking for more help ^^'!
    Last edited by MXA92; March 23rd, 2012 at 01:07 PM. Reason: Image was too small/un-readable


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Parsing information from a Custom Dialogue Box

    What have you tried so far?
    Did you post the code you are working on?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Custom jsp tag does not compile
    By skiabox in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: March 18th, 2012, 12:22 PM
  2. Custom JList
    By daghost in forum AWT / Java Swing
    Replies: 3
    Last Post: November 17th, 2011, 05:57 PM
  3. Custom Log level help
    By seanman in forum Java SE APIs
    Replies: 1
    Last Post: September 25th, 2011, 08:55 PM
  4. can i use custom buttons ?
    By Gaurav18 in forum Member Introductions
    Replies: 3
    Last Post: September 12th, 2011, 09:56 AM
  5. how to write an if statement which evaluates 3 dialogue boxes
    By humdinger in forum Loops & Control Statements
    Replies: 6
    Last Post: January 14th, 2010, 01:28 PM