1

I need to let users add more text fields to my JFrame so once the size of the containing frame being JPanel has exceeded its original value a scroll pane would step in. In order to be able to do this, I came up with an idea to put one JButton and upon hitting it a new TextField would show up (this was my original idea which doesn't necessarily mean I am right). The problem is, once I call the ActionListener class to add more TextFields and eventually stretch its containing panel, the program asks me to make the JPanel final which in turns doesn't allow for stretching of the panel. In other words, it appears to me that I'm just beating around the bush, please help me out put this together, below is a piece of my code:

public class Button {
    public static void main(String[] args) {

        JFrame f = new JFrame();
        f.setLayout(new BorderLayout());
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JPanel p = new JPanel(new GridLayout(0, 5));
        JScrollPane jsp = new JScrollPane(p);

        jsp.setPreferredSize(new Dimension(300,300));
        jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);

        JButton but = new JButton("Add");
        f.add(but);
        but.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int height=0;
                JTextField jtx = new JTextField();
                jtx.setSize(new Dimension(70,20));
                jtx.setPreferredSize(new Dimension(70,20));    
                p.add(jtf);
                height+=20;
                p.setSize(new Dimension(300, height));
                p.setPreferredSize(new Dimension(300, height)); 
            }
       });
       f.add(jsp, BorderLayout.CENTER);
       f.setLocation(300, 300);
       f.setVisible(true);
       f.pack();
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Ivan Ćeličanin
  • 79
  • 2
  • 3
  • 9
  • 1
    Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). Note the first example shows how to add components dynamically. – Andrew Thompson Sep 01 '13 at 13:08
  • I'm sorry but I don't really understand your question. Do you need to resize your panel everytime you need a new line in your `GridLayout` ? – StepTNT Sep 01 '13 at 13:11
  • Basically, it is supposed to resize everytime a user hits an "add" button so that JPanel, its container, will stretch and from there I will need to make a JScrollingPane scroll down to the bottom of the JPanel. The thing is, since I'm calling for a new JPanel from within the ActionListener and therefore assign JPanel the final attribute, I cannot make it stretch dynamically. – Ivan Ćeličanin Sep 01 '13 at 19:00
  • Andrew, I appreciate your reply. From what I gather from your post, you are suggesting that it is possible for me to put all of these on another JPanel outside of the body of the ActionListener method? Please advise. – Ivan Ćeličanin Sep 01 '13 at 19:46
  • OK, I figured it all out and this works perfect with me! Actually it is not the container that is changing its size dynamically, instead you just add a JScrollPane to its parent container, JPanel that is, and put the container that will hold the text fields in its constructor. Again, thank you for your assistance. – Ivan Ćeličanin Sep 01 '13 at 21:49

1 Answers1

0

I will give you an idea : you can add button and inside the button instruction you would add an instruction set that create text field . if you want more than once then you must handle position pointer that tell you the last place the user add text field then by updating the pointer the user can see the text in different place , if you want the user to control the position of the text then he must enter the location .