1

I'm teaching myself Java, and I am using netbeans to develop it in. I have made a simple JInternalFrame with a text box inside, but I am wanting to be able to clone it, and then fill the text box with some new text.

I thought I could just create an array of JInternalFrame and keep adding to that, but I dont seem to be able to do that.

I am new, so as much advice as possible would be awesome. :)

mKorbel
  • 109,525
  • 20
  • 134
  • 319
AndyD
  • 105
  • 1
  • 6
  • What exactly do you mean by 'clone'? Are you simply looking for a way to create additional instances with the same setup? Please post a short example of the code you already have, especially the 'adding to array' portion. – sarcan Aug 15 '12 at 13:45
  • I want to be able to create new instances of the JInternalFrame ad infinitum, but on creation, put data into the text box within the newly created frame. The adding to array part didnt work since it was syntactically incorrect. I tried a private JInternalFrame[]; which just failed. – AndyD Aug 15 '12 at 13:49

1 Answers1

3

I'd make a factory method, createInternalFrame(), like they did in the answers here.

enter image description here

private int count;

desktop.add(createInternalFrame(++count));

private JInternalFrame createInternalFrame(int number) {
    JInternalFrame jif = new JInternalFrame(
        "F" + number, true, true, true, false);
    int topLeft = 25 * number;
    jif.add(new JTextField(String.valueOf(number), 8));
    jif.pack();
    jif.setLocation(topLeft, topLeft);
    jif.setVisible(true);
    return jif;
}
Community
  • 1
  • 1
Catalina Island
  • 7,027
  • 2
  • 23
  • 42