Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > easy question

Reply
Thread Tools

easy question

 
 
koko
Guest
Posts: n/a
 
      05-16-2006
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?

 
Reply With Quote
 
 
 
 
Jeroen V.
Guest
Posts: n/a
 
      05-16-2006
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/tutor...ing/index.html

for(int i = 0; i < userInput; i++){
container.add(new JTextField());
}
 
Reply With Quote
 
 
 
 
Mark Thomas
Guest
Posts: n/a
 
      05-16-2006
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/tutor...ing/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
 
Reply With Quote
 
Jeffrey Schwab
Guest
Posts: n/a
 
      05-16-2006
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/tutor...ing/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()
 
Reply With Quote
 
Jeroen V.
Guest
Posts: n/a
 
      05-16-2006
> What would you do to cause a repaint with the new components at this
> point?
>

....

container =
someFrameYouHaveToCreateAndConfigureBeforeLikeInTh eJavaSwingTutorial.getRootPane();
 
Reply With Quote
 
Mark Thomas
Guest
Posts: n/a
 
      05-16-2006
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/tutor...ing/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
 
Reply With Quote
 
koko
Guest
Posts: n/a
 
      05-16-2006
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.

 
Reply With Quote
 
Jeffrey Schwab
Guest
Posts: n/a
 
      05-16-2006
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/tutor...ing/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_CLOS E);
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);
}
});
}
}
 
Reply With Quote
 
Alan Krueger
Guest
Posts: n/a
 
      05-17-2006
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.
 
Reply With Quote
 
Jeroen V.
Guest
Posts: n/a
 
      05-17-2006
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()
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Easy come, easy go magwhite Wireless Networking 0 12-31-2007 09:29 AM
Is it reasonably easy easy to something like this with python? Bruno Desthuilliers Python 5 08-29-2007 07:40 AM
Stripping audio from qa DVD. Easy or not so easy? GJ Computer Support 1 05-23-2007 02:03 AM
Shared folders - easy question JLunis Wireless Networking 7 03-11-2005 11:33 PM
easy to look at and easy to maintain web page menuing system. Hazzard ASP .Net 2 04-06-2004 03:51 AM



Advertisments