We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,703 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Add jtextfields automatically when a button is pressed

Hi !
I have a JFrame which contains some textfields and buttons. imagine a situation that a user wants to fill sth in textfield, then she/he needs more textfields to be shown ! I want when the user clicks a button, more textfields will appear !
its like cc/bcc in some Email services. you enter an address in "TO" and if you need to add BCC, just click on "add bcc" and a new textfield for entering address as BCC, will appear.
Can anyone help me to write it in Java?
I have tried this but no result :

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {

    jButton4.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
         jButton4.add(new JTextField("j"));
              NewJFrame a = new NewJFrame();

a.add( new JTextField("j"));

        a.validate();

a.repaint();
a.setVisible(true);

Can anyone help me or write for me a whole new code ? Thanks a lot :)

4
Contributors
5
Replies
13 Hours
Discussion Span
8 Months Ago
Last Updated
6
Views
sanilo
Newbie Poster
2 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Can you make a small program that compiles, executes and tries to do what you want done?
SOmething that we can use for testing.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

You're close. All you need to do is to add the new field to the existing visible JFrame - not a button, not a new frame, just the existing one.
Depending on what layout manager you are using you may also need to call pack() afterwards to let the layout manager make space for the new field.

JamesCherrill
... trying to help
Moderator
8,500 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,454
Skill Endorsements: 29

ok so can you edit the above text for me?

sanilo
Newbie Poster
2 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

why are you creating a new JFrame? there 's no need whatso ever to do so.
just instantiate a new JTextField and add that to the current JFrame.

stultuske
Industrious Poster
4,369 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 23

ok so can you edit the above text for me?

No, absolutely not. This is not a "we do your homework for you" service.
We will help you to learn Java, and help you to write your own programs, but you have to put in the effort.
Try to write this yourself. Get as far as you can, then come back here for help on whatever it is that's stopping you.

JamesCherrill
... trying to help
Moderator
8,500 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,454
Skill Endorsements: 29

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0753 seconds using 2.76MB