Results 1 to 9 of 9
  1. #1
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default Getting userinput from GUI

    Ok so I am trying to get the user input from a GUI, i am not too familiar. I changed code from the cardlayout example from SUN.

    Java Code:
    /*
     * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     *   - Redistributions of source code must retain the above copyright
     *     notice, this list of conditions and the following disclaimer.
     *
     *   - Redistributions in binary form must reproduce the above copyright
     *     notice, this list of conditions and the following disclaimer in the
     *     documentation and/or other materials provided with the distribution.
     *
     *   - Neither the name of Sun Microsystems nor the names of its
     *     contributors may be used to endorse or promote products derived
     *     from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
     * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */
    
    
    
    /*
     * CardLayoutDemo.java
     *
     */
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class CardLayoutDemo implements ItemListener {
        JPanel cards; //a panel that uses CardLayout
       // final static String BUTTONPANEL = "Card with JButtons";
        final static String TEXTPANEL = "Enter the amount of Prime Numbers you would like to compute up to.";
    
        public void addComponentToPane(Container pane) {
            //Put the JComboBox in a JPanel to get a nicer look.
            JPanel comboBoxPane = new JPanel(); //use FlowLayout
            String comboBoxItems[] = {TEXTPANEL };
            JComboBox cb = new JComboBox(comboBoxItems);
            cb.setEditable(false);
            cb.addItemListener(this);
            comboBoxPane.add(cb);
    
            //Create the "cards".
            //JPanel card1 = new JPanel();
           // card1.add(new JButton("Button 1"));
            //card1.add(new JButton("Button 2"));
            //card1.add(new JButton("Button 3"));
    
            JPanel card2 = new JPanel();
           // card2.add(new JTextField("TextField", 20));
            card2.add(new JTextField("How many clients do you have?", 20));
            
            card2.add(new JButton("OK"));
            
           // card2.addKeyListener(null);
            
            
    
    [B]How do I get the user input from here?[/B]
    
    
    
            //Create the panel that contains the "cards".
            cards = new JPanel(new CardLayout());
            //cards.add(card1, BUTTONPANEL);
            cards.add(card2, TEXTPANEL);
    
            pane.add(comboBoxPane, BorderLayout.PAGE_START);
            pane.add(cards, BorderLayout.CENTER);
        }
    
        public void itemStateChanged(ItemEvent evt) {
            CardLayout cl = (CardLayout)(cards.getLayout());
            cl.show(cards, (String)evt.getItem());
        }
    
        /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event dispatch thread.
         */
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("[3dslice.net] PrimeGen");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    
    
    
            //Create and set up the content pane.
            CardLayoutDemo demo = new CardLayoutDemo();
            demo.addComponentToPane(frame.getContentPane());
    
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            /* Use an appropriate Look and Feel */
            try {
                //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            } catch (UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            } catch (InstantiationException ex) {
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
            /* Turn off metal's use of bold fonts */
            UIManager.put("swing.boldMetal", Boolean.FALSE);
    
            //Schedule a job for the event dispatch thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }

  2. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    just thought...

    Java Code:
    final static String TEXTPANEL = "1 2 3 4 5 6 7 8 9 10 ...";
    String comboBoxItems[] = TEXTPANEL.split("\\s");
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  3. #3
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    I don't follow that.

  4. #4
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    11

    Default

    ok so you created a textfield why did you?

    remove the actionlistener statement?.

    basically, your making a textfield but program doesnt know what to do with it?

  5. #5
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Yes, the program doesn't know what do with it, I want to read from it after the button OK is clicked.

    I just don't know how. That listener I put in, is commented out.

  6. #6
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    11

  7. #7
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Well I see that, but I am having trouble implementing it with the code I got now, I mean I can do cards2.something, and i have implemented ActionListener (not shown)

    But since I can do cards2.add(Jbutton or w/e)
    or textfield
    I don't know how to specify, The textfield of cards2.*getuserinput*

    I don't know if that makes sense.

  8. #8
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    11

    Default

    actually, you can try the method gettext().

    because Jtextfield inherits that method from jtextcomponent.

    JTextField (Java 2 Platform SE v1.4.2)

    this might help too

    How to Use Text Fields (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

  9. #9
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    why you don't follow that? The code is to use JCombobox as an input tool. Or you want to modify it totally in a new way?

    Java Code:
    final static String TEXTPANEL = "1 2 3 4 5 6 7 8 9 10 ...";
    ...
    String comboBoxItems[] = TEXTPANEL.split("\\s");
    ...
    cb.setEditable(true);
    and moreover, don't you see the method

    public void itemStateChanged(ItemEvent evt) {
    CardLayout cl = (CardLayout)(cards.getLayout());
    cl.show(cards, (String)evt.getItem()); //<-------------
    }
    second parameter is the JCombobox item :)
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •