Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

easy question

Thread view: 
koko - 16 May 2006 18:44 GMT
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?
Jeroen V. - 16 May 2006 19:15 GMT
> 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());
}
Mark Thomas - 16 May 2006 19:42 GMT
>> 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
[quoted text clipped - 9 lines]
>    container.add(new JTextField());
> }
 What would you do to cause a repaint with the new components at this
point?

Mark
Jeffrey Schwab - 16 May 2006 20:07 GMT
>>> 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
[quoted text clipped - 11 lines]
>  What would you do to cause a repaint with the new components at this
> point?

pack()
Mark Thomas - 16 May 2006 20:55 GMT
>>>> 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
[quoted text clipped - 14 lines]
>
> pack()

Would validate() be the way?

Mark
Jeffrey Schwab - 16 May 2006 21:12 GMT
>>>>> how can I create a program that prompts a user to enter a number
>>>>> into a
[quoted text clipped - 17 lines]
>
> 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);
            }
        });
    }
}
Alan Krueger - 17 May 2006 04:29 GMT
> At the risk of robbing "koko" of a proper education:
[...]
> class TextRabbit {

Since this was Koko, you should have named it FuzzyKitten or something.
Jeroen V. - 16 May 2006 20:24 GMT
>  What would you do to cause a repaint with the new components at this
> point?

...

container =
someFrameYouHaveToCreateAndConfigureBeforeLikeInTheJavaSwingTutorial.getRootPane();
koko - 16 May 2006 21:04 GMT
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.
Jeroen V. - 17 May 2006 09:38 GMT
> 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()


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.