easy question

Discussion in 'Java' started by koko, May 16, 2006.

  1. koko

    koko Guest

    how can I create a program that prompts a user to enter a number into a
    textfield and when they click a button they create that many more
    textfields?
     
    koko, May 16, 2006
    #1
    1. Advertising

  2. koko

    Jeroen V. Guest

    koko wrote:
    > how can I create a program that prompts a user to enter a number into a
    > textfield and when they click a button they create that many more
    > textfields?
    >


    The question is the answer...

    Prob some homework?

    Have a look at http://java.sun.com/docs/books/tutorial/uiswing/index.html

    for(int i = 0; i < userInput; i++){
    container.add(new JTextField());
    }
     
    Jeroen V., May 16, 2006
    #2
    1. Advertising

  3. koko

    Mark Thomas Guest

    Jeroen V. wrote:
    > koko wrote:
    >> how can I create a program that prompts a user to enter a number into a
    >> textfield and when they click a button they create that many more
    >> textfields?
    >>

    >
    > The question is the answer...
    >
    > Prob some homework?
    >
    > Have a look at http://java.sun.com/docs/books/tutorial/uiswing/index.html
    >
    > for(int i = 0; i < userInput; i++){
    > container.add(new JTextField());
    > }

    What would you do to cause a repaint with the new components at this
    point?

    Mark
     
    Mark Thomas, May 16, 2006
    #3
  4. Mark Thomas wrote:
    > Jeroen V. wrote:
    >> koko wrote:
    >>> how can I create a program that prompts a user to enter a number into a
    >>> textfield and when they click a button they create that many more
    >>> textfields?
    >>>

    >>
    >> The question is the answer...
    >>
    >> Prob some homework?
    >>
    >> Have a look at http://java.sun.com/docs/books/tutorial/uiswing/index.html
    >>
    >> for(int i = 0; i < userInput; i++){
    >> container.add(new JTextField());
    >> }

    > What would you do to cause a repaint with the new components at this
    > point?


    pack()
     
    Jeffrey Schwab, May 16, 2006
    #4
  5. koko

    Jeroen V. Guest

    > What would you do to cause a repaint with the new components at this
    > point?
    >

    ....

    container =
    someFrameYouHaveToCreateAndConfigureBeforeLikeInTheJavaSwingTutorial.getRootPane();
     
    Jeroen V., May 16, 2006
    #5
  6. koko

    Mark Thomas Guest

    Jeffrey Schwab wrote:
    > Mark Thomas wrote:
    >> Jeroen V. wrote:
    >>> koko wrote:
    >>>> how can I create a program that prompts a user to enter a number into a
    >>>> textfield and when they click a button they create that many more
    >>>> textfields?
    >>>>
    >>>
    >>> The question is the answer...
    >>>
    >>> Prob some homework?
    >>>
    >>> Have a look at
    >>> http://java.sun.com/docs/books/tutorial/uiswing/index.html
    >>>
    >>> for(int i = 0; i < userInput; i++){
    >>> container.add(new JTextField());
    >>> }

    >> What would you do to cause a repaint with the new components at this
    >> point?

    >
    > pack()


    Would validate() be the way?

    Mark
     
    Mark Thomas, May 16, 2006
    #6
  7. koko

    koko Guest

    thanks and yes it is hw... however, i am getting an error stating my
    container cannot be resolved. not sure how to fix that. thanks for
    the link - lots of good info.
     
    koko, May 16, 2006
    #7
  8. Mark Thomas wrote:
    > Jeffrey Schwab wrote:
    >> Mark Thomas wrote:
    >>> Jeroen V. wrote:
    >>>> koko wrote:
    >>>>> how can I create a program that prompts a user to enter a number
    >>>>> into a
    >>>>> textfield and when they click a button they create that many more
    >>>>> textfields?
    >>>>>
    >>>>
    >>>> The question is the answer...
    >>>>
    >>>> Prob some homework?
    >>>>
    >>>> Have a look at
    >>>> http://java.sun.com/docs/books/tutorial/uiswing/index.html
    >>>>
    >>>> for(int i = 0; i < userInput; i++){
    >>>> container.add(new JTextField());
    >>>> }
    >>> What would you do to cause a repaint with the new components at this
    >>> point?

    >>
    >> pack()

    >
    > Would validate() be the way?


    Depends what you want to do. If you call invalidate()/validate(), the
    components will be laid out anew within their container, but the frame
    won't be resized. At the risk of robbing "koko" of a proper education:


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

    class TextRabbit {

    private JFrame frame = createFrame();

    private JTextField createTextField() {
    final JTextField field = new JTextField();
    field.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    try {
    int i = Integer.parseInt(field.getText());
    while(--i >= 0) {
    frame.add(createTextField());
    }

    } catch(NumberFormatException x) {
    System.err.println("Exiting not-so-gracefully.");
    System.exit(1);
    }

    frame.pack();

    /* If you use in/validate() instead of pack(), you'll
    * have to resize the frame manually to see the new
    * text fields. */

    // frame.invalidate();
    // frame.validate();
    }
    });
    return field;
    }

    private JFrame createFrame() {
    JFrame frame = new JFrame("Text Rabbit");
    frame.setContentPane(Box.createVerticalBox());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(createTextField());
    frame.pack();
    return frame;
    }

    public void setVisible(boolean b) {
    frame.setVisible(b);
    }

    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    new TextRabbit().setVisible(true);
    }
    });
    }
    }
     
    Jeffrey Schwab, May 16, 2006
    #8
  9. koko

    Alan Krueger Guest

    Jeffrey Schwab wrote:
    > At the risk of robbing "koko" of a proper education:

    [...]
    > class TextRabbit {


    Since this was Koko, you should have named it FuzzyKitten or something.
     
    Alan Krueger, May 17, 2006
    #9
  10. koko

    Jeroen V. Guest

    koko wrote:
    > thanks and yes it is hw... however, i am getting an error stating my
    > container cannot be resolved. not sure how to fix that. thanks for
    > the link - lots of good info.
    >

    I meant frame.getContentPane()
     
    Jeroen V., May 17, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike
    Replies:
    1
    Views:
    2,160
  2. Replies:
    2
    Views:
    483
    Ralf Hildebrandt
    May 2, 2005
  3. Mike King

    Easy RegEx Question

    Mike King, Apr 12, 2005, in forum: Perl
    Replies:
    1
    Views:
    2,613
    Mike King
    Apr 13, 2005
  4. Hazzard
    Replies:
    2
    Views:
    656
    Hazzard
    Apr 6, 2004
  5. Bruno Desthuilliers
    Replies:
    5
    Views:
    396
    Bruno Desthuilliers
    Aug 29, 2007
Loading...

Share This Page